自定义遥控器

CircleView 是一个自定义视图,实现了一个遥控器的界面效果。它根据屏幕宽度进行适配,通过触摸事件来模拟上下左右及中心按钮的点击,并通过 onDraw 方法绘制背景和按钮图像。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.yilian.app.demo.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ImageFormat;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

import com.yilian.app.demo.R;
import com.yilian.app.demo.Utils.BitmapUtils;

/**
 * 作者:${Jsc} on 2018/6/20 0020 16:47
 * 邮箱:jsc_job@163.com
 */
public class CircleView extends View {

    private Context mContext;

    private int mWidth;//真实宽度
    private int widthbg = 584;//图片背景宽度
    private int widthMid = 220;//中心宽度

    private int circle_X = 292;//圆心x    private int circle_Y = 292;//圆心y    private int bigRadius = 292;//外圆半径
    private int small_X = 182; //内圆X
    private int small_Y = 182;//内圆Y
    private int smallRadius = 110;//内圆半径
    private float scale = 1.0f;
    private boolean isInit = false;
    private String TAG = "CircleView";
    private int inputPress = -1;


    private Bitmap ok;
    private Bitmap ok_press;
    private Bitmap button;
    private Bitmap button_press;


    public CircleView(Context context) {
        super(context);
        mContext = context;
    }


    public CircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;

    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
    }

    private void initData() {
        mWidth = getWidth();
        scale = mWidth * 1.0f / widthbg;

        circle_X = (int) (circle_X * scale);
        circle_Y = (int) (circle_Y * scale);
        bigRadius = (int) (bigRadius * scale);
        small_X = (int) (small_X * scale);
        small_Y = (int) (small_Y * scale);
        smallRadius = (int) (smallRadius * scale);

        initView();
        isInit = true;
    }

    private void initView() {

        if (mWidth == widthbg) {
            ok = BitmapUtils.decodeCustomRes(mContext, R.mipmap.ok);
            ok_press = BitmapUtils.decodeCustomRes(mContext, R.mipmap.ok_press);
            button = BitmapUtils.decodeCustomRes(mContext, R.mipmap.button);
            button_press = BitmapUtils.decodeCustomRes(mContext, R.mipmap.button_press);
        } else {
            int mid = (int) (widthMid * scale);
            Bitmap bitmap = BitmapUtils.decodeCustomRes(mContext, R.mipmap.button);
            button = Bitmap.createScaledBitmap(bitmap, mWidth, mWidth, true);
            bitmap.recycle();
            bitmap = null;

            bitmap = BitmapUtils.decodeCustomRes(mContext, R.mipmap.button_press);
            button_press = Bitmap.createScaledBitmap(bitmap, mWidth, mWidth, true);
            bitmap.recycle();
            bitmap = null;

            bitmap = BitmapUtils.decodeCustomRes(mContext, R.mipmap.ok);
            ok = Bitmap.createScaledBitmap(bitmap, mid, mid, true);
            bitmap.recycle();
            bitmap = null;

            bitmap = BitmapUtils.decodeCustomRes(mContext, R.mipmap.ok_press);
            ok_press = Bitmap.createScaledBitmap(bitmap, mid, mid, true);
            bitmap.recycle();
            bitmap = null;


        }
        System.gc();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!isInit) {
            initData();
        }
        canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));

        drawCir(canvas);
    }

    private void drawCir(Canvas canvas) {
        switch (inputPress) {//view点击事件使用
            case -1:
                canvas.drawBitmap(button, 0, 0, null);
                canvas.drawBitmap(ok, small_X, small_Y, null);
                break;
            case KeyEvent.KEYCODE_DPAD_CENTER:
                canvas.drawBitmap(button, 0, 0, null);
                canvas.drawBitmap(ok_press, small_X, small_Y, null);
                break;
            case KeyEvent.KEYCODE_DPAD_UP:
                drawbg(canvas, 90);
                canvas.drawBitmap(ok, small_X, small_Y, null);
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:
                drawbg(canvas, 360);
                canvas.drawBitmap(ok, small_X, small_Y, null);
                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:
                drawbg(canvas, 0);
                canvas.drawBitmap(ok, small_X, small_Y, null);
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                drawbg(canvas, 180);
                canvas.drawBitmap(ok, small_X, small_Y, null);
                break;
            default:
                break;
        }


    }

    private void drawbg(Canvas canvas, int i) {
        canvas.save();
        canvas.rotate(i, circle_X, circle_Y);
        canvas.drawBitmap(button_press, 0, 0, null);
        canvas.restore();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {//view点击事件使用
            case MotionEvent.ACTION_DOWN:
                int X = (int) (event.getX() - circle_X);
                int Y = (int) (event.getY() - circle_Y);
                Log.e(TAG, "X=====: " + X);
                Log.e(TAG, "Y=====: " + Y);
                int X2 = X * X;
                int Y2 = Y * Y;

                int smallCircle = smallRadius * smallRadius;
                if (X2 + Y2 < smallCircle) {
                    setEventDown(KeyEvent.KEYCODE_DPAD_CENTER);
                } else if (Y > X) {
                    if (Y < -X) {
                        setEventDown(KeyEvent.KEYCODE_DPAD_LEFT);
                    } else {
                        setEventDown(KeyEvent.KEYCODE_DPAD_DOWN);
                    }
                } else if (X > Y) {
                    if (Y < -X) {
                        setEventDown(KeyEvent.KEYCODE_DPAD_UP);
                    } else {
                        setEventDown(KeyEvent.KEYCODE_DPAD_RIGHT);
                    }
                }
                postInvalidate();
                break;
            default:
                break;
        }
        return true;
    }

    private void setEventDown(int keycodeDpadCenter) {
        inputPress = keycodeDpadCenter;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值