Android實(shí)現(xiàn)手機(jī)多點(diǎn)觸摸畫(huà)圓
本文實(shí)例為大家分享了Android實(shí)現(xiàn)手機(jī)多點(diǎn)觸摸畫(huà)圓的具體代碼,供大家參考,具體內(nèi)容如下
靜態(tài)效果圖:(多個(gè)手指按下和抬起的狀態(tài))
代碼實(shí)現(xiàn)部分:
1、先寫(xiě)個(gè)實(shí)體類(lèi),設(shè)置相關(guān)的屬性
package com.zking.laci.android19_pointstouch; ? import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; ? import java.util.Random; ? /** ?* Created by Laci on 2017/7/9. ?*/ ? public class MyCircle { ? ? public float x; ? ? public float y; ? ? public int r=100; ? ? public int pointId; ? ? int red; ? ? int green; ? ? int blue; ? ? Random random=new Random(); ? ? ? public MyCircle(float x, float y, int pointId) { ? ? ? ? this.x = x; ? ? ? ? this.y = y; ? ? ? ? this.pointId = pointId; ? ? ? ? red=random.nextInt(255); ? ? ? ? green=random.nextInt(255); ? ? ? ? blue=random.nextInt(255); ? ? } ? ? public void drawSelf(Canvas canvas, Paint paint){ ? ? ? ? paint.setStyle(Paint.Style.STROKE); ? ? ? ? paint.setColor(Color.rgb(red,green,blue)); ? ? ? ? canvas.drawCircle(x,y,r,paint); ? ? } }
2、然后我們自己再寫(xiě)個(gè)java類(lèi),用來(lái)畫(huà)圓的
package com.zking.laci.android19_pointstouch; ? import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; ? import java.util.ArrayList; import java.util.List; ? /** ?* Created by Laci on 2017/7/9. ?*/ ? public class MyView extends View { ? ? ? List<MyCircle> lt=new ArrayList<>(); ? ? ? ? ? public MyView(Context context) { ? ? ? ? super(context); ? ? } ? ? ? public MyView(Context context, @Nullable AttributeSet attrs) { ? ? ? ? super(context, attrs); ? ? } ? ? ? public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { ? ? ? ? super(context, attrs, defStyleAttr); ? ? } ? ? ? public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { ? ? ? ? super(context, attrs, defStyleAttr, defStyleRes); ? ? } ? ? ? @Override ? ? protected void onDraw(Canvas canvas) { ? ? ? ? super.onDraw(canvas); ? ? ? ? Paint paint=new Paint(); ? ? ? ? for (MyCircle myCircle : lt) { ? ? ? ? ? ? myCircle.drawSelf(canvas,paint); ? ? ? ? } ? ? ? } ? ? ? @Override ? ? public boolean onTouchEvent(MotionEvent event) { ? ? ? ? //獲取手指的行為 ? ? ? ? int action=event.getAction(); ? ? ? ? int action_code=action&0xff; ? ? ? ? //手指的下標(biāo) ? ? ? ? int pointIndex=action>>8; ? ? ? ? ? ? //獲取手指的坐標(biāo) ? ? ? ? float x=event.getX(pointIndex); ? ? ? ? float y=event.getY(pointIndex); ? ? ? ? //獲取手指的名字的ID ? ? ? ? int pointId=event.getPointerId(pointIndex); ? ? ? ? if(action_code>=5){ ? ? ? ? ? ? action_code-=5; ? ? ? ? } ? ? ? ? ? switch (action_code) { ? ? ? ? ? ? case MotionEvent.ACTION_DOWN: ? ? ? ? ? ? ? ? //實(shí)例化園 ? ? ? ? ? ? ? ? MyCircle myCircle=new MyCircle(x,y,pointId); ? ? ? ? ? ? ? ? //將園添加到集合中 ? ? ? ? ? ? ? ? lt.add(myCircle); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_UP: ? ? ? ? ? ? ? ? lt.remove(get(pointId)); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_MOVE: ? ? ? ? ? ? ? ? for (int i = 0; i <event.getPointerCount() ; i++) { ? ? ? ? ? ? ? ? ? ? int id=event.getPointerId(i); ? ? ? ? ? ? ? ? ? ? get(id).x=event.getX(i); ? ? ? ? ? ? ? ? ? ? get(id).y=event.getY(i); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? ? //重新調(diào)用onDraw 重繪 ? ? ? ? invalidate(); ? ? ? ? return true; ? ? } ? ? ? public MyCircle get(int pointId){ ? ? ? ? for (MyCircle myCircle : lt) { ? ? ? ? ? ? if(myCircle.pointId==pointId){ ? ? ? ? ? ? ? ? return myCircle; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return null; ? ? } ? }
3、最后我們?cè)赼ctivity中改一句代碼就可以了
setContentView(new MyView(this));
最后打開(kāi)真機(jī)測(cè)試就可以啦!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解析Android開(kāi)發(fā)中多點(diǎn)觸摸的實(shí)現(xiàn)方法
- android 多點(diǎn)觸摸圖片縮放的具體實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)手勢(shì)滑動(dòng)多點(diǎn)觸摸放大縮小圖片效果
- Android實(shí)現(xiàn)手勢(shì)滑動(dòng)多點(diǎn)觸摸縮放平移圖片效果
- Android實(shí)現(xiàn)手勢(shì)滑動(dòng)多點(diǎn)觸摸縮放平移圖片效果(二)
- Android檢測(cè)手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)的方法
- Android實(shí)現(xiàn)檢測(cè)手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)
- android實(shí)現(xiàn)多點(diǎn)觸摸效果
- Android實(shí)現(xiàn)多點(diǎn)觸摸操作
- android實(shí)現(xiàn)多點(diǎn)觸摸應(yīng)用
相關(guān)文章
Android中Glide加載圓形圖片和圓角圖片實(shí)例代碼
本篇文章主要介紹了Android中Glide加載圓形圖片和圓角圖片實(shí)例代碼,具體一定的參考價(jià)值,有興趣的可以了解一下2017-05-05一個(gè)簡(jiǎn)單的Android圓弧刷新動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了一個(gè)簡(jiǎn)單的Android圓弧刷新動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Android中RecyclerView點(diǎn)擊Item設(shè)置事件
這篇文章主要介紹了Android中RecyclerView點(diǎn)擊Item設(shè)置事件的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07Android逐幀動(dòng)畫(huà)實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android逐幀動(dòng)畫(huà)實(shí)現(xiàn)代碼,可以通過(guò)xml或java代碼實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android UI控件之ImageSwitcher實(shí)現(xiàn)圖片切換效果
這篇文章主要為大家詳細(xì)介紹了Android UI控件之ImageSwitcher實(shí)現(xiàn)圖片切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android 實(shí)現(xiàn)滑動(dòng)方法總結(jié)
這篇文章主要介紹了Android 實(shí)現(xiàn)滑動(dòng)方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-07-07Android通過(guò)BLE傳輸文件遇到問(wèn)題解決
這篇文章主要為大家介紹了Android通過(guò)BLE傳輸文件遇到問(wèn)題解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04