2018/10/27 修改
效果图:
没有首尾连接,可以向上向下拉出,然后弹回
放手后有短暂的"回弹"的动画
代码:
package com.example.crazyflower.mywheelview;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
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 java.lang.ref.WeakReference;
import java.util.List;
/**
* Created by CrazyFlower on 2018/4/2.
*/
public class MyWheelView extends View {
private static final String TAG = "MyWheelView";
private static final String RESILIENCE_DISTANCE_OF_ONCE = "resilience_distance_of_once";
private static final String RESILIENCE_LEFT_TIMES = "left_times";
private static final int RESILIENCE_TIMES = 5;
private static final int RESILIENCE_TIME_INTERVAL = 50;
private List<String> data;
private int selectedItemIndex = 0;
private float lastY;
private float scrollY;
private int viewWidth;
private int viewHeight;
private float itemHeight;
private int itemNumber;
private int halfItemNumber;
private static final float maxScaleTextSizeToItemHeight = 0.9f;
private static final float minScaleTextSizeToItemHeight = 0.72f;
private float maxTextSize;
private float minTextSize;
private IWheelViewSelectedListener wheelViewSelectedListener;
Paint selectedLinePaint;
Paint selectedBackgroundPaint;
Paint normalTextPaint;
Paint selectedTextPaint;
private Handler handler;
public MyWheelView(Context context) {
this(context, null);
}
public MyWheelView(Context context, AttributeSet attrs) {
this(context, attrs, 1);
}
public MyWheelView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyWheelView);
initAttributesData(typedArray);
initDefaultData();
}
private void initAttributesData(TypedArray typedArray) {
Log.d(TAG, "initDataAndPaint: ");
itemNumber = typedArray.getInt(R.styleable.MyWheelView_item_number, 5);
halfItemNumber = itemNumber / 2;
selectedLinePaint = new Paint();
selectedBackgroundPaint = new Paint();
normalTextPaint = new Paint();
selectedTextPaint = new Paint();
selectedLinePaint.setColor(typedArray.getColor(R.styleable.MyWheelView_selected_line_color, Color.rgb(0, 0, 0)));
selectedBackgroundPaint.setColor(typedArray.getColor(R.styleable.MyWheelView_selected_background_color, Color.rgb(255, 255, 255)));
normalTextPaint.setColor(typedArray.getColor(R.styleable.MyWheelView_normal_text_color, Color.rgb(0, 0, 0)));
selectedTextPaint.setColor(typedArray.getColor(R.styleable.MyWheelView_selected_text_color, Color.rgb(0, 255, 204)));
selectedLinePaint.setStyle(Paint.Style.FILL_AND_STROKE);
selectedLinePaint.setStrokeWidth(4);
selectedBackgroundPaint.setStyle(Paint.Style.FILL);
}