Android 图片全屏滑动效果

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
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;  
    }  
}  

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:将颜色转换为其反色的PHP代码

下一篇:计算文件大小的php代码