效果图:
package com.whzg.zbjy.homepage;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.whzg.zbjy.R;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class CopyActivity extends AppCompatActivity {
private Toolbar toolbar;
private TextView clear;
private Bitmap baseBitmap;
private ImageView iv;
private Canvas canvas;
private Paint paint;
private VelocityTracker mVelocityTracker;
private float velocityX, velocityY;
private int i = 40, j = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_copy);
toolbar = (Toolbar) findViewById(R.id.homepager_copy_toolbar);
toolbar.setNavigationIcon(R.drawable.back);
toolbar.setTitle("");
toolbar.setSubtitle("返回");
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CopyActivity.this.finish();
}
});
clear = (TextView) findViewById(R.id.homepager_copy_clear);
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 创建一张空白图片
baseBitmap = Bitmap.createBitmap(getWindowManager().getDefaultDisplay().getWidth(), getWindowManager().getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
// 创建一张画布
canvas = new Canvas(baseBitmap);
iv.setImageBitmap(baseBitmap);
}
});
iv = (ImageView) this.findViewById(R.id.homepager_copy_img);
// 创建一张空白图片
baseBitmap = Bitmap.createBitmap(getWindowManager().getDefaultDisplay().getWidth(), getWindowManager().getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
// 创建一张画布
canvas = new Canvas(baseBitmap);
// 创建画笔
paint = new Paint();
paintSet(30, 255);
// 先将灰色背景画上
canvas.drawBitmap(baseBitmap, new Matrix(), paint);
iv.setImageBitmap(baseBitmap);
iv.setOnTouchListener(new View.OnTouchListener() {
int startX;
int startY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 获取手按下时的坐标
startX = (int) event.getX();
startY = (int) event.getY();
break;
case MotionEvent.ACTION_UP:
i = 40;
j = 0;
break;
case MotionEvent.ACTION_MOVE:
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();//获得VelocityTracker类实例
}
mVelocityTracker.addMovement(event);//将事件加入到VelocityTracker类实例中
mVelocityTracker.computeCurrentVelocity(1);
velocityX = mVelocityTracker.getXVelocity();
velocityY = mVelocityTracker.getXVelocity();
float xyv = velocityX * velocityX + velocityY * velocityY;
xyv = (float) Math.sqrt(xyv);
for (; j < 5; j++) {
if (Math.abs(xyv) < 0.4) {
continue;
} else {
j = 0;
break;
}
}
if (Math.abs(xyv) <= 3 && j == 0) {
i = (i * 1 - 1) / 1;
}
if (j != 0) {
i = (i * 1 - 1) / 1;
j = 0;
}
if (Math.abs(xyv) > 3 ){
i = (i * 1 + 1) / 1;
if (i>=40){
i=40;
}
}
paintSet(i, 255);
if (i <= 0) {
paintSet(i, 0);
}
// 获取手移动后的坐标
int stopX = (int) event.getX();
int stopY = (int) event.getY();
// 在开始和结束坐标间画一条线
canvas.drawLine(startX, startY, stopX, stopY, paint);
// 实时更新开始坐标
startX = (int) event.getX();
startY = (int) event.getY();
iv.setImageBitmap(baseBitmap);
break;
}
return true;
}
});
}
/**
* 重设画笔
*
* @param width 画笔宽度
* @param alpha 画笔透明度
*/
public void paintSet(int width, int alpha) {
paint.setColor(getResources().getColor(R.color.black));
//消除锯齿
paint.setAntiAlias(true);
paint.setTextSize(20);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(width);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setAlpha(alpha);
}
public void save(View view) {
try {
File file = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
OutputStream stream = new FileOutputStream(file);
baseBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
stream.close();
// 模拟一个广播,通知系统sdcard被挂载
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
intent.setData(Uri.fromFile(Environment
.getExternalStorageDirectory()));
sendBroadcast(intent);
Toast.makeText(this, "保存图片成功", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, "保存图片失败", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}