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:
try {
// 保存图片到SD卡上
File file = new File( Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + “.png”);
FileOutputStream stream = new FileOutputStream(file);
bmcopy.compress(Bitmap.CompressFormat.PNG, 100, stream);
Toast.makeText(MainActivity.this, “保存图片成功”, Toast.LENGTH_LONG).show();
// Android设备Gallery应用只会在启动的时候扫描系统文件夹
// 这里模拟一个媒体装载的广播,用于使保存的图片可以在Gallery中查看
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
intent.setData( Uri.fromFile(Environment
.getExternalStorageDirectory()));
sendBroadcast(intent);
} catch (Exception e) {
Toast.makeText(MainActivity.this, "保存图片失败", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
break;
}
}
}