文章标题

public class MainActivity extends AppCompatActivity implements View.OnTouchListener{
ImageView image;
private static final String TAG = “TAG”;
Canvas canvas;
Paint p;
Paint p1;
Path path;
Bitmap baseBitmap;
Bitmap bitmap;
Button refresh,ablum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
//刷新
refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
refreshImage();
}
});

    //从相册里获取图片
    ablum.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


        }
    });
}

private void init(){
    image = (ImageView) findViewById(R.id.image01_main);
    image.setOnTouchListener(this);
    refresh = (Button) findViewById(R.id.btn01_main);
    ablum = (Button) findViewById(R.id.btn02_main);
    p = new Paint();
    p.setColor(Color.RED);
    p.setStrokeWidth(5);
    p1 = new Paint();
    p1.setColor(Color.GREEN);
    p1.setStrokeWidth(5);
    p1.setStyle(Paint.Style.STROKE);
    path = new Path();

}


private void initCanvas(){

// if (baseBitmap==null){
//新建一个空的Bitmap
baseBitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.ARGB_8888);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a2);
canvas = new Canvas(baseBitmap);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(bitmap,new Matrix(),p);
// }
image.setImageBitmap(baseBitmap);

}
float startX;
float startY;
float mstartX1;
float mstartY1;
@Override
public boolean onTouch(View v, MotionEvent event) {
    Log.d(TAG, "onTouch: ");
    initCanvas();
    switch (event.getAction()&MotionEvent.ACTION_MASK){
        case MotionEvent.ACTION_DOWN://按下第一个手指
            startX = event.getX();
            startY=event.getY();
            mstartY1=0;
            mstartX1=0;
            Log.d(TAG, "按下第一个手指MotionEvent.ACTION_DOWN: ");
            Log.d(TAG, "按下第一个手指d的坐标: x "+startX+"   y  : "+startY);
            path.moveTo(startX,startY);
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            float startX1 = event.getX(0);
            float startY1=event.getY(0);
            float startX2 = event.getX(1);
            float startY2=event.getY(1);
            Log.d(TAG, "按下第二个手指MotionEvent.ACTION_POINTER_DOWN: ");
            Log.d(TAG, "按下第二个手指d的坐标: startX1:  "+startX1+"  startY1 y  : "+startY1+" startX2  :"+startX2+"  startY2 : "+startY2);
            if (mstartX1>0&&mstartY1>0){
                path.lineTo(mstartX1,mstartY1);
                canvas.drawPath(path,p1);
                startX=mstartX1;
                startY=mstartY1;
                image.setImageBitmap(baseBitmap);
            }
            break;
        case MotionEvent.ACTION_MOVE:
             mstartX1 = event.getX();
             mstartY1=event.getY();
            Log.d(TAG, "按下第二个手指MotionEvent.ACTION_POINTER_DOWN: ");
            Log.d(TAG, "按下第二个手指d的坐标: mstartX1:  "+mstartX1+"  mstartY1 y  : "+mstartY1);
            canvas.drawPath(path,p1);
            canvas.drawLine(startX,startY,mstartX1,mstartY1,p);
            image.setImageBitmap(baseBitmap);
            break;
        case MotionEvent.ACTION_UP:
            Log.d(TAG, "MotionEvent.ACTION_UP: ");
            path.close();
            canvas.drawPath(path,p1);
            Log.d(TAG, "最后一个手指抬起来的时候的坐标 mstartX1: "+event.getX()+"-ss--"+mstartX1+"--mstartY1-"+event.getY()+"  ss:"+mstartY1);
            image.setImageBitmap(baseBitmap);
            break;

        case MotionEvent.ACTION_POINTER_UP:

// canvas.drawPath(path,p);
Log.d(TAG, “第一根手指抬起来的时候MotionEvent.ACTION_POINTER_UP: “);
break;
}
return true;
}

private void refreshImage(){
    if (baseBitmap!=null){
        baseBitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.ARGB_8888);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a2);
        canvas = new Canvas(baseBitmap);
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(bitmap,new Matrix(),p);
        path.reset();
    }
    image.setImageBitmap(baseBitmap);

}

}

你可能感兴趣的:(android项目知识点)