Android幻灯片切换效果

Home / Android MrLee 2016-3-14 4077

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://www.it72.com/7788.htm

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