Android编程实现在Bitmap上涂鸦效果

时间:2021-08-12 08:33:57

本文实例讲述了Android编程实现在Bitmap涂鸦效果。分享给大家供大家参考,具体如下:

布局文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >
 <LinearLayout
  android:id="@+id/handwriteview"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />
 <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal"
  android:gravity="center_horizontal" >
  <Button
   android:id="@+id/clear"
   android:layout_width="200dp"
   android:layout_height="wrap_content"
   android:text="清屏" />
 </LinearLayout>
</LinearLayout>

重写的View文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
public class HandWrite extends View
{
 private Paint paint = null;
 private Bitmap originalBitmap = null;
 private Bitmap new1Bitmap = null;
 private Bitmap new2Bitmap = null;
 private float clickX = 0,clickY = 0;
 private float startX = 0,startY = 0;
 private boolean isMove = true;
 private boolean isClear = false;
 private int color = Color.GREEN;
 private float strokeWidth = 2.0f;
 public HandWrite(Context context,Bitmap b)
 {
  super(context);
  originalBitmap = Bitmap.createBitmap(b).copy(Bitmap.Config.ARGB_8888, true);
  new1Bitmap = Bitmap.createBitmap(originalBitmap);
 }
 public void clear(){
  isClear = true;
  new2Bitmap = Bitmap.createBitmap(originalBitmap);
  invalidate();
 }
 public void setstyle(float strokeWidth){
  this.strokeWidth = strokeWidth;
 }
 @Override
 protected void onDraw(Canvas canvas)
 {
  super.onDraw(canvas);
  canvas.drawBitmap(HandWriting(new1Bitmap), 0, 0,null);
 }
 public Bitmap HandWriting(Bitmap originalBitmap)
 {
  Canvas canvas = null;
  if(isClear){
   canvas = new Canvas(new2Bitmap);
  }
  else{
   canvas = new Canvas(originalBitmap);
  }
  paint = new Paint();
  paint.setStyle(Style.STROKE);
  paint.setAntiAlias(true);
  paint.setColor(color);
  paint.setStrokeWidth(strokeWidth);
  if(isMove){
   canvas.drawLine(startX, startY, clickX, clickY, paint);
  }
  startX = clickX;
  startY = clickY;
  if(isClear){
   return new2Bitmap;
  }
  return originalBitmap;
 }
 @Override
 public boolean onTouchEvent(MotionEvent event)
 {
  clickX = event.getX();
  clickY = event.getY();
  if(event.getAction() == MotionEvent.ACTION_DOWN){
   isMove = false;
   invalidate();
   return true;
  }
  else if(event.getAction() == MotionEvent.ACTION_MOVE){
   isMove = true;
   invalidate();
   return true;
  }
  return super.onTouchEvent(event);
 }
}

Activity文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class HandWritingActivity extends Activity
{
 /** Called when the activity is first created. */
 private LinearLayout handWrite = null;
 private Button clear = null;
 int requestWidth=116;
 int requestHeight=173;
 int inSampleSize;
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_hand_writing);
  handWrite = (LinearLayout)findViewById(R.id.handwriteview);
  clear = (Button)findViewById(R.id.clear);
  clear.setOnClickListener(new clearListener());
 }
 private class clearListener implements OnClickListener{
  public void onClick(View v)
  {
//   handWrite.clear();
   BitmapFactory.Options opts = new Options();
   opts.inJustDecodeBounds = true;// 让 bimapfactory假的解析这个位图,只获取位图的边框信息
   BitmapFactory.decodeResource(getResources(), R.drawable.cool, opts);
   if (opts.outHeight > requestHeight || opts.outWidth > requestWidth) {
    if (opts.outWidth > opts.outHeight) {
     inSampleSize = Math.round((float) opts.outHeight
       / (float) requestHeight);
    } else {
     inSampleSize = Math.round((float) opts.outWidth
       / (float) requestWidth);
    }
   }
    System.out.println("宽度:" + opts.outWidth);
    System.out.println("高度:" + opts.outHeight);
   opts.inSampleSize = inSampleSize;
   System.out.println(inSampleSize);
   opts.inJustDecodeBounds = false;// 由于已经得到了缩放比例 ,让位图工厂真正的解析这个位图
   // 由于前面 我们已经解析了这个输入流, 需要重新初始化这个输入流
   Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.cool, opts);
   HandWrite hw = new HandWrite(HandWritingActivity.this, b);
   System.out.println(b.getWidth());
   handWrite.addView(hw);
  }
 }
}

整合的一个涂鸦工具类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
 * 使用方法:
 * 1. 创建TuYaView类实例
 * 2. 调用drawTuya方法
 * 3. 参数1:context
 * 4. 参数2:图像的byte[]字节数组
 * 5. ImageView实例
 * 6. 画笔定义
 * **/
import com.ziipin.lhdc.utils.ToastUtil;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.BitmapFactory.Options;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
public class TuYaView {
 // 原始图片
 private Bitmap mOrignBitmap;
 private Bitmap mEditBitmap;
 private int inSampleSize;
 private int requestWidth = 500;
 private int requestHeight = 700;
 /** 编辑图片的画布 */
 private Canvas mCanvas;
 private ImageView image;
 private Paint mPaint;
 public Bitmap drawTuya(Context context, byte[] _data, ImageView image,
   Paint mPaint) {
  this.image = image;
  this.mPaint = mPaint;
  mOrignBitmap = BitmapFactory.decodeByteArray(_data, 0, _data.length);
  return showEditBitmap(context, _data, image);
 }
 /**
  * 显示编辑的图片
  */
 private Bitmap showEditBitmap(Context context, byte[] _data, ImageView image) {
  mOrignBitmap = getScaleBitmap(_data, image);
  if (mOrignBitmap == null) {
   ToastUtil.show(context, "编辑出错");
  }
  mEditBitmap = mOrignBitmap.copy(mOrignBitmap.getConfig(), true);
  mCanvas = new Canvas(mEditBitmap);
  mCanvas.drawBitmap(mOrignBitmap, new Matrix(), new Paint());
  image.setImageBitmap(mEditBitmap);
  image.setOnTouchListener(mTouchListener);
  return mEditBitmap;
 }
 /**
  * 获取结果缩放放后的图片
  *
  * @return
  */
 private Bitmap getScaleBitmap(byte[] _data, ImageView image) {
  BitmapFactory.Options opts = new Options();
  opts.inJustDecodeBounds = true;// 让 bimapfactory假的解析这个位图,只获取位图的边框信息
  BitmapFactory.decodeByteArray(_data, 0, _data.length, opts);
  if (opts.outHeight > requestHeight || opts.outWidth > requestWidth) {
   if (opts.outWidth > opts.outHeight) {
    inSampleSize = Math.round((float) opts.outHeight
      / (float) requestHeight);
   } else {
    inSampleSize = Math.round((float) opts.outWidth
      / (float) requestWidth);
   }
  }
  opts.inSampleSize = inSampleSize;
  opts.inJustDecodeBounds = false;// 由于已经得到了缩放比例 ,让位图工厂真正的解析这个位图
  // 由于前面 我们已经解析了这个输入流, 需要重新初始化这个输入流
  Bitmap bmp = BitmapFactory
    .decodeByteArray(_data, 0, _data.length, opts);
  return bmp;
 }
 // touch事件
 private OnTouchListener mTouchListener = new OnTouchListener() {
  int startx = 0;
  int starty = 0;
  @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_MOVE: // 手指在imageview上中移动
    int x = (int) event.getX();
    int y = (int) event.getY();
    mCanvas.drawLine(startx, starty, x, y, mPaint);
    startx = (int) event.getX();
    starty = (int) event.getY();
    image.invalidate();
    break;
   }
   return true;
  }
 };
}

希望本文所述对大家Android程序设计有所帮助。