Android 实现多点触摸画圆

实现效果图:

Android 实现多点触摸画圆_第1张图片

Android 实现多点触摸画圆_第2张图片

 

想要实现上述效果,非常的简单,我在这里就直接用代码给大家讲解了:

首先我们要写一个圆的类:

 

package com.example.android19_zhangkai_pointstouch;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import java.util.Random;

/**
 * Created by Administrator on 2017/7/9 0009.
 */

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);
    }
}

 

 

 

 

 

然后我们需要写一个自定义控件的类:

 

package com.example.android19_zhangkai_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 0009.
 */

public class MyView extends View {

    List 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);

        //多点触摸按下时,状态为5
        if (action_code>=5){
            action_code-=5;
        }

        switch (action_code){
            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();
        return true;
    }

    /**
     *
     * @param pointId 手指id
     * @return
     */
    public Circle get(int pointId){
        for (Circle circle : circles) {
            //判断是哪个手指松开
            if (circle.pointId==pointId){
                return circle;
            }
        }
        return null;
    }

}

 

 

 

 

 

 

package com.example.android19_zhangkai_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));
    }
}

 

 

 

 

 

上述代码便可以实现效果啦,注释也写在了代码中,有什么不懂得在下方评论,谢谢

你可能感兴趣的:(Android)