android實(shí)現(xiàn)多點(diǎn)觸摸效果
本文實(shí)例為大家分享了android實(shí)現(xiàn)多點(diǎn)觸摸效果的具體代碼,供大家參考,具體內(nèi)容如下
1.獲取點(diǎn)擊xy軸的下標(biāo),實(shí)現(xiàn)觸摸效果。
獲取XY畫一個(gè)圓并且自動(dòng)從下變大,直到消失不見(jiàn)。
效果圖如下:

代碼如下:
1.寫一個(gè)實(shí)體類,用于存寫觸摸點(diǎn)擊的XY軸下表,并根據(jù)獲得的下標(biāo)用半徑把圓畫出來(lái),半徑默認(rèn)為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.利用線程的調(diào)用改變圓半徑的大小
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();
? ? ? ? //手指的下標(biāo)Index
? ? ? ? int pointIndex = action >> 8;
? ? ? ? int action_code= action&0xff;
? ? ? ? x = event.getX(pointIndex);//X軸
? ? ? ? y = event.getY(pointIndex);//Y軸
? ? ? ? indexid = event.getPointerId(pointIndex);//觸摸的下標(biāo)
? ? ? ?// 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://點(diǎn)擊
? ? ? ? ? ? ? ? i++;
? ? ? ? ? ? ? ? Circle circle=new Circle(x,y,0,indexid);//進(jìn)來(lái)一次就NEW一個(gè)對(duì)象
? ? ? ? ? ? ? ? circleList.add(circle);//加入集合
? ? ? ? ? ? ? ? new my().start();//啟動(dòng)線程
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case MotionEvent.ACTION_UP://移開(kāi)
? ? ? ? ? ? ? ? //circleList.remove(delete(indexid));
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case MotionEvent.ACTION_MOVE://移動(dòng)
? ? ? ? ? ? ? ?/* 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();//回調(diào)
? ? ? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if(i==9){
? ? ? ? ? ? ? ? ? ? circleList.remove(0);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解析Android開(kāi)發(fā)中多點(diǎn)觸摸的實(shí)現(xiàn)方法
- android 多點(diǎn)觸摸圖片縮放的具體實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)手勢(shì)滑動(dòng)多點(diǎn)觸摸放大縮小圖片效果
- Android實(shí)現(xiàn)手勢(shì)滑動(dòng)多點(diǎn)觸摸縮放平移圖片效果
- Android實(shí)現(xiàn)手勢(shì)滑動(dòng)多點(diǎn)觸摸縮放平移圖片效果(二)
- Android實(shí)現(xiàn)手機(jī)多點(diǎn)觸摸畫圓
- Android檢測(cè)手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)的方法
- Android實(shí)現(xiàn)檢測(cè)手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)
- Android實(shí)現(xiàn)多點(diǎn)觸摸操作
- android實(shí)現(xiàn)多點(diǎn)觸摸應(yīng)用
相關(guān)文章
Android ImageView 不顯示JPEG圖片的問(wèn)題解決
本篇文章主要介紹了Android ImageView 不顯示JPEG圖片及Android Studio中如何引用圖片資源的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-05-05
簡(jiǎn)單實(shí)現(xiàn)android輪播圖
這篇文章主要為大家詳細(xì)介紹了android輪播圖的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Kotlin實(shí)現(xiàn)在類里面創(chuàng)建main函數(shù)
這篇文章主要介紹了Kotlin實(shí)現(xiàn)在類里面創(chuàng)建main函數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
SafeList?in?Flutter?and?Dart小技巧
這篇文章主要為大家介紹了SafeList?in?Flutter?and?Dart小技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
android webvie指定視頻播放器播放網(wǎng)站視頻
android webview過(guò)濾調(diào)用第三方瀏覽器,并且解析視頻網(wǎng)站播放地址,使用指定播放器2013-11-11
Android使用OkHttp進(jìn)行網(wǎng)絡(luò)同步異步操作
這篇文章主要為大家詳細(xì)介紹了Android使用OkHttp進(jìn)行網(wǎng)絡(luò)同步異步操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android第三方HTTP網(wǎng)絡(luò)支持包OkHttp的基礎(chǔ)使用教程
在GitHub上開(kāi)源的安卓HTTP編程包OkHttp正在積累著越來(lái)越高的人氣,這里我們就來(lái)看一下這款A(yù)ndroid第三方HTTP網(wǎng)絡(luò)支持包OkHttp的基礎(chǔ)使用教程:2016-07-07
Android中backgroundDimEnabled的作用
這篇文章主要介紹了Android中backgroundDimEnabled的作用的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10

