1. 程式人生 > >使用canvas畫圖

使用canvas畫圖

package com.example.paintcustomcontrol;

import android.annotation.SuppressLint; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private ImageView image;
Bitmap bitmap;
Bitmap bmcopy;
Canvas canvas;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
int startX;
int startY;
int stopX;
int stopY;
 Paint paint;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_main );
    initView();
    //載入原圖

/* bitmap = BitmapFactory.decodeResource( getResources(), R.drawable.a );

    //建立白紙,設定寬高圖片等引數
    bmcopy = Bitmap.createBitmap( bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig() );

    //建立畫板
    canvas = new Canvas( bmcopy );

    //建立畫筆
    paint = new Paint();

    paint.setStrokeWidth( 5 );

    //在紙上畫畫,白紙在畫板
    canvas.drawBitmap( bitmap, new Matrix(), paint );*/
    bitmap = BitmapFactory.decodeResource( getResources(), R.drawable.a );
    bmcopy = Bitmap.createBitmap( this.bitmap.getWidth(), this.bitmap.getHeight(), this.bitmap.getConfig() );
    canvas = new Canvas( bmcopy );
    paint = new Paint();

    canvas.drawBitmap( bitmap,new Matrix(),paint );

    image.setOnTouchListener( new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

// switch (motionEvent.getAction()) {

                case MotionEvent.ACTION_DOWN:
                    startX = (int) motionEvent.getX();
                    startY = (int) motionEvent.getY();

                 //   canvas.drawLine( startX,startY,stopX,stopY,paint);

                 //   image.setImageBitmap(bmcopy);
                    //  int x = (int) motionEvent.getX();
                    //  int y = (int) motionEvent.getY();
                    //   Log.e( "text","X座標"+x+"   "+"Y座標"+y);
                    //     Log.e( "text","+"+y+"+");


                    break;
                case MotionEvent.ACTION_MOVE:
                    stopX = (int) motionEvent.getX();
                    stopY = (int) motionEvent.getY();

                    canvas.drawLine( startX,startY,stopX,stopY, MainActivity.this.paint );
                    startX=stopX;
                    startY=stopY;

                    image.setImageBitmap(bmcopy);

// int x = (int) motionEvent.getX(); // int y = (int) motionEvent.getY(); // Log.e( “text”, “X座標” + x + " " + “Y座標” + y ); break;

                case MotionEvent.ACTION_UP:

                    break;
            }
            return true;
        }
    } );
}

private void initView() {
    image = (ImageView) findViewById( R.id.image );
    button1 = (Button) findViewById( R.id.button1 );
    button1.setOnClickListener( this );
    button2 = (Button) findViewById( R.id.button2 );
    button2.setOnClickListener( this );
    button3 = (Button) findViewById( R.id.button3 );
    button3.setOnClickListener( this );
    button4 = (Button) findViewById( R.id.button4 );
    button4.setOnClickListener( this );
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button1:
            paint.setColor( Color.RED );
            break;
        case R.id.button2:
            paint.setColor( Color.BLACK);
            break;
        case R.id.button3:
            if(bmcopy!=null){
                canvas.drawColor( Color.GRAY );
                image.setImageBitmap( bmcopy );
            }
            break;
        case R.id.button4:

            break;
    }
}

}