ImageSwitcher 滑动切换图片

  布局文件:

<ImageSwitcher
        android:id="@+id/isImage"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        />

动画文件:

left_in.xml

<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android">  
    <translate   
        android:fromXDelta="-100%p"   
        android:toXDelta="0"  
        android:duration="1000" />  
</set>

left_out.xml

<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android">  
    <translate   
        android:fromXDelta="0"   
        android:toXDelta="-100%p"  
        android:duration="1000" />  
</set>

right_in.xml

<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android">  
    <translate   
        android:fromXDelta="100%"   
        android:toXDelta="0"  
        android:duration="1000" />  
</set>

right_out.xml

<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android">  
    <translate   
        android:fromXDelta="0"   
        android:toXDelta="100%p"  
        android:duration="1000" />  
</set>

主函数:

private int[] mImageArray = {R.drawable.goods1, R.drawable.goods2};//图片资源
ImageSwitcher isImage = (ImageSwitcher) rlHomeListViewHead.findViewById(R.id.isImage);
 
isImage.setFactory(this);//设置工厂 要继承ViewSwitcher.ViewFactory
isImage.setImageResource(mImageArray[0]);//初始化显示第一张
isImage.setOnTouchListener(this);//设置监听器
 
// 重写视图工厂中的makeView方法,对ImageSwitcher显示的ImageView对象进行了设置
    @Override
    public View makeView() {
        final ImageView i = new ImageView(mContext);
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        i.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        return i;
    }

 
   
 //重写onTouch构造方法"
@Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();//downX为全局变量:手指按下时x坐标
                break;
            case MotionEvent.ACTION_UP:
                float upX = event.getX(); //upX: 手指抬起时x坐标
                if (upX > downX) {   //向右滑
                    if (mCurrentPosition == 0) {
                        mCurrentPosition = mImageArrar_length - 1;
                    } else {
                        --mCurrentPosition;
                    }
                    isImage.setInAnimation(mContext,R.anim.left_in);
                    isImage.setOutAnimation(mContext,R.anim.right_out);
                }
                if (upX < downX) {    //向左滑
                   if (mCurrentPosition == mImageArrar_length - 1) {
                        mCurrentPosition = 0;
                    } else {
                        ++mCurrentPosition;
                    }
                    isImage.setInAnimation(mContext,R.anim.right_in);
                    isImage.setOutAnimation(mContext,R.anim.left_out);
                }
                isImage.setImageResource(mImageArray[mCurrentPosition]);
                break;
        }
        return true;//返回true才能touch有效
    }






你可能感兴趣的:(ImageSwitcher 滑动切换图片)