欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)手機多點觸摸畫圓

 更新時間:2022年05月18日 10:54:41   作者:宋崢清  
這篇文章主要為大家詳細介紹了Android實現(xiàn)手機多點觸摸畫圓,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android實現(xiàn)手機多點觸摸畫圓的具體代碼,供大家參考,具體內(nèi)容如下

靜態(tài)效果圖:(多個手指按下和抬起的狀態(tài))

代碼實現(xiàn)部分:

1、先寫個實體類,設(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、然后我們自己再寫個java類,用來畫圓的

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;
? ? ? ? //手指的下標
? ? ? ? int pointIndex=action>>8;
?
?
? ? ? ? //獲取手指的坐標
? ? ? ? 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:
? ? ? ? ? ? ? ? //實例化園
? ? ? ? ? ? ? ? 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、最后我們在activity中改一句代碼就可以了

setContentView(new MyView(this));

最后打開真機測試就可以啦!

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論