欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-11 15:51:52
本文实例讲述了android编程实现在bitmap上涂鸦效果。分享给大家供大家参考,具体如下: 布局文件:

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

布局文件:

<?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文件:

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文件:

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. 创建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程序设计有所帮助。

上一篇:

下一篇: