使用imageSwitcher左右划查看手机相片

import android.app.Activity; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.view.animation.AnimationUtils; 
import android.widget.ImageSwitcher; 
import android.widget.ImageView; 
import android.widget.ViewSwitcher.ViewFactory; 
 
/**
 * 一个左右滑动浏览图片的Demo
 * 
 * @author tianjf
 * 
 */ 
public class ImageSwicherDemoActivity extends Activity implements ViewFactory, 
        OnTouchListener { 
 
    private ImageSwitcher imageSwicher; 
 
    // 图片数组  
    private int[] arrayPictures = { R.drawable.bg001, R.drawable.bg002, 
            R.drawable.bg003, R.drawable.bg004 }; 
    // 要显示的图片在图片数组中的Index  
    private int pictureIndex; 
    // 左右滑动时手指按下的X坐标  
    private float touchDownX; 
    // 左右滑动时手指松开的X坐标  
    private float touchUpX; 
 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        imageSwicher = (ImageSwitcher) findViewById(R.id.imageSwicher); 
 
        // 为ImageSwicher设置Factory,用来为ImageSwicher制造ImageView  
        imageSwicher.setFactory(this); 
        // 设置ImageSwitcher左右滑动事件  
        imageSwicher.setOnTouchListener(this); 
    } 
 
    @Override 
    public View makeView() { 
        ImageView imageView = new ImageView(this); 
        imageView.setImageResource(arrayPictures[pictureIndex]); 
        return imageView; 
    } 
 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
        if (event.getAction() == MotionEvent.ACTION_DOWN) { 
            // 取得左右滑动时手指按下的X坐标  
            touchDownX = event.getX(); 
            return true; 
        } else if (event.getAction() == MotionEvent.ACTION_UP) { 
            // 取得左右滑动时手指松开的X坐标  
            touchUpX = event.getX(); 
            // 从左往右,看前一张  
            if (touchUpX - touchDownX > 100) { 
                // 取得当前要看的图片的index  
                pictureIndex = pictureIndex == 0 ? arrayPictures.length - 1 
                        : pictureIndex - 1; 
                // 设置图片切换的动画  
                imageSwicher.setInAnimation(AnimationUtils.loadAnimation(this, 
                        android.R.anim.slide_in_left)); 
                imageSwicher.setOutAnimation(AnimationUtils.loadAnimation(this, 
                        android.R.anim.slide_out_right)); 
                // 设置当前要看的图片  
                imageSwicher.setImageResource(arrayPictures[pictureIndex]); 
                // 从右往左,看下一张  
            } else if (touchDownX - touchUpX > 100) { 
                // 取得当前要看的图片的index  
                pictureIndex = pictureIndex == arrayPictures.length - 1 ? 0 
                        : pictureIndex + 1; 
                // 设置图片切换的动画  
                // 由于Android没有提供slide_out_left和slide_in_right,所以仿照slide_in_left和slide_out_right编写了slide_out_left和slide_in_right  
                imageSwicher.setInAnimation(AnimationUtils.loadAnimation(this, 
                        R.anim.slide_out_left)); 
                imageSwicher.setOutAnimation(AnimationUtils.loadAnimation(this, 
                        R.anim.slide_in_right)); 
                // 设置当前要看的图片  
                imageSwicher.setImageResource(arrayPictures[pictureIndex]); 
            } 
            return true; 
        } 
        return false; 
    } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值