Android onClick自定义视图

时间:2017-04-06 17:23:51

标签: android view onclick android-custom-view

我制作了一个自定义视图,它沿着圆形路径有12个圆圈。我想为每个圆圈实现一个onClick监听器。我如何在自定义视图中实现它?我还上传了自定义视图的代码。它只是扩展了一个视图。

enter image description here

public class WatchView extends View {

    private Paint mCircleTickPaint;
    private Paint mHourPaint;
    private Paint mMinutePaint;
    private Paint mSecondPaint;

    private boolean mShouldTick;
    private int mTickSize;
    private int mHourColor;
    private int mMinuteColor;
    private int mSecondColor;

    private int mSeconds = 0;
    private int mMinutes = 0;
    private int mHours = 0;
    private Handler handler = new Handler(Looper.getMainLooper());

    public WatchView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.watchface,
                0, 0);

        try {
            mShouldTick = a.getBoolean(R.styleable.watchface_shouldTick, true);
            mTickSize = a.getInt(R.styleable.watchface_tickSize, 40);
            mHourColor = a.getColor(R.styleable.watchface_hourColor, Color.RED);
            mMinuteColor = a.getColor(R.styleable.watchface_minuteColor, Color.GREEN);
            mSecondColor = a.getColor(R.styleable.watchface_secondColor, Color.BLUE);
        } finally {
            a.recycle();
        }

        init();
    }

    public void init() {
        mCircleTickPaint = new Paint();
        mCircleTickPaint.setAntiAlias(true);
        mCircleTickPaint.setColor(Color.WHITE);

        mHourPaint = new Paint();
        mHourPaint.setAntiAlias(true);
        mHourPaint.setColor(mHourColor);

        mMinutePaint = new Paint();
        mMinutePaint.setAntiAlias(true);
        mMinutePaint.setColor(mMinuteColor);

        mSecondPaint = new Paint();
        mSecondPaint.setAntiAlias(true);
        mSecondPaint.setColor(mSecondColor);

        long now = System.currentTimeMillis();
        SimpleDateFormat hh = new SimpleDateFormat("hh");
        SimpleDateFormat mm = new SimpleDateFormat("mm");
        SimpleDateFormat ss = new SimpleDateFormat("ss");

        int hours = Integer.parseInt(hh.format(new Date(now)));
        int minutes = Integer.parseInt(mm.format(new Date(now)));
        int seconds = Integer.parseInt(ss.format(new Date(now)));

        int mappedMinutes = (int) Utils.map(minutes, 1, 60, 1, 12);

        int reverseSeconds = (60 - seconds) % 12;
        int mappedSeconds = (int) Utils.map(reverseSeconds, 1, 60, 1, 12);

        this.mHours = hours;
        this.mMinutes = mappedMinutes;
        this.mSeconds = mappedSeconds;

        if (mShouldTick) {
            if (null != handler) {
                handler.removeCallbacks(mUpdateRunnable);
            }

            handler.post(mUpdateRunnable);
        }

        Log.d("WatchView", "Setting the time at: " + mHours + ":" + mMinutes + ":" + mSeconds);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int centerX = canvas.getWidth() / 2;
        int centerY = canvas.getHeight() / 2;

        float innerTickRadius = centerX / 2;
        for (int tickIndex = 0; tickIndex < 12; tickIndex++) {
            float tickRot = (float) (tickIndex * Math.PI * 2 / 12);

            float innerX = (float) Math.sin(tickRot) * innerTickRadius;
            float innerY = (float) -Math.cos(tickRot) * innerTickRadius;

            int reverseSeconds = (60 - mSeconds) % 12;
            int mappedSeconds = (int) Utils.map(reverseSeconds, 0, 11, 11, 0);

            if (mHours == tickIndex) {
                canvas.drawCircle(innerX + centerX, innerY + centerY, mTickSize, mHourPaint);
            } else if (mMinutes == tickIndex) {
                canvas.drawCircle(innerX + centerX, innerY + centerY, mTickSize, mMinutePaint);
            } else if (mappedSeconds == tickIndex) {
                canvas.drawCircle(innerX + centerX, innerY + centerY, mTickSize, mSecondPaint);
            } else {
                canvas.drawCircle(innerX + centerX, innerY + centerY, mTickSize, mCircleTickPaint);
            }
        }
    }

    private Runnable mUpdateRunnable = new Runnable() {
        @Override public void run() {
            mSeconds += 1;

            if (mSeconds > 59) {
                mSeconds = 0;
                mMinutes += 1;
            }

            if (mMinutes > 59) {
                mMinutes = 0;
                mHours += 1;
            }

            if (mHours > 11) {
                mHours = 0;
            }

            invalidate();
            handler.postDelayed(mUpdateRunnable, 1000);
        }
    };


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int intrinsicSize = ScreenUtils.getScreenWidth(getContext());

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int width;
        int height;

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else if (widthMode == MeasureSpec.AT_MOST) {
            width = Math.min(intrinsicSize, widthSize);
        } else {
            width = intrinsicSize;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else if (heightMode == MeasureSpec.AT_MOST) {
            height = Math.min(intrinsicSize, heightSize);
        } else {
            height = intrinsicSize;
        }

        int min = Math.min(width, height);
        setMeasuredDimension(min, min);
    }
}

0 个答案:

没有答案