图片轮播使用的容器是ViewPager,文字轮播使用的是TextSwitcher。
图片轮播的主要思路:利用Timer和TimerTask构建定时任务;监听ViewPager的滑动,根据滑动百分比动态改变指示小圆点的宽度和水平位移,达到拉伸和平移效果;触发MotionEvent.ACTION_DOWN时,停止定时任务;触发MotionEvent.ACTION_UP时重启定时任务;触发MotionEvent.ACTION_MOVE计算位移量,判断点击事件触发条件;
package com.example.autoplayview;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import java.util.Timer;
import java.util.TimerTask;
public class AutoPlayView extends RelativeLayout {
private Context mContext;
private ViewPager mPager;
private LinearLayout dots;//计数小原点的布局
private RelativeLayout indicator;//指示小原点
private int width;//10dp(标准)
private int dotWidth;//指示小原点的宽度
private int dotDx;//指示小原点水平位移
private LayoutParams mParams;//指示小原点布局参数(改变参数制作动画)
private int current = 0;//当前页码
private boolean canPlay;//可否播放
private boolean isRunning;//是否正在播放;
private int pageSize=0;//轮播数量
private boolean isFirst = true;//第一次启动,为避免空指针问题,所以延迟0.2秒启动
private Timer timer;
private TimerTask timerTask;
private float x;//用于判断是否为水平滑动
private float y;//用于判断是否为竖直滑动
private boolean isMove;//滑动不触发点击事件
private long touchTime; //按下的时间
private long hold;//按住了多久
private ClickListener mListener;//点击事件
public AutoPlayView(Context context) {
this(context, null, 0);
}
public AutoPlayView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AutoPlayView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext = context;
width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10,
getResources().getDisplayMetrics());
dotWidth = width;
initView();
initListener();
}
private void initView(){
//图片容器ViewPager
mPager = new ViewPager(mContext);
RelativeLayout.LayoutParams lp01 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
this.addView(mPager, lp01);
//计数小圆点
dots = new LinearLayout(mContext);
dots.setId(R.id.auto_play_dots);//添加自定义id(res/value/ids)
RelativeLayout.LayoutParams lp02 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp02.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
lp02.setMargins(0, 0, 0, width);
lp02.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
this.addView(dots, lp02);
//指示小圆点
indicator = new RelativeLayout(mContext);
indicator.setBackgroundResource(R.drawable.dot_indicator);
RelativeLayout.LayoutParams lp03 = new LayoutParams(width, width);
lp03.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.auto_play_dots);//相对定位
lp03.addRule(RelativeLayout.ALIGN_LEFT, R.id.auto_play_dots);//相对定位
this.addView(indicator, lp03);
}
private void initListener() {
mPager.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
hold = System.currentTimeMillis() - touchTime;
startPlay();
if (hold < 800 && mListener != null && !isMove) {//按住小于0.8秒,点击事件不为空,不是滑动状态,则触发点击
mListener.onClick(current);
}
break;
case MotionEvent.ACTION_DOWN:
版权声明:本文为zpf0943084056原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。