Android自定义View实现遥控器按钮
作者:z真真 发布时间:2021-12-27 09:50:47
标签:Android,遥控器,按钮
本文实例为大家分享了Android自定义View实现遥控器按钮的具体代码,供大家参考,具体内容如下
效果图:
原理:
onSizeChanged拿到控件宽高,进行path和region的计算(此处,path和region的坐标值都是以viewWidth/2,viewHeight/2为坐标原点进行计算的)
画布平移,绘制5个path
点击事件,判断是否处于相应的region区域内,进行控件的重绘
点击事件motionEvent的原始坐标(getX和getY),是以viewParent的左上角为坐标原点的,需要经过matrix转换成以控件中心点为原点的坐标体系。
Region区域,paint的style设置为stroke模式,遍历绘制
mPaint.setColor(Color.RED);
RegionIterator iterator = new RegionIterator(topRegion);
?Rect r = new Rect();
?while (iterator.next(r)) {
? ? ? canvas.drawRect(r, mPaint);
?}
源码:
public class RemoteControlMenu extends View {
? ? private int mWidth;
? ? private int mHeight;
? ? private RectF bigRectF;
? ? private int bigRadius;
? ? private RectF smallRectF;
? ? private int smallRadius;
? ? private int padding = 20;
? ? private int sweepAngel = 80;
? ? private int offsetAngel;
? ? @TouchArea
? ? private int mTouchArea = TouchArea.INVALID;
? ? private Paint mPaint;
? ? private Region topRegion, bottomRegion, leftRegion, rightRegion, centerRegion, globalRegion;
? ? private Path topPath, bottomPath, leftPath, rightPath, centerPath, selectedPath;
? ? Matrix mMapMatrix;
? ? private int unselectedColor = 0xff4c5165;
? ? private int selectedColor = 0xffdd9181;
? ? private boolean isSelected = false;
? ? public RemoteControlMenu(Context context) {
? ? ? ? this(context, null);
? ? }
? ? public RemoteControlMenu(Context context, @Nullable AttributeSet attrs) {
? ? ? ? this(context, attrs, 0);
? ? }
? ? public RemoteControlMenu(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
? ? ? ? super(context, attrs, defStyleAttr);
? ? ? ? mPaint = new Paint();
? ? ? ? mPaint.setAntiAlias(true);
? ? ? ? mPaint.setStyle(Paint.Style.FILL);
? ? ? ? mPaint.setStrokeWidth(4);
? ? ? ? mPaint.setColor(unselectedColor);
? ? ? ? offsetAngel = (360 - sweepAngel * 4) / 4;
? ? ? ? bigRectF = new RectF();
? ? ? ? smallRectF = new RectF();
? ? ? ? topRegion = new Region();
? ? ? ? bottomRegion = new Region();
? ? ? ? leftRegion = new Region();
? ? ? ? rightRegion = new Region();
? ? ? ? centerRegion = new Region();
? ? ? ? globalRegion = new Region();
? ? ? ? topPath = new Path();
? ? ? ? bottomPath = new Path();
? ? ? ? leftPath = new Path();
? ? ? ? rightPath = new Path();
? ? ? ? centerPath = new Path();
? ? ? ? mMapMatrix = new Matrix();
? ? }
? ? @Retention(RetentionPolicy.SOURCE)
? ? @IntDef({TouchArea.LEFT, TouchArea.TOP, TouchArea.RIGHT, TouchArea.BOTTOM,
? ? ? ? ? ? TouchArea.CENTER, TouchArea.INVALID})
? ? private @interface TouchArea {
? ? ? ? int LEFT = 1;
? ? ? ? int TOP = 2;
? ? ? ? int RIGHT = 3;
? ? ? ? int BOTTOM = 4;
? ? ? ? int CENTER = 5;
? ? ? ? int INVALID = 0;
? ? }
? ? @Override
? ? public boolean onTouchEvent(MotionEvent event) {
? ? ? ? float[] pts = new float[2];
? ? ? ? pts[0] = event.getX();
? ? ? ? pts[1] = event.getY();
? ? ? ? Log.d("zhen", "原始触摸位置:" + Arrays.toString(pts) + " mMapMatrix: " + mMapMatrix);
? ? ? ? mMapMatrix.mapPoints(pts);
? ? ? ? int x = (int) pts[0];
? ? ? ? int y = (int) pts[1];
? ? ? ? Log.w("zhen", "转换后的触摸位置:" + Arrays.toString(pts) + " mMapMatrix: " + mMapMatrix);
? ? ? ? int touchArea = TouchArea.INVALID;
? ? ? ? switch (event.getAction()) {
? ? ? ? ? ? case MotionEvent.ACTION_UP:
? ? ? ? ? ? ? ? if (leftRegion.contains(x, y)) {
? ? ? ? ? ? ? ? ? ? touchArea = TouchArea.LEFT;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (topRegion.contains(x, y)) {
? ? ? ? ? ? ? ? ? ? touchArea = TouchArea.TOP;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (rightRegion.contains(x, y)) {
? ? ? ? ? ? ? ? ? ? touchArea = TouchArea.RIGHT;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (bottomRegion.contains(x, y)) {
? ? ? ? ? ? ? ? ? ? touchArea = TouchArea.BOTTOM;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (centerRegion.contains(x, y)) {
? ? ? ? ? ? ? ? ? ? touchArea = TouchArea.CENTER;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (touchArea == TouchArea.INVALID) {
? ? ? ? ? ? ? ? ? ? mTouchArea = touchArea;
? ? ? ? ? ? ? ? ? ? Log.w("zhen", "点击outside");
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? if (mTouchArea == touchArea) {
? ? ? ? ? ? ? ? ? ? ? ? //取消选中
? ? ? ? ? ? ? ? ? ? ? ? isSelected = false;
? ? ? ? ? ? ? ? ? ? ? ? mTouchArea = TouchArea.INVALID;
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? //选中
? ? ? ? ? ? ? ? ? ? ? ? isSelected = true;
? ? ? ? ? ? ? ? ? ? ? ? mTouchArea = touchArea;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? Log.w("zhen", "按钮状态 mTouchArea " + mTouchArea + " isSelected: " + isSelected);
? ? ? ? ? ? ? ? ? ? if (mListener != null) {
? ? ? ? ? ? ? ? ? ? ? ? mListener.onMenuClicked(mTouchArea, isSelected);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? invalidate();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? return true;
? ? }
? ? @Override
? ? protected void onSizeChanged(int w, int h, int oldw, int oldh) {
? ? ? ? super.onSizeChanged(w, h, oldw, oldh);
? ? ? ? mWidth = w;
? ? ? ? mHeight = h;
? ? ? ? //大圆
? ? ? ? bigRadius = (Math.min(mWidth, mHeight) - 250) / 2;
? ? ? ? bigRectF.set(-bigRadius, -bigRadius, bigRadius, bigRadius);
? ? ? ? //小圆
? ? ? ? smallRadius = (bigRadius - padding) / 2;
? ? ? ? smallRectF.set(-smallRadius - padding, -smallRadius - padding,
? ? ? ? ? ? ? ? smallRadius + padding, smallRadius + padding);
? ? ? ? mMapMatrix.reset();
? ? ? ? globalRegion.set(-mWidth / 2, -mHeight / 2, mWidth / 2, mHeight / 2);
? ? ? ? centerPath.addCircle(0, 0, smallRadius, Path.Direction.CW);
? ? ? ? centerRegion.setPath(centerPath, globalRegion);
? ? ? ? float startAngel = -sweepAngel / 2f;
? ? ? ? rightPath.addArc(bigRectF, startAngel, sweepAngel + 4);
? ? ? ? startAngel += sweepAngel;
? ? ? ? rightPath.arcTo(smallRectF, startAngel, -sweepAngel);
? ? ? ? rightPath.close();
? ? ? ? rightRegion.setPath(rightPath, globalRegion);
? ? ? ? startAngel += offsetAngel;
? ? ? ? bottomPath.addArc(bigRectF, startAngel, sweepAngel + 4);
? ? ? ? startAngel += sweepAngel;
? ? ? ? bottomPath.arcTo(smallRectF, startAngel, -sweepAngel);
? ? ? ? bottomPath.close();
? ? ? ? bottomRegion.setPath(bottomPath, globalRegion);
? ? ? ? startAngel += offsetAngel;
? ? ? ? leftPath.addArc(bigRectF, startAngel, sweepAngel + 4);
? ? ? ? startAngel += sweepAngel;
? ? ? ? leftPath.arcTo(smallRectF, startAngel, -sweepAngel);
? ? ? ? leftPath.close();
? ? ? ? leftRegion.setPath(leftPath, globalRegion);
? ? ? ? startAngel += offsetAngel;
? ? ? ? topPath.addArc(bigRectF, startAngel, sweepAngel + 4);
? ? ? ? startAngel += sweepAngel;
? ? ? ? topPath.arcTo(smallRectF, startAngel, -sweepAngel);
? ? ? ? topPath.close();
? ? ? ? topRegion.setPath(topPath, globalRegion);
? ? ? ? Log.d("zhen", "globalRegion: " + globalRegion);
? ? ? ? Log.d("zhen", "globalRegion: " + globalRegion);
? ? ? ? Log.d("zhen", "leftRegion: " + leftRegion);
? ? ? ? Log.d("zhen", "topRegion: " + topRegion);
? ? ? ? Log.d("zhen", "rightRegion: " + rightRegion);
? ? ? ? Log.d("zhen", "bottomRegion: " + bottomRegion);
? ? ? ? Log.d("zhen", "centerRegion: " + centerRegion);
? ? }
? ? @Override
? ? protected void onDraw(Canvas canvas) {
? ? ? ? super.onDraw(canvas);
? ? ? ? canvas.translate(mWidth / 2, mHeight / 2);
? ? ? ? // 获取测量矩阵(逆矩阵)
? ? ? ? if (mMapMatrix.isIdentity()) {
? ? ? ? ? ? canvas.getMatrix().invert(mMapMatrix);
? ? ? ? }
? ? ? ? mPaint.setColor(unselectedColor);
? ? ? ? canvas.drawPath(centerPath, mPaint);
? ? ? ? canvas.drawPath(rightPath, mPaint);
? ? ? ? canvas.drawPath(bottomPath, mPaint);
? ? ? ? canvas.drawPath(leftPath, mPaint);
? ? ? ? canvas.drawPath(topPath, mPaint);
? ? ? ? if (!isSelected) return;
? ? ? ? mPaint.setColor(selectedColor);
? ? ? ? switch (mTouchArea) {
? ? ? ? ? ? case TouchArea.LEFT:
? ? ? ? ? ? ? ? canvas.drawPath(leftPath, mPaint);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case TouchArea.TOP:
? ? ? ? ? ? ? ? canvas.drawPath(topPath, mPaint);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case TouchArea.RIGHT:
? ? ? ? ? ? ? ? canvas.drawPath(rightPath, mPaint);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case TouchArea.BOTTOM:
? ? ? ? ? ? ? ? canvas.drawPath(bottomPath, mPaint);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case TouchArea.CENTER:
? ? ? ? ? ? ? ? canvas.drawPath(centerPath, mPaint);
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? Log.e("zhen", " touchArea: " + mTouchArea);
? ? ? ? //Android还提供了一个RegionIterator来对Region中的所有矩阵进行迭代,
? ? ? ? // 可以使用该类,获得某个Region的所有矩阵
? ? ? ? //通过遍历region中的矩阵,并绘制出来,来绘制region
// ? ? ? ?mPaint.setColor(Color.RED);
// ? ? ? ?RegionIterator iterator = new RegionIterator(topRegion);
// ? ? ? ?Rect r = new Rect();
// ? ? ? ?while (iterator.next(r)) {
// ? ? ? ? ? ?canvas.drawRect(r, mPaint);
// ? ? ? ?}
//
// ? ? ? ?mPaint.setColor(Color.BLUE);
// ? ? ? ?RegionIterator iterator1 = new RegionIterator(leftRegion);
// ? ? ? ?Rect r1 = new Rect();
// ? ? ? ?while (iterator1.next(r1)) {
// ? ? ? ? ? ?canvas.drawRect(r1, mPaint);
// ? ? ? ?}
//
// ? ? ? ?mPaint.setColor(Color.BLACK);
// ? ? ? ?RegionIterator iterator2 = new RegionIterator(rightRegion);
// ? ? ? ?Rect r2 = new Rect();
// ? ? ? ?while (iterator2.next(r2)) {
// ? ? ? ? ? ?canvas.drawRect(r2, mPaint);
// ? ? ? ?}
//
// ? ? ? ?mPaint.setColor(Color.YELLOW);
// ? ? ? ?RegionIterator iterator3 = new RegionIterator(bottomRegion);
// ? ? ? ?Rect r3 = new Rect();
// ? ? ? ?while (iterator3.next(r3)) {
// ? ? ? ? ? ?canvas.drawRect(r3, mPaint);
// ? ? ? ?}
//
// ? ? ? ?mPaint.setColor(Color.GREEN);
// ? ? ? ?RegionIterator iterator4 = new RegionIterator(centerRegion);
// ? ? ? ?Rect r4 = new Rect();
// ? ? ? ?while (iterator4.next(r4)) {
// ? ? ? ? ? ?canvas.drawRect(r4, mPaint);
// ? ? ? ?}
? ? }
? ? private MenuListener mListener;
? ? public void setListener(MenuListener listener) {
? ? ? ? mListener = listener;
? ? }
? ? // 点击事件 *
? ? public interface MenuListener {
? ? ? ? void onMenuClicked(int type, boolean isSelected);
? ? }
}
来源:https://blog.csdn.net/ZHENZHEN9310/article/details/95364272
0
投稿
猜你喜欢
- 简介该文档主要介绍以Nacos为配置中心,实现Spring Cloud GateWay 实现动态路由的功能。Spring Cloud Gat
- 本文帮助大家掌握Java多线程基础知识来对应日后碰到的问题,具体内容如下一、Java多线程面试问题1. 进程和线程之间有什么不同?一个进程是
- 一、Optional类的来源到目前为止,臭名昭著的空指针异常是导致Java应用程序失败的最常见原因。以前,为了解决空指针异常,Google公
- 目录IntroSampleWhat insideMoreReferenceIntroC# 9 中引入了 record,record 是一个特
- 关于注入数据说明1.不通过配置文件注入数据通过@Value将外部的值动态注入到Bean中,使用的情况有:注入普通字符串注入操作系统属性注入表
- 上一篇文章:Android 10 启动分析之Init篇 (一)在前文提到,init进程会在在Trigger 为init的Action中,启动
- 一、什么是SFTP?SFTP是一个安全文件传送协议,可以为传输文件提供一种安全的加密方法。SFTP 为 SSH的一部份,是一种传输文件到服务
- @Cacheable在同一类中方法调用无效上述图片中,同一个类中genLiveBullets()方法调用同类中的queryLiveByRoo
- 使用httpclient检测url状态及链接是否能打开有时候我们需要检测某个url返回的状态码是不是200或者页面能不能正常打开响应可使用如
- 我们知道,spring boot自动配置功能可以根据不同情况来决定spring配置应该用哪个,不应该用哪个,举个例子:Spring的Jdbc
- Android 自带的资源字符串资源:定义字符串资源在 <string.xml >;在JAVA 中 使用字符串资源,通过 get
- 话不多说,请看下面//C# 代码int year = DateTime.Now.Year;int month = DateTime.Now.
- 前言J.U.C是java包java.util.concurrent的简写,中文简称并发包,是jdk1.5新增用来编写并发相关的基础api。j
- 图形验证码属于老生常谈了,具体细节这里就不说了。生成图形验证码的办法非常多,今天讲解一种通过Kaptcha组件快速生成图形验证码的方法。Ka
- 前言之前我们提到了 CustomPaint er 的 Paint 可以使用渐变(GradientShader)来填充绘制的图形,本篇我们来介
- 蔡勒公式 蔡勒(Zeller)公式:是一个计算星期的公式。随便给一个日期,就能用这个公式推算出是星期几。蔡勒公式如下:W = [
- 前言随着网络技术的发展、计算机应用水平广泛提高,原来系统的时效性、数据的正确性、操作的方便性上都存在不足,已影响到系统的正常使用。经过考察比
- 本文实例讲述了Java实现的上传并压缩图片功能。分享给大家供大家参考,具体如下:先看效果:原图:1.33M处理后:27.4kb关键代码:pa
- C#WinForm程序设计之图片浏览器,这次我们一起做一个图片查看器,这个图片查看器的原始图如下:我们首先来介绍一下这个原始图的构成:左边上
- 1.设置url-pattern为*.do(最为常见的方式)只要你的请求url中包含配置的url-pattern,该url就可以到达Dispa