001.package com.h3c.my;

002. 
003.import android.app.Activity;
004.import android.os.Bundle;
005.import android.os.Handler;
006.import android.view.GestureDetector;
007.import android.view.MotionEvent;
008.import android.view.View;
009.import android.view.View.OnTouchListener;
010.import android.view.Window;
011.import android.view.animation.AnimationUtils;
012.import android.widget.Gallery.LayoutParams;
013.import android.widget.ImageSwitcher;
014.import android.widget.ImageView;
015.import android.widget.Toast;
016.import android.widget.ViewSwitcher;
017. 
018.public class MyGalleryActivity extends Activity implements
019.ViewSwitcher.ViewFactory {
020.private ImageSwitcher mSwitcher;
021. 
022.private int mPosition = 0;
023.private GestureDetector mGestureDetector;
024. 
025.private Handler _handle;
026.private Runnable _runable;
027. 
028.private static final float HORIZONTAL_SCROLL_DISTANCE = 10f;
029. 
030.@Override
031.public void onCreate(Bundle savedInstanceState) {
032.super.onCreate(savedInstanceState);
033.requestWindowFeature(Window.FEATURE_NO_TITLE);
034.setContentView(R.layout.main);
035. 
036.mSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitch);
037.mSwitcher.setFactory(this);
038. 
039.setupOnTouchListeners(findViewById(R.id.rootview));
040.mSwitcher.setImageResource(mImageIds[mPosition]);
041. 
042._handle = new Handler();
043._runable = new Runnable() {
044.@Override
045.public void run() {
046. 
047.if (mPosition == (mImageIds.length - 1)) {
048.Toast.makeText(MyGalleryActivity.this"最后一张"0).show();
049.else {
050.mSwitcher.setInAnimation(AnimationUtils.loadAnimation(
051.MyGalleryActivity.this, R.anim.slide_in_right));
052.mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
053.MyGalleryActivity.this, R.anim.slide_out_left));
054.mSwitcher.setImageResource(mImageIds[++mPosition]);
055. 
056._handle.postDelayed(_runable, 3000);
057.}
058.}
059.};
060.}
061. 
062.private void setupOnTouchListeners(View rootView) {
063.mGestureDetector = new GestureDetector(thisnew MyGestureListener());
064. 
065.OnTouchListener rootListener = new OnTouchListener() {
066.public boolean onTouch(View v, MotionEvent event) {
067.mGestureDetector.onTouchEvent(event);
068. 
069.// We do not use the return value of www.it165.net
070.// mGestureDetector.onTouchEvent because we will not receive
071.// the "up" event if we return false for the "down" event.
072.return true;
073.}
074.};
075. 
076.rootView.setOnTouchListener(rootListener);
077.}
078. 
079.public void onPause() {
080.super.onPause();
081._handle.removeCallbacks(_runable);
082.}
083. 
084.private class MyGestureListener extends
085.GestureDetector.SimpleOnGestureListener {
086. 
087.@Override
088.public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
089.float velocityY) {
090.if (Math.abs(velocityY) <= Math.abs(velocityX)
091.&& Math.abs(velocityX) > HORIZONTAL_SCROLL_DISTANCE) {
092.//
093.System.out.println(velocityX);
094.if (velocityX > 0) {
095.if (mPosition > 0) {
096._handle.removeCallbacks(_runable);
097.//
098.mSwitcher.setInAnimation(AnimationUtils.loadAnimation(
099.MyGalleryActivity.this, R.anim.slide_in_left));
100.mSwitcher
101..setOutAnimation(AnimationUtils.loadAnimation(
102.MyGalleryActivity.this,
103.R.anim.slide_out_right));
104.mSwitcher.setImageResource(mImageIds[--mPosition]);
105. 
106.}
107.else {
108.if (mPosition < (mImageIds.length - 1)) {
109._handle.removeCallbacks(_runable);
110. 
111.mSwitcher.setInAnimation(AnimationUtils.loadAnimation(
112.MyGalleryActivity.this, R.anim.slide_in_right));
113.mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
114.MyGalleryActivity.this, R.anim.slide_out_left));
115.mSwitcher.setImageResource(mImageIds[++mPosition]);
116. 
117.else if (mPosition == (mImageIds.length - 1)) {
118._handle.removeCallbacks(_runable);
119.Toast.makeText(MyGalleryActivity.this"注册",0).show();
120.return true;
121.}
122.}
123.}
124. 
125.return true;
126.}
127. 
128.}
129. 
130.private Integer[] mImageIds = { R.drawable.a, R.drawable.b, R.drawable.c,
131.R.drawable.d, R.drawable.e, R.drawable.f };
132. 
133.@Override
134.public View makeView() {
135.ImageView i = new ImageView(this);
136.i.setBackgroundColor(0xFF000000);
137.i.setScaleType(ImageView.ScaleType.FIT_XY);
138.i.setLayoutParams(new ImageSwitcher.LayoutParams(
139.LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
140.return i;
141.}
142.}
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(1) 人氣()