android實現(xiàn)多點觸摸效果
本文實例為大家分享了android實現(xiàn)多點觸摸效果的具體代碼,供大家參考,具體內(nèi)容如下
1.獲取點擊xy軸的下標,實現(xiàn)觸摸效果。
獲取XY畫一個圓并且自動從下變大,直到消失不見。
效果圖如下:
代碼如下:
1.寫一個實體類,用于存寫觸摸點擊的XY軸下表,并根據(jù)獲得的下標用半徑把圓畫出來,半徑默認為0
package com.example.android_pointstouch; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Handler; import android.os.Message; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.View; import java.util.Random; /** ?* Created by Administrator on 2017/7/9 0009. ?*/ public class Circle { ? ? public ?float x; ? ? public ?float y; ? ? public ?int r; ? ? public int pointId; ? ? private int red; ? ? private int green; ? ? private int blue; ? ? Random random=new Random(); ? ? public ?Circle(float x,float y,int r,int pointId){ ? ? ? ? this.x=x; ? ? ? ? this.y=y; ? ? ? ? this.r=r; ? ? ? ? this.pointId=pointId; ? ? ? ? this.red=random.nextInt(255); ? ? ? ? this.green=random.nextInt(255); ? ? ? ? this.blue=random.nextInt(255); ? ? } ? ? //畫圓 ? ? public void drawSelf(Canvas canvas, Paint paint){ ? ? ? ? paint.setColor(Color.rgb(red,green,blue));//顏色 ? ? ? ? canvas.drawCircle(x,y,r,paint); ? ? ? ? paint.setStyle(Paint.Style.STROKE); ? ? } }
2.利用線程的調(diào)用改變圓半徑的大小
package com.example.android_pointstouch; import android.content.Context; import android.content.Intent; import android.graphics.Canvas; import android.graphics.Paint; import android.os.Handler; import android.os.Message; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** ?* Created by Administrator on 2017/7/9 0009. ?*/ public class MyButton extends View { ? ? ? ? private List<Circle> circleList=new ArrayList<>(); ? ? private float x; ? ? private float y; ? ? private int indexid; ? ? int i=0; ? ? public MyButton(Context context) { ? ? ? ? super(context); ? ? } ? ? public MyButton(Context context, @Nullable AttributeSet attrs) { ? ? ? ? super(context, attrs); ? ? } ? ? public MyButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { ? ? ? ? super(context, attrs, defStyleAttr); ? ? } ? ? public MyButton(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(); ? ? ? ? paint.setStyle(Paint.Style.STROKE); ? ? ? ? for (Circle circle : circleList) { ? ? ? ? ? ? circle.r+=10; ? ? ? ? ? ? circle.drawSelf(canvas,paint); ? ? ? ? } ? ? } ? ? //觸摸 ? ? @Override ? ? public boolean onTouchEvent(MotionEvent event) { ? ? ? ? int action = event.getAction(); ? ? ? ? //手指的下標Index ? ? ? ? int pointIndex = action >> 8; ? ? ? ? int action_code= action&0xff; ? ? ? ? x = event.getX(pointIndex);//X軸 ? ? ? ? y = event.getY(pointIndex);//Y軸 ? ? ? ? indexid = event.getPointerId(pointIndex);//觸摸的下標 ? ? ? ?// Log.i("text","pointIndex="+pointIndex+"action_code="+action_code+"indexid="+indexid); ? ? ? ? if(action_code>=5){ ? ? ? ? ? ? action_code-=5; ? ? ? ? } ? ? ? ? switch (action_code){ ? ? ? ? ? ? case MotionEvent.ACTION_DOWN://點擊 ? ? ? ? ? ? ? ? i++; ? ? ? ? ? ? ? ? Circle circle=new Circle(x,y,0,indexid);//進來一次就NEW一個對象 ? ? ? ? ? ? ? ? circleList.add(circle);//加入集合 ? ? ? ? ? ? ? ? new my().start();//啟動線程 ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_UP://移開 ? ? ? ? ? ? ? ? //circleList.remove(delete(indexid)); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_MOVE://移動 ? ? ? ? ? ? ? ?/* for (int i = 0; i <event.getPointerCount() ; i++) { ? ? ? ? ? ? ? ? ? ?int id= event.getPointerId(i); ? ? ? ? ? ? ? ? ? ? delete(id).x=event.getX(i); ? ? ? ? ? ? ? ? ? ? delete(id).y=event.getY(i); ? ? ? ? ? ? ? ? }*/ ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? invalidate(); ? ? ? ? return true; ? ? } ? ? //移除 ? ? public Circle delete(int indexid){ ? ? ? ? for (Circle circle : circleList) { ? ? ? ? ? ? if(circle.pointId==indexid){ ? ? ? ? ? ? ? ? return circle; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return null; ? ? } ? ? //線程 改變圓大小 ? ? @Override ? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { ? ? ? ? super.onMeasure(widthMeasureSpec, heightMeasureSpec); ? ? } ? ? class my extends Thread{ ? ? ? ? @Override ? ? ? ? public void run() { ? ? ? ? ? ? for (int i = 0; i <10; i++) { ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? sleep(110); ? ? ? ? ? ? ? ? ? ? postInvalidate();//回調(diào) ? ? ? ? ? ? ? ? } catch (InterruptedException e) { ? ? ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if(i==9){ ? ? ? ? ? ? ? ? ? ? circleList.remove(0); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android ImageView 不顯示JPEG圖片的問題解決
本篇文章主要介紹了Android ImageView 不顯示JPEG圖片及Android Studio中如何引用圖片資源的相關(guān)知識,具有很好的參考價值。下面跟著小編一起來看下吧2017-05-05Kotlin實現(xiàn)在類里面創(chuàng)建main函數(shù)
這篇文章主要介紹了Kotlin實現(xiàn)在類里面創(chuàng)建main函數(shù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03SafeList?in?Flutter?and?Dart小技巧
這篇文章主要為大家介紹了SafeList?in?Flutter?and?Dart小技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12android webvie指定視頻播放器播放網(wǎng)站視頻
android webview過濾調(diào)用第三方瀏覽器,并且解析視頻網(wǎng)站播放地址,使用指定播放器2013-11-11Android使用OkHttp進行網(wǎng)絡同步異步操作
這篇文章主要為大家詳細介紹了Android使用OkHttp進行網(wǎng)絡同步異步操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07Android第三方HTTP網(wǎng)絡支持包OkHttp的基礎使用教程
在GitHub上開源的安卓HTTP編程包OkHttp正在積累著越來越高的人氣,這里我們就來看一下這款Android第三方HTTP網(wǎng)絡支持包OkHttp的基礎使用教程:2016-07-07Android中backgroundDimEnabled的作用
這篇文章主要介紹了Android中backgroundDimEnabled的作用的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10