Android编程实现的手写板和涂鸦功能,android编程

本文实例讲述了android编程实现的手写板和涂鸦功能。分享给大家供大家参考,具体如下:

    下面仿一个android手写板和涂鸦的功能,直接上代码:

    write_pad.xml

[Java] 查看源文件 复制代码

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:greendroid="http://schemas.android.com/apk/res/com.cyrilmottier.android.gdcatalog"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <framelayout
    android:id="@+id/tablet_view"
    android:layout_width="fill_parent"
    android:layout_height="300dp" >
  </framelayout>
  <linearlayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/bottom_bar"
    android:paddingtop="4dp" >
    <button
      android:id="@+id/write_pad_ok"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="确定" />
    <button
      android:id="@+id/write_pad_clear"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="清除" />
    <button
      android:id="@+id/write_pad_cancel"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="取消" />
  </linearlayout>
</linearlayout>
    这个是手写板的主要布局文件,能够手写的部分是一个framelayout。下面有确定、清除和取消按钮,用来保存和擦除签名。

    主要代码逻辑如下:

    mainactivity.java

[Java] 查看源文件 复制代码

package com.jackie.handwriting;
import java.io.bytearrayoutputstream;
import java.io.file;
import java.io.fileoutputstream;
import java.io.ioexception;
import android.app.activity;
import android.graphics.bitmap;
import android.os.bundle;
import android.os.environment;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.imageview;
import android.widget.textview;
public class mainactivity extends activity {
  private imageview mivsign;
  private textview mtvsign;
  private bitmap msignbitmap;
  /** called when the activity is first created. */
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    mivsign = (imageview) findviewbyid(r.id.iv_sign);
    mtvsign = (textview) findviewbyid(r.id.tv_sign);
    mtvsign.setonclicklistener(new onclicklistener() {
      @override
      public void onclick(view view) {
        writepaddialog mwritepaddialog = new writepaddialog(
            mainactivity.this, new writedialoglistener() {
              @override
              public void onpaintdone(object object) {
                msignbitmap = (bitmap) object;
                createsignfile();
                mivsign.setimagebitmap(msignbitmap);
                mtvsign.setvisibility(view.gone);
              }
            });
        mwritepaddialog.show();
      }
    });
  }
  //创建签名文件
  private void createsignfile() {
    bytearrayoutputstream baos = null;
    fileoutputstream fos = null;
    string path = null;
    file file = null;
    try {
      path = environment.getexternalstoragedirectory() + file.separator + system.currenttimemillis() + ".jpg";
      file = new file(path);
      fos = new fileoutputstream(file);
      baos = new bytearrayoutputstream();
      //如果设置成bitmap.compress(compressformat.jpeg, 100, fos) 图片的背景都是黑色的
      msignbitmap.compress(bitmap.compressformat.png, 100, baos);
      byte[] b = baos.tobytearray();
      if (b != null) {
        fos.write(b);
      }
    } catch (ioexception e) {
      e.printstacktrace();
    } finally {
      try {
        if (fos != null) {
          fos.close();
        }
        if (baos != null) {
          baos.close();
        }
      } catch (ioexception e) {
        e.printstacktrace();
      }
    }
  }
}
    paintview.java

[Java] 查看源文件 复制代码

package com.jackie.handwriting;
import android.content.context;
import android.graphics.bitmap;
import android.graphics.bitmap.config;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.matrix;
import android.graphics.paint;
import android.graphics.path;
import android.graphics.porterduff;
import android.view.motionevent;
import android.view.view;
public class paintview extends view {
  private paint mpaint;
  private path mpath;
  private bitmap mbitmap;
  private canvas mcanvas;
  private int screenwidth, screenheight;
  private float currentx, currenty;
  public paintview(context context, int screenwidth, int screenheight) {
    super(context);
    this.screenwidth = screenwidth;
    this.screenheight = screenheight;
    init();
  }
  private void init() {
    mpaint = new paint();
    mpaint.setantialias(true); // 去除锯齿
    mpaint.setstrokewidth(5);
    mpaint.setstyle(paint.style.stroke);
    mpaint.setcolor(color.black);
    mpath = new path();
    mbitmap = bitmap.createbitmap(screenwidth, screenheight, config.argb_8888);
    mcanvas = new canvas(mbitmap);
//   mcanvas.drawcolor(color.white);
  }
  @override
  protected void ondraw(canvas canvas) {
    canvas.drawbitmap(mbitmap, 0, 0, null);
    canvas.drawpath(mpath, mpaint);
  }
  @override
  public boolean ontouchevent(motionevent event) {
    float x = event.getx();
    float y = event.gety();
    switch (event.getaction()) {
    case motionevent.action_down:
      currentx = x;
      currenty = y;
      mpath.moveto(currentx, currenty);
      break;
    case motionevent.action_move:
      currentx = x;
      currenty = y;
      mpath.quadto(currentx, currenty, x, y); // 画线
      break;
    case motionevent.action_up:
      mcanvas.drawpath(mpath, mpaint);
      break;
    }
    invalidate();
    return true;
  }
  public bitmap getpaintbitmap() {
    return resizeimage(mbitmap, 320, 480);
  }
  public path getpath() {
    return mpath;
  }
  // 缩放
  public static bitmap resizeimage(bitmap bitmap, int width, int height) {
    int originwidth = bitmap.getwidth();
    int originheight = bitmap.getheight();
    float scalewidth = ((float) width) / originwidth;
    float scaleheight = ((float) height) / originheight;
    matrix matrix = new matrix();
    matrix.postscale(scalewidth, scaleheight);
    bitmap resizedbitmap = bitmap.createbitmap(bitmap, 0, 0, originwidth,
        originheight, matrix, true);
    return resizedbitmap;
  }
  //清除画板
  public void clear() {
    if (mcanvas != null) {
      mpath.reset();
      mcanvas.drawcolor(color.transparent, porterduff.mode.clear);
      invalidate();
    }
  }
}
        writepaddialog.java

[Java] 查看源文件 复制代码

package com.jackie.handwriting;
import android.app.dialog;
import android.content.context;
import android.os.bundle;
import android.util.displaymetrics;
import android.view.view;
import android.view.window;
import android.widget.button;
import android.widget.framelayout;
import android.widget.toast;
public class writepaddialog extends dialog {
  private context mcontext;
  private writedialoglistener mwritedialoglistener;
  private paintview mpaintview;
  private framelayout mframelayout;
  private button mbtnok, mbtnclear, mbtncancel;
  public writepaddialog(context context,
      writedialoglistener writedialoglistener) {
    super(context);
    this.mcontext = context;
    this.mwritedialoglistener = writedialoglistener;
  }
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    requestwindowfeature(window.feature_no_title); //无标题
    setcontentview(r.layout.write_pad);
    mframelayout = (framelayout) findviewbyid(r.id.tablet_view);
    // 获取屏幕尺寸
    displaymetrics mdisplaymetrics = new displaymetrics();
    getwindow().getwindowmanager().getdefaultdisplay().getmetrics(mdisplaymetrics);
    int screenwidth = mdisplaymetrics.widthpixels;
    int screenheight = mdisplaymetrics.heightpixels;
    mpaintview = new paintview(mcontext, screenwidth, screenheight);
    mframelayout.addview(mpaintview);
    mpaintview.requestfocus();
    mbtnok = (button) findviewbyid(r.id.write_pad_ok);
    mbtnok.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view v) {
        if (mpaintview.getpath().isempty()) {
          toast.maketext(mcontext, "请写下你的大名", toast.length_short).show();
          return;
        }
        mwritedialoglistener.onpaintdone(mpaintview.getpaintbitmap());
        dismiss();
      }
    });
    mbtnclear = (button) findviewbyid(r.id.write_pad_clear);
    mbtnclear.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view v) {
        mpaintview.clear();
      }
    });
    mbtncancel = (button) findviewbyid(r.id.write_pad_cancel);
    mbtncancel.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view v) {
        cancel();
      }
    });
  }
}
    writediloglistener.java

[Java] 查看源文件 复制代码

package com.jackie.handwriting;
/**
 * 监听手写板对话框
 * @author chengcj1
 *
 */
public interface writedialoglistener {
  public void onpaintdone(object object);
}
    效果如下:

Android编程实现的手写板和涂鸦功能,android编程_第1张图片

    更多关于android相关内容感兴趣的读者可查看本站专题:《android图形与图像处理技巧总结》、《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结》

文章来源:安卓巴士
Android编程实现的手写板和涂鸦功能,android编程_第2张图片

你可能感兴趣的:(干货)