自定义view package com.android.joyfultest.rollingselect; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.Paint.Align; import android.graphics.Paint.FontMetricsInt; import android.graphics.Paint.Style; import android.graphics.drawable.Drawable; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; import com.android.joyfultest.R; import com.android.joyfultest.utils.SizeConvertUtil; import java.util.ArrayList; import java.util.List; import java.util.Timer; import java.util.TimerTask; /** * 滚轮选择器 * <p/> */ public class WheelView extends View { public static final String TAG = "WheelView"; /** * 自动回滚到中间的速度 */ public static final float SPEED = 2; /** * 除选中item外,上下各需要显示的备选项数目 */ public static final int SHOW_SIZE = 1; private Context context; private List<String> itemList; private int itemCount; /** * item高度 */ private int itemHeight = 50; /** * 选中的位置,这个位置是mDataList的中心位置,一直不变 */ private int currentItem; private Paint selectPaint; private Paint mPaint; // 画背景图中单独的画笔 private Paint centerLinePaint; private float centerY; private float centerX; private float mLastDownY; /** * 滑动的距离 */ private float mMoveLen = 0; private boolean isInit = false; private SelectListener mSelectListener; private Timer timer; private MyTimerTask mTask; Handler updateHandler = new Handler() { @Override public void handleMessage(Message msg) { if (Math.abs(mMoveLen) < SPEED) { // 如果偏移量少于最少偏移量 mMoveLen = 0; if (null != timer) { timer.cancel(); timer.purge(); timer = null; } if (mTask != null) { mTask.cancel(); mTask = null; performSelect(); } } else { // 这里mMoveLen / Math.abs(mMoveLen)是为了保有mMoveLen的正负号,以实现上滚或下滚 mMoveLen = mMoveLen - mMoveLen / Math.abs(mMoveLen) * SPEED; } invalidate(); } }; public WheelView(Context context) { super(context); init(context); } public WheelView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public void setOnSelectListener(SelectListener listener) { mSelectListener = listener; } public void setWheelStyle(int style) { itemList = WheelStyle.getItemList(context, style); if (itemList != null) { itemCount = itemList.size(); resetCurrentSelect(); invalidate(); } else { Log.i(TAG, "item is null"); } } public void setWheelItemList(List<String> itemList) { this.itemList = itemList; if (itemList != null) { itemCount = itemList.size(); resetCurrentSelect(); } else { Log.i(
android 简单的滚动选择器
最新推荐文章于 2024-05-29 09:36:24 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)