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

Android檢測(cè)手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)的方法

 更新時(shí)間:2022年05月18日 10:38:24   作者:XHui_Lin  
這篇文章主要為大家詳細(xì)介紹了Android檢測(cè)手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android檢測(cè)手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)的具體代碼,供大家參考,具體內(nèi)容如下

多點(diǎn)觸摸點(diǎn)數(shù)效果圖

Circle.java

package com.zking.administrator.g160628_android19_pointstouch;
?
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
?
import java.util.Random;
?
/**
?* Created by Administrator on 2017/7/9.
?*/
?
public class Circle {
? ? public float x;
? ? public float y;
? ? public ?int r=100;//半徑
? ? public int id;//手指的名字
? ? //三原色(每一個(gè)int類型的取值都是255)
? ? int red;
? ? int green;
? ? int blue;
? ? //隨機(jī)數(shù)
? ? Random random=new Random();
?
? ? public Circle(float x, float y, int id) {
? ? ? ? this.x = x;
? ? ? ? this.y = y;
? ? ? ? this.id = id;
? ? ? ? red=random.nextInt(255);
? ? ? ? green=random.nextInt(255);
? ? ? ? blue=random.nextInt(255);
? ? }
?
? ? //畫自己
? ? public void drawSelf(Canvas canvas, Paint paint){
? ? ? ? //設(shè)置顏色隨機(jī)
? ? ? ? paint.setColor(Color.rgb(red,green,blue));
? ? ? ? canvas.drawCircle(x,y,r,paint);
? ? }
}

MainActivity.java

package com.zking.administrator.g160628_android19_pointstouch;
?
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
?
public class MainActivity extends AppCompatActivity {
?
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(new MyView(this));
? ? }
}

MyView.java

package com.zking.administrator.g160628_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 Administrator on 2017/7/9.
?*/
?
public class MyView extends View{
? ? //全局變量(單點(diǎn)觸摸)
// ? ?private float x;
// ? ?private float y;
// ? ?private int r=100;
?
? ? //定義圓的集合
? ? List<Circle> circles=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();
? ? ? ? //畫圓(單點(diǎn)觸摸)
// ? ? ? ?canvas.drawCircle(x,y,r,paint);
? ? ? ? for (Circle circle : circles) {
? ? ? ? ? ? //把畫布和畫筆傳進(jìn)去(運(yùn)行第一次進(jìn)入什么都沒有,所有默認(rèn)的圓也就沒了)
? ? ? ? ? ? circle.drawSelf(canvas,paint);
? ? ? ? }
? ? }
?
? ? @Override
? ? public boolean onTouchEvent(MotionEvent event) {
? ? ? ? ?//獲取手指的行為
? ? ? ? int action=event.getAction();
? ? ? ? int action_code=action&0xff;
? ? ? ? //手指的下標(biāo)Index
? ? ? ? int pointIndex=action>>8;//右移8
?
? ? ? ? //獲取手指的坐標(biāo)
? ? ? ? float x=event.getX(pointIndex);
? ? ? ? float y=event.getY(pointIndex);
? ? ? ? //獲取手指的名字(id)
? ? ? ? int id=event.getPointerId(pointIndex);
?
? ? ? ? if(action_code>=5){
? ? ? ? ? ? action_code-=5;
? ? ? ? }
?
? ? ? ? switch (action_code){//action_code單點(diǎn)觸摸是012,多點(diǎn)觸摸562
? ? ? ? ? ? //case 5://多點(diǎn)觸摸的按下時(shí)5
? ? ? ? ? ? case MotionEvent.ACTION_DOWN://0按下
? ? ? ? ? ? ? ? //實(shí)例化圓
? ? ? ? ? ? ? ? Circle circle=new Circle(x,y,id);
? ? ? ? ? ? ? ? //將圓添加到集合中
? ? ? ? ? ? ? ? circles.add(circle);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case MotionEvent.ACTION_UP://1抬起
? ? ? ? ? ? ? ? //調(diào)用拿圓的方法(拿到是哪個(gè)圓我集合就移除,然后重新繪制)
? ? ? ? ? ? ? ? circles.remove(get(id));
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case MotionEvent.ACTION_MOVE://2移動(dòng)
? ? ? ? ? ? ? ? //拿到所有手指的數(shù)量(循環(huán)所有的圓拿到他的ID,然后用現(xiàn)在的圓,給每一個(gè)圓的xy重新賦值)
? ? ? ? ? ? ? ? for (int i = 0; i <event.getPointerCount() ; i++) {
? ? ? ? ? ? ? ? ? ? //根據(jù)下標(biāo)拿到ID
? ? ? ? ? ? ? ? ? ? int did=event.getPointerId(i);
? ? ? ? ? ? ? ? ? ? //根據(jù)ID拿到新的圓(i就是當(dāng)前手指的下標(biāo),因?yàn)槲覀兪歉鶕?jù)下標(biāo)去拿xy)
? ? ? ? ? ? ? ? ? ? get(did).x=event.getX(i);
? ? ? ? ? ? ? ? ? ? get(did).y=event.getY(i);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? //重新調(diào)用onDraw 重繪 在主線程調(diào)用
? ? ? ? invalidate();
? ? ? ? return true;
? ? }
?
? ? //這個(gè)方法的目的就是拿圓
? ? public Circle get(int id){
? ? ? ? for (Circle circle : circles) {
? ? ? ? ? ? //判斷(拿到每一個(gè)圓的id等等于你傳過來的ID,如果是你想要得圓就return過去)
? ? ? ? ? ? if(circle.id==id){
? ? ? ? ? ? ? ? return circle;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return null;
? ? }
}

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

相關(guān)文章

最新評(píng)論