Android幻灯片切换效果

Home / Android MrLee 2016-3-14 4295

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.example.demo;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;
public class FrameLayoutView extends FrameLayout {
    private int mCurrentIndex = 0;
    private Context context;
    private float startX;
    private boolean loop;
    private int mDisplayWidth;
    private Animation mLeftRightInAnimation;
    private Animation mLeftRightOutAnimation;
    private Animation mRightLeftInAnimation;
    private Animation mRightLeftOutAnimation;
    private long duration = 500;
    private ViewFlipper mViewFlipper;
    // 导航索引的容器
    private LinearLayout mTipLinearLayout;
    private View[] views;
    private int onID;
    private int offID;
    public FrameLayoutView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public FrameLayoutView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public FrameLayoutView(Context context) {
        super(context);
    }
    // 初始化控件
    public void initView(View[] views, int onID, int offID) {
        this.context = getContext();
        this.views = views;
        this.onID = onID;
        this.offID = offID;
        // mDisplayWidth = getResources().getDisplayMetrics().widthPixels;
        mViewFlipper = new ViewFlipper(context);
        mTipLinearLayout = new LinearLayout(context);
        // //将图片界面加入mViewFlipper布局中
        for (int i = 0; i < views.length; i++) {
            mViewFlipper.addView(views[i]);
        }
        // 最外层
        LinearLayout layout = new LinearLayout(context);
        ImageView imageLeft = new ImageView(context);
        imageLeft.setImageResource(R.drawable.page_img_left);
        layout.addView(imageLeft);
        // 将下面的那些点点动态加入到LinearLayout布局中
        for (int j = 0; j < views.length; j++) { ImageView imageView = new ImageView(context); if (j == 0) { imageView.setImageResource(onID); } else { imageView.setImageResource(offID); } mTipLinearLayout.addView(imageView); } LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( -2, -2); layoutParams.weight = 1; layout.addView(mTipLinearLayout, layoutParams); mTipLinearLayout.setGravity(Gravity.CENTER); ImageView imageRight = new ImageView(context); imageRight.setImageResource(R.drawable.page_img_right); layout.addView(imageRight); addView(mViewFlipper); // 设置下面的那些点点按底部和水平居中 LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, -2); params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL; addView(layout, params); layout.setGravity(Gravity.CENTER); layout.setPadding(10, 0, 10, 10); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { // TODO Auto-generated method stub super.onLayout(changed, left, top, right, bottom); if (mDisplayWidth == 0) { mDisplayWidth = getWidth(); // 初始化动画 mLeftRightInAnimation = new TranslateAnimation(-mDisplayWidth, 0, 0, 0); mLeftRightInAnimation.setDuration(duration); mLeftRightOutAnimation = new TranslateAnimation(0, mDisplayWidth, 0, 0); mLeftRightOutAnimation.setDuration(duration); mRightLeftInAnimation = new TranslateAnimation(mDisplayWidth, 0, 0, 0); mRightLeftInAnimation.setDuration(duration); mRightLeftOutAnimation = new TranslateAnimation(0, -mDisplayWidth, 0, 0); mRightLeftOutAnimation.setDuration(duration); } } public boolean isLoop() { return loop; } public void setLoop(boolean loop) { this.loop = loop; } public long getDuration() { return duration; } public void setDuration(long duration) { this.duration = duration; } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: startX = event.getX(); break; case MotionEvent.ACTION_UP: // 通过对触摸屏幕时的X坐标的改变来判断导航的方向 int tmpIndex = mCurrentIndex; if (event.getX() > startX) {// 后退
                mCurrentIndex--;
                if (loop) {
                    mViewFlipper.setInAnimation(mLeftRightInAnimation);
                    mViewFlipper.setOutAnimation(mLeftRightOutAnimation);
                    mViewFlipper.showPrevious();
                    if (mCurrentIndex < 0) {
                        mCurrentIndex = views.length - 1;
                    }
                } else {
                    if (mCurrentIndex < 0) {
                        mCurrentIndex = 0;
                        // 到了开头,上次索引为1
                        if (tmpIndex == mCurrentIndex)
                            tmpIndex = 1;
                    } else {
                        mViewFlipper.setInAnimation(mLeftRightInAnimation);
                        mViewFlipper.setOutAnimation(mLeftRightOutAnimation);
                        mViewFlipper.showPrevious();
                    }
                }
            } else if (event.getX() < startX) {// 前进 mCurrentIndex++; if (loop) { mViewFlipper.setInAnimation(mRightLeftInAnimation); mViewFlipper.setOutAnimation(mRightLeftOutAnimation); mViewFlipper.showNext(); if (mCurrentIndex > views.length - 1) {
                        mCurrentIndex = 0;
                    }
                } else {
                    if (mCurrentIndex > views.length - 1) {
                        mCurrentIndex = views.length - 1;
                        // 到了末尾,上次索引为总长度-2
                        if (tmpIndex == mCurrentIndex)
                            tmpIndex = views.length - 2;
                    } else {
                        mViewFlipper.setInAnimation(mRightLeftInAnimation);
                        mViewFlipper.setOutAnimation(mRightLeftOutAnimation);
                        mViewFlipper.showNext();
                    }
                }
            }
            ImageView lastImageView = (ImageView) mTipLinearLayout
                    .getChildAt(tmpIndex);
            ImageView imageView = (ImageView) mTipLinearLayout
                    .getChildAt(mCurrentIndex);
            imageView.setImageResource(onID);
            lastImageView.setImageResource(offID);
            break;
        default:
            break;
        }
        return true;
    }
}

 

本文链接:https://it72.com/7788.htm

推荐阅读
最新回复 (0)
返回