android 涂鸦
public class PaintView extends View {
private Canvas mCanvas;
private Path mPath;
private Paint mPaint;
private float mX, mY;
private Bitmap imgBitmap;
private static final float TOUCH_TOLERANCE = 4;
/**
* 构造
*
* @param context
* @param width
* 屏幕宽度
*
* @param height
* 屏幕高度
*
*/
public PaintView(Context context, Bitmap bm, Paint paint) {
super(context);
this.imgBitmap = bm;
this.mPaint = paint;
mCanvas = new Canvas(imgBitmap);
mPath = new Path();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(imgBitmap, 0, 0, mPaint);
canvas.drawPath(mPath, mPaint);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
}
public Bitmap getBitmap() {
return imgBitmap;
}
/**
* 屏幕触摸开始
*
* @param x
* X坐标
* @param y
* Y坐标
*/
private void touchStart(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
this.mX = x;
this.mY = y;
}
private void touchUp() {
mPath.lineTo(mX, mY);
mCanvas.drawPath(mPath, mPaint);
mPath.reset();
}
private void touchMove(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchStart(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touchMove(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touchUp();
invalidate();
break;
default:
break;
}
return true;
}
}
private Paint mPaint;
private MaskFilter mEmboss;
private MaskFilter mBlur;
Bitmap bitmap;
PaintView paintView;
LinearLayout linearLayout;
Bitmap imageBitmap;
public static final String MIME_TYPE_IMAGE_JPEG = "image/jpeg";
public static final int ACTIVITY_GET_IMAGE = 0;
private byte[] mContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.paint);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(6);
mEmboss=new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);
mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
//默认为灰色背景
mPaint.setColor(0xFFFF0000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// 填充选项菜单
MenuInflater inflater = getMenuInflater();
SubMenu choosepicMenu = menu.addSubMenu(0,1,1,"选择图片");
SubMenu boldMenu = menu.addSubMenu(0,2,2,"线条粗细");
SubMenu effectMenu=menu.addSubMenu(0,3,3,"线条效果");
effectMenu.add(11, 7, 1, "浮雕");
effectMenu.add(11, 8, 2, "空心");
effectMenu.add(11, 9, 3, "模糊");
effectMenu.add(11, 10, 4, "正常");
SubMenu colorMenu=menu.addSubMenu(0,4,4,"线条颜色");
SubMenu clearMenu=menu.addSubMenu(0,5,5,"清除");
SubMenu saveMenu=menu.addSubMenu(0,6,6,"保存");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
getImage.addCategory(Intent.CATEGORY_OPENABLE);
getImage.setType(MIME_TYPE_IMAGE_JPEG);
startActivityForResult(getImage, ACTIVITY_GET_IMAGE);
break;
case 2:
Toast.makeText(this, "线条粗细", Toast.LENGTH_LONG).show();
break;
case 4:
new ColorPickerDialog(this, this, mPaint.getColor()).show();
break;
case 5: //清除
break;
case 6: //保存图片
Bitmap bitmap = paintView.getBitmap();
FileOutputStream fos = null;
String filepath="/sdcard/"+System.currentTimeMillis()+".jpg";
try {
fos = new FileOutputStream(filepath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
Toast.makeText(this, "图片文件已经保存到"+filepath +" 。",Toast.LENGTH_LONG).show();
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
break;
case 7: //浮雕
mPaint.setMaskFilter(mEmboss);
break;
case 8 :
break;
case 9:
mPaint.setMaskFilter(mBlur);
break;
case 10:
mPaint.setMaskFilter(null);
default:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void colorChanged(int color) {
mPaint.setColor(color);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
ContentResolver resolver = getContentResolver();
if (requestCode == ACTIVITY_GET_IMAGE) {
try {
Uri originalUri = data.getData();
mContent = getBytesFromInputStream(resolver.openInputStream(Uri
.parse(originalUri.toString())), 3500000);
imageBitmap = getPicFromBytes(mContent, null);
bitmap = Bitmap.createBitmap(imageBitmap, 0, 0, imageBitmap.getWidth(), imageBitmap
.getHeight());
Bitmap b=bitmap.copy(Bitmap.Config.ARGB_8888, true);
paintView = new PaintView(this, b, mPaint);
linearLayout.removeAllViews();
linearLayout.addView(paintView);
b=null;
imageBitmap=null;
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
public static Bitmap getPicFromBytes(byte[] bytes,
BitmapFactory.Options opts) {
if (bytes != null)
if (opts != null) {
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
opts);
} else {
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
return null;
}
public static byte[] getBytesFromInputStream(InputStream is, int bufsiz)
throws IOException {
int total = 0;
byte[] bytes = new byte[4096];
ByteBuffer bb = ByteBuffer.allocate(bufsiz);
while (true) {
int read = is.read(bytes);
if (read == -1)
break;
bb.put(bytes, 0, read);
total += read;
}
byte[] content = new byte[total];
bb.flip();
bb.get(content, 0, total);
return content;
}