您好,登錄后才能下訂單哦!
自制一個簡單的WheelView控件,配合Dialog樣式的Activity使用
為什么不直接用Dialog呢?
因為感覺Dialog有時候傳值會很麻煩,而且寫起來也不方便。用Activity仿的話就很方便,效果也不錯。
當然用Dialog也是可以的,具體根據項目需求和個人習慣而定。
效果圖如下:
那么直接貼核心代碼,源碼我會在博客底部發(fā)鏈接
Dialog樣式的Activity布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="80dp"> <TextView android:id="@+id/rate_tv" android:layout_width="fill_parent" android:layout_height="45dp" android:background="@drawable/more_top_nor" android:gravity="center" android:text="選項" android:drawableBottom="@drawable/cell_cut_line" android:textColor="@android:color/black" android:textSize="18sp" /> <com.lg.wheelviewdemo.PickerView android:id="@+id/pickerview" android:layout_below="@id/rate_tv" android:background="@drawable/more_content_nor" android:layout_width="match_parent" android:layout_height="150dp" /> <TextView android:onClick="onClick" android:layout_width="fill_parent" android:layout_height="45dp" android:drawableTop="@drawable/cell_cut_line" android:layout_below="@id/pickerview" android:text="確定" android:textColor="@android:color/black" android:textSize="18sp" android:gravity="center" android:background="@drawable/more_bottom_nor"/> </RelativeLayout>
PickerView也就是自制的WheelView,代碼如下:
/** * 滾動選擇器 */ public class PickerView extends View { public static final String TAG = "PickerView"; /** * text之間間距和minTextSize之比 */ public static final float MARGIN_ALPHA = 2.3f; /** * 自動回滾到中間的速度 */ public static final float SPEED = 2; private List<String> mDataList; /** * 選中的位置,這個位置是mDataList的中心位置,一直不變 */ private int mCurrentSelected; private Paint mPaint; private float mMaxTextSize = 30; private float mMinTextSize = 15; private float mMaxTextAlpha = 255; private float mMinTextAlpha = 100; private int mViewHeight; private int mViewWidth; private float mLastDownY; /** * 滑動的距離 */ private float mMoveLen = 0; private boolean isInit = false; private onSelectListener 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 (mTask != null) { mTask.cancel(); mTask = null; performSelect(); } } else // 這里mMoveLen / Math.abs(mMoveLen)是為了保有mMoveLen的正負號,以實現(xiàn)上滾或下滾 mMoveLen = mMoveLen - mMoveLen / Math.abs(mMoveLen) * SPEED; invalidate(); } }; public PickerView(Context context) { super(context); init(); } public PickerView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public void setOnSelectListener(onSelectListener listener) { mSelectListener = listener; } private void performSelect() { if (mSelectListener != null) mSelectListener.onSelect(mDataList.get(mCurrentSelected)); } public void setData(List<String> datas) { mDataList = datas; mCurrentSelected = datas.size() / 2; invalidate(); } /** * 選擇選中的item的index * * @param selected */ public void setSelected(int selected) { mCurrentSelected = selected; int distance = mDataList.size() / 2 - mCurrentSelected; if (distance < 0) for (int i = 0; i < -distance; i++) { moveHeadToTail(); mCurrentSelected--; } else if (distance > 0) for (int i = 0; i < distance; i++) { moveTailToHead(); mCurrentSelected++; } invalidate(); } /** * 選擇選中的內容 * * @param mSelectItem */ public void setSelected(String mSelectItem) { for (int i = 0; i < mDataList.size(); i++) if (mDataList.get(i).equals(mSelectItem)) { setSelected(i); break; } } private void moveHeadToTail() { String head = mDataList.get(0); mDataList.remove(0); mDataList.add(head); } private void moveTailToHead() { String tail = mDataList.get(mDataList.size() - 1); mDataList.remove(mDataList.size() - 1); mDataList.add(0, tail); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mViewHeight = getMeasuredHeight(); mViewWidth = getMeasuredWidth(); // 按照View的高度計算字體大小 mMaxTextSize = mViewHeight / 4.0f; mMinTextSize = mMaxTextSize / 2f; isInit = true; invalidate(); } private void init() { timer = new Timer(); mDataList = new ArrayList<String>(); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setStyle(Style.FILL); mPaint.setTextAlign(Align.CENTER); mPaint.setColor(Color.WHITE); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 根據index繪制view if (isInit) drawData(canvas); } private void drawData(Canvas canvas) { // 先繪制選中的text再往上往下繪制其余的text float scale = parabola(mViewHeight / 4.0f, mMoveLen); float size = (mMaxTextSize - mMinTextSize) * scale + mMinTextSize; mPaint.setTextSize(size); mPaint.setColor(this.getResources().getColor(android.R.color.black)); mPaint.setAlpha((int) ((mMaxTextAlpha - mMinTextAlpha) * scale + mMinTextAlpha)); // text居中繪制,注意baseline的計算才能達到居中,y值是text中心坐標 float x = (float) (mViewWidth / 2.0); float y = (float) (mViewHeight / 2.0 + mMoveLen); FontMetricsInt fmi = mPaint.getFontMetricsInt(); float baseline = (float) (y - (fmi.bottom / 2.0 + fmi.top / 2.0)); canvas.drawText(mDataList.get(mCurrentSelected), x, baseline, mPaint); // 繪制上方data for (int i = 1; (mCurrentSelected - i) >= 0; i++) { drawOtherText(canvas, i, -1); } // 繪制下方data for (int i = 1; (mCurrentSelected + i) < mDataList.size(); i++) { drawOtherText(canvas, i, 1); } } /** * @param canvas * @param position 距離mCurrentSelected的差值 * @param type 1表示向下繪制,-1表示向上繪制 */ private void drawOtherText(Canvas canvas, int position, int type) { float d = (float) (MARGIN_ALPHA * mMinTextSize * position + type * mMoveLen); float scale = parabola(mViewHeight / 4.0f, d); float size = (mMaxTextSize - mMinTextSize) * scale + mMinTextSize; mPaint.setTextSize(size); mPaint.setAlpha((int) ((mMaxTextAlpha - mMinTextAlpha) * scale + mMinTextAlpha)); float y = (float) (mViewHeight / 2.0 + type * d); FontMetricsInt fmi = mPaint.getFontMetricsInt(); float baseline = (float) (y - (fmi.bottom / 2.0 + fmi.top / 2.0)); canvas.drawText(mDataList.get(mCurrentSelected + type * position), (float) (mViewWidth / 2.0), baseline, mPaint); } /** * 拋物線 * * @param zero 零點坐標 * @param x 偏移量 * @return scale */ private float parabola(float zero, float x) { float f = (float) (1 - Math.pow(x / zero, 2)); return f < 0 ? 0 : f; } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: doDown(event); break; case MotionEvent.ACTION_MOVE: doMove(event); break; case MotionEvent.ACTION_UP: doUp(event); break; } return true; } private void doDown(MotionEvent event) { if (mTask != null) { mTask.cancel(); mTask = null; } mLastDownY = event.getY(); } private void doMove(MotionEvent event) { mMoveLen += (event.getY() - mLastDownY); if (mMoveLen > MARGIN_ALPHA * mMinTextSize / 2) { // 往下滑超過離開距離 moveTailToHead(); mMoveLen = mMoveLen - MARGIN_ALPHA * mMinTextSize; } else if (mMoveLen < -MARGIN_ALPHA * mMinTextSize / 2) { // 往上滑超過離開距離 moveHeadToTail(); mMoveLen = mMoveLen + MARGIN_ALPHA * mMinTextSize; } mLastDownY = event.getY(); invalidate(); } private void doUp(MotionEvent event) { // 抬起手后mCurrentSelected的位置由當前位置move到中間選中位置 if (Math.abs(mMoveLen) < 0.0001) { mMoveLen = 0; return; } if (mTask != null) { mTask.cancel(); mTask = null; } mTask = new MyTimerTask(updateHandler); timer.schedule(mTask, 0, 10); } class MyTimerTask extends TimerTask { Handler handler; public MyTimerTask(Handler handler) { this.handler = handler; } @Override public void run() { handler.sendMessage(handler.obtainMessage()); } } public interface onSelectListener { void onSelect(String text); }
Dialog樣式Activity:
private PickerView pickerView; private List<String> mDatas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_wheel); pickerView = (PickerView) findViewById(R.id.pickerview); mDatas=new ArrayList<>(); for (int i = 1; i < 60; i++) { mDatas.add(""+i); } pickerView.setData(mDatas); pickerView.setSelected(MainActivity.num-1); pickerView.setOnSelectListener(new PickerView.onSelectListener() { @Override public void onSelect(String text) { MainActivity.num=Integer.parseInt(text); } }); } public void onClick(View view){ MainActivity.tv.setText("當前選擇 "+MainActivity.num); finish(); }
接下來要給Activity換上Dialog樣式
在styles.xml中加上
<style name="MyDialogStyle" parent="Theme.AppCompat.Dialog"> <item name="android:windowFrame">@null</item> <item name="windowNoTitle">true</item> <!-- 邊框 --> <item name="android:windowIsFloating">true</item> <!-- 是否浮現(xiàn)在activity之上 --> <item name="android:windowIsTranslucent">true</item> <!-- 半透明 --> <item name="android:windowNoTitle">true</item> <!-- 無標題 --> <item name="android:windowBackground">@android:color/transparent</item> <!-- 背景透明 --> <item name="android:backgroundDimEnabled">true</item> <!-- 模糊 --> </style>
AndroidManifest.xml中:
<activity android:name=".WheelActivity" android:theme="@style/MyDialogStyle"/>
源碼地址:http://down.51cto.com/data/2223518
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。