Android 一个简单的自定义WheelView实现

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);
    }
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值