多点触摸

多点触摸



安卓多点触摸画圆,手指抬起圆消失

先写一个圆的实体类

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
           
           
import android.graphics.Canvas ;
import android.graphics.Color ;
import android.graphics.Paint ;
import java.util.Random ;
public class Circle {
public float x ;
public float y ;
public int r = 100 ;
public int pointId ;
int red ;
int green ;
int blue ;
Random random = new Random ();
public Circle ( 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 . setColor ( Color . rgb ( red , green , blue ));
canvas . drawCircle ( x , y , r , paint );
}
}
 来自CODE的代码片
android19Circle.java


再来一个类,继承View 重写它的四个方法

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
           
           
import android.content.Context ;
import android.graphics.Canvas ;
import android.graphics.Paint ;
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.List ;
public class MyView extends View {
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 ();
for ( Circle circle : circles ) {
circle . drawSelf ( canvas , paint );
}
}
@Override
public boolean onTouchEvent ( MotionEvent event ) {
//获取手指的行为
int action = event . getAction ();
int action_code = action & 0xff ;
//手指的下标Index
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 5:
case MotionEvent . ACTION_DOWN :
//实例化圆
Circle circle = new Circle ( x , y , pointId );
//将圆添加到集合中
circles . add ( circle );
break ;
case MotionEvent . ACTION_UP :
circles . 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 ;
}
//重新调用onDraw 重绘
invalidate ();
//重新绘制 子线程
// postInvalidate();
return true ;
}
public Circle get ( int pointId ){
for ( Circle circle : circles ) {
if ( circle . pointId == pointId ){
return circle ;
}
}
return null ;
}
}
 来自CODE的代码片
android19MyView.java

再去使用这个类就OK了

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
           
           
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 ));
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值