android實現多點觸摸效果
本文實例為大傢分享瞭android實現多點觸摸效果的具體代碼,供大傢參考,具體內容如下
1.獲取點擊xy軸的下標,實現觸摸效果。
獲取XY畫一個圓並且自動從下變大,直到消失不見。
效果圖如下:
代碼如下:
1.寫一個實體類,用於存寫觸摸點擊的XY軸下表,並根據獲得的下標用半徑把圓畫出來,半徑默認為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.利用線程的調用改變圓半徑的大小
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();//回調 } catch (InterruptedException e) { e.printStackTrace(); } if(i==9){ circleList.remove(0); } } } } }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- Android自定義view貝塞爾曲線
- Android自定義view仿QQ的Tab按鈕動畫效果(示例代碼)
- Android開發之自定義UI組件詳解
- Android自定義View繪制貝塞爾曲線的方法
- Android顏色處理SweepGradient掃描及梯度渲染示例