<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了Android自定義View實現遙控器按鈕的具體程式碼,供大家參考,具體內容如下
效果圖:
原理:
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); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45