網站首頁 編程語言 正文
本文實例為大家分享了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);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
原文鏈接:https://blog.csdn.net/newparallel/article/details/74906297
相關推薦
- 2022-11-04 解析Android?Jetpack簡介_Android
- 2023-07-06 mac配置idea自帶的maven3環境變量
- 2022-12-24 Docker中redis集群部署實戰_docker
- 2022-07-02 python列表:開始、結束、步長值實例_python
- 2022-07-29 pytest?fixtures函數及測試函數的參數化解讀_python
- 2022-12-29 Android?Activity狀態與操作探究_Android
- 2022-07-26 Fatal error in launcher: Unable to create process
- 2022-04-01 docker registry私庫鏡像查看與刪除
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支