Android使用ViewPager实现左右切换(转)

临时转载 全部代码贴上来,学习关闭后整理或删除。

主界面只有一个Activity,无XML文件。


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new ViewFlipperView(this));
    }
}

ViewFlipperView搭配一个接口IAdImages实现主界面的View。

/**
 * 图片信息接口
 * @author haozi
 *
 */
public interface IAdImages {

    /**
     * 广告图片
     */
    int[] adImages = {
            R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04, R.drawable.img05,
            R.drawable.img06, R.drawable.img07, R.drawable.img08, R.drawable.img09, R.drawable.img10
    };
}
/**
 * 这是一个类似于ViewFlipper的Wiget。
 * @author haozi
 *
 */
public class ViewFlipperView extends FrameLayout implements IAdImages {

    private Context context;                           // 调用方的上下文
    private int currentAdImgIndex;                     // 当前广告图片索引
    private Animation left2RightInAnimation;           // 广告图片从左到右进入屏幕动画
    private Animation left2RightOutAnimation;          // 广告图片从左到右出去屏幕动画
    private Animation right2LeftInAnimation;           // 广告图片从右到左进入屏幕动画
    private Animation right2LeftOutAnimation;          // 广告图片从右到左出去屏幕动画
    private int animationDuration = 500;               // 动画花费时间1000毫秒
    private ViewFlipper mViewFlipper;                  // 滑动页面控件
    private LinearLayout mTipLinearLayout;             // 下方点点控件
    private float startX = 0;                          // touch action down 时的x坐标
    private float endX = 0;                            // touch action up 时的x坐标


    public ViewFlipperView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        setView();
    }

    public ViewFlipperView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        setView();
    }

    public ViewFlipperView(Context context) {
        super(context);
        this.context = context;
        setView();
    }

    /**
     * 显示View
     */
    private void setView(){

        // 初始化
        int screenWidth = getResources().getDisplayMetrics().widthPixels;
        mViewFlipper = new ViewFlipper(context);
        mTipLinearLayout = new LinearLayout(context);
        // 初始化动画
        left2RightInAnimation = new TranslateAnimation(-screenWidth, 0, 0, 0);
        left2RightInAnimation.setDuration(animationDuration);
        left2RightOutAnimation = new TranslateAnimation(0, screenWidth, 0, 0);
        left2RightOutAnimation.setDuration(animationDuration);
        right2LeftInAnimation = new TranslateAnimation(screenWidth, 0, 0, 0);
        right2LeftInAnimation.setDuration(animationDuration);
        right2LeftOutAnimation = new TranslateAnimation(0, -screenWidth, 0, 0);
        right2LeftOutAnimation.setDuration(animationDuration);


        // 将广告图片加入ViewFlipper
        for(int i=0; i<adImages.length; i++){
            ImageView image = new ImageView(context);
            image.setImageResource(adImages[i]);
            mViewFlipper.addView(image);
        }
        addView(mViewFlipper);

        // 将图片索引点动态加入
        for(int i=0; i<adImages.length; i++){
            ImageView image = new ImageView(context);
            if(i == 0){
                image.setImageResource(R.drawable.point_selected);
            }else{
                image.setImageResource(R.drawable.point_normal);
            }
            image.setPadding(5, 0, 5, 20);
            mTipLinearLayout.addView(image);
        }
        // 放置在左下角
        mTipLinearLayout.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        addView(mTipLinearLayout);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            startX = event.getX();

            break;
        case MotionEvent.ACTION_UP:

            endX = event.getX();
            // 先保存上一个点
            ImageView lastTipImageView = (ImageView) mTipLinearLayout.getChildAt(currentAdImgIndex);

            if(currentAdImgIndex > 0 && endX > startX){// 查看前一页的广告

                mViewFlipper.setInAnimation(left2RightInAnimation);
                mViewFlipper.setOutAnimation(left2RightOutAnimation);
                mViewFlipper.showPrevious();
                currentAdImgIndex--;
                if(currentAdImgIndex < 0){
                    currentAdImgIndex = 0;
                }
            }

            if(currentAdImgIndex < adImages.length-1 && endX < startX){// 查看后一页的广告

                mViewFlipper.setInAnimation(right2LeftInAnimation);
                mViewFlipper.setOutAnimation(right2LeftOutAnimation);
                mViewFlipper.showNext();
                currentAdImgIndex++;
                if(currentAdImgIndex > adImages.length-1){
                    currentAdImgIndex = adImages.length-1;
                }
            }

            // 根据currentAdImgIndex改变底部点的状态
            ImageView currTipImageView = (ImageView) mTipLinearLayout.getChildAt(currentAdImgIndex);
            lastTipImageView.setImageResource(R.drawable.point_normal);
            currTipImageView.setImageResource(R.drawable.point_selected);

            break;      
        }   
        return true;
    }
}

由于工作原因,无法下载代码。因此贴出代码仅供自己学习。学习结束整理分析或者删除博文。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值