Android 图片全屏滑动效果

openkk 12年前

修改一下代码,还可以有幻灯片效果。

package com.h3c.my;    import android.app.Activity;  import android.os.Bundle;  import android.os.Handler;  import android.view.GestureDetector;  import android.view.MotionEvent;  import android.view.View;  import android.view.View.OnTouchListener;  import android.view.Window;  import android.view.animation.AnimationUtils;  import android.widget.Gallery.LayoutParams;  import android.widget.ImageSwitcher;  import android.widget.ImageView;  import android.widget.Toast;  import android.widget.ViewSwitcher;    public class MyGalleryActivity extends Activity implements    ViewSwitcher.ViewFactory {   private ImageSwitcher mSwitcher;     private int mPosition = 0;   private GestureDetector mGestureDetector;     private Handler _handle;   private Runnable _runable;     private static final float HORIZONTAL_SCROLL_DISTANCE = 10f;     @Override   public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    requestWindowFeature(Window.FEATURE_NO_TITLE);    setContentView(R.layout.main);      mSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitch);    mSwitcher.setFactory(this);      setupOnTouchListeners(findViewById(R.id.rootview));    mSwitcher.setImageResource(mImageIds[mPosition]);      _handle = new Handler();    _runable = new Runnable() {     @Override     public void run() {        if (mPosition == (mImageIds.length - 1)) {       Toast.makeText(MyGalleryActivity.this, "最后一张", 0).show();      } else {       mSwitcher.setInAnimation(AnimationUtils.loadAnimation(         MyGalleryActivity.this, R.anim.slide_in_right));       mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(         MyGalleryActivity.this, R.anim.slide_out_left));       mSwitcher.setImageResource(mImageIds[++mPosition]);         _handle.postDelayed(_runable, 3000);      }     }    };   }     private void setupOnTouchListeners(View rootView) {    mGestureDetector = new GestureDetector(this, new MyGestureListener());      OnTouchListener rootListener = new OnTouchListener() {     public boolean onTouch(View v, MotionEvent event) {      mGestureDetector.onTouchEvent(event);        // We do not use the return value of      // mGestureDetector.onTouchEvent because we will not receive      // the "up" event if we return false for the "down" event.      return true;     }    };      rootView.setOnTouchListener(rootListener);   }     public void onPause() {    super.onPause();    _handle.removeCallbacks(_runable);   }     private class MyGestureListener extends     GestureDetector.SimpleOnGestureListener {      @Override    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,      float velocityY) {     if (Math.abs(velocityY) <= Math.abs(velocityX)       && Math.abs(velocityX) > HORIZONTAL_SCROLL_DISTANCE) {      //      System.out.println(velocityX);      if (velocityX > 0) {       if (mPosition > 0) {        _handle.removeCallbacks(_runable);        //        mSwitcher.setInAnimation(AnimationUtils.loadAnimation(          MyGalleryActivity.this, R.anim.slide_in_left));        mSwitcher          .setOutAnimation(AnimationUtils.loadAnimation(            MyGalleryActivity.this,            R.anim.slide_out_right));        mSwitcher.setImageResource(mImageIds[--mPosition]);         }      } else {       if (mPosition < (mImageIds.length - 1)) {        _handle.removeCallbacks(_runable);          mSwitcher.setInAnimation(AnimationUtils.loadAnimation(          MyGalleryActivity.this, R.anim.slide_in_right));        mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(          MyGalleryActivity.this, R.anim.slide_out_left));        mSwitcher.setImageResource(mImageIds[++mPosition]);         } else if (mPosition == (mImageIds.length - 1)) {        _handle.removeCallbacks(_runable);        Toast.makeText(MyGalleryActivity.this, "注册", 0).show();        return true;       }      }     }       return true;    }     }     private Integer[] mImageIds = { R.drawable.a, R.drawable.b, R.drawable.c,     R.drawable.d, R.drawable.e, R.drawable.f };     @Override   public View makeView() {    ImageView i = new ImageView(this);    i.setBackgroundColor(0xFF000000);    i.setScaleType(ImageView.ScaleType.FIT_XY);    i.setLayoutParams(new ImageSwitcher.LayoutParams(      LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));    return i;   }  }