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; } }
自定义遥控器
最新推荐文章于 2021-08-03 19:00:54 发布