亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android實(shí)現(xiàn)手機(jī)多點(diǎn)觸摸畫(huà)圓

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

本文實(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論