点击按钮

WritePadDialog mWritePadDialog = new WritePadDialog(activity, new WritePadDialog.WriteDialogListener() {@Overridepublic void onPaintDone(Object object) {mSignBitmap = (Bitmap) object;createSignFile();}});mWritePadDialog.show();

画板对框

public class WritePadDialog extends Dialog {private Context mContext;private WriteDialogListener mWriteDialogListener;private PaintDrawingboard mPaintView;private FrameLayout mFrameLayout;private Button mBtnOK, mBtnClear, mBtnCancel;public WritePadDialog(Context context,WriteDialogListener writeDialogListener) {super(context);this.mContext = context;this.mWriteDialogListener = writeDialogListener;}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE); //无标题setContentView(R.layout.write_pad);mFrameLayout = findViewById(R.id.tablet_view);// 获取屏幕尺寸DisplayMetrics mDisplayMetrics = new DisplayMetrics();getWindow().getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);int screenWidth = mDisplayMetrics.widthPixels;int screenHeight = mDisplayMetrics.heightPixels;mPaintView = new PaintDrawingboard(mContext, screenWidth, screenHeight);mFrameLayout.addView(mPaintView);mPaintView.requestFocus();mBtnOK = findViewById(R.id.write_pad_ok);mBtnOK.setOnClickListener(new View.OnClickListener() {@Overridepublic 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() {@Overridepublic void onClick(View v) {mPaintView.clear();}});}public interface WriteDialogListener {public void onPaintDone(Object object);}
}

画板功能

public class PaintDrawingboard extends View {private Paint mPaint;private Path mPath;private Bitmap mBitmap;private Canvas mCanvas;private int screenWidth, screenHeight;private float currentX, currentY;public PaintDrawingboard(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, Bitmap.Config.ARGB_8888);mCanvas = new Canvas(mBitmap);
//      mCanvas.drawColor(Color.WHITE);}@Overrideprotected void onDraw(Canvas canvas) {canvas.drawBitmap(mBitmap, 0, 0, null);canvas.drawPath(mPath, mPaint);}@Overridepublic 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 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();}}
}

保存bitmap成手机图片

//创建签名文件private void createSignFile() {ByteArrayOutputStream baos = null;FileOutputStream fos = null;String path = null;File file = null;try {SimpleDateFormat simpleDateFormat = new SimpleDateFormat();simpleDateFormat.applyPattern("yyyy年MM月dd日 HH时mm分ss秒");path = Environment.getExternalStorageDirectory() + File.separator + simpleDateFormat.format(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();}}}

layout文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><FrameLayoutandroid:id="@+id/tablet_view"android:layout_width="fill_parent"android:layout_height="300dp" /><TextViewandroid:id="@+id/divider2"android:layout_width="match_parent"android:layout_height="0.3dp"android:layout_below="@id/tv_pay_money_type"android:background="@color/colore9e9e9" /><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:background="@color/color_f6f6f6"><Buttonandroid:id="@+id/write_pad_ok"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:background="@color/white"android:text="确定" /><Buttonandroid:id="@+id/write_pad_clear"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:background="@color/white"android:text="清除" /></LinearLayout></LinearLayout>

Android手写板相关推荐

  1. Android 实现手写板技术

    Android手写板和涂鸦的功能,代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andro ...

  2. Android实现手写板和涂鸦功能

    下面仿一个Android手写板和涂鸦的功能,直接上代码: write_pad.xml <LinearLayout xmlns:android="http://schemas.andro ...

  3. android canvas 手写,自定义view—Canvas实现手写板和涂鸦功能

    学习导航 第一节:http://blog..net/bobo8945510/article/details/53197727 -自定义View-自定义属性及引用 第二节:http://blog..ne ...

  4. Android实现手写板功能

    自定义Android手写签名板组件,并进行手写区域裁剪,最终得到手写区域的图片(不是整个手写板区域) 使用方法: /*** 初始化视图,将手写版添加到布局中*/private void initVie ...

  5. Android绘图学习 - 手写板

    效果图: View代码:activity里设置显示该view即可 package com.tszy.view;import android.content.Context; import androi ...

  6. Android遇上手写板

    版本说明 Linux内核:3.0.8 Android  :4.2.2 Linux下鼠标驱动的研究分析 有些东西不能靠直觉,就得实实在在地研究一番才能知道所以然来.比如这次 的鼠标驱动,现象是 该鼠标为 ...

  7. Android 淡出效果手写板

    仿讯飞输入法手写效果,笔迹在抬笔后会渐渐淡出直至消失. EXPIRE_TIME 为保持颜色不变的时间,GRACE_TIME 为颜色透明度从255到0的时间,总的显示时间为 EXPIRE_TIME + ...

  8. android 仿qq手写板涂鸦

    以前博客的链接:点击打开链接 附上关键代码: MainView.java [java] view plaincopy package com.tszy.views; import java.io.Fi ...

  9. android开发分享Android实现手写板功能

    笔画为一次down-move-up的集合撤销笔画并非一次path的动作撤销 应该也是一次down-move -up的撤销为了更好的笔画需要使用贝塞尔曲线来完成 手写板文件:url80.ctfile.c ...

最新文章

  1. R语言数据纵向合并rbind函数实战(以及rbind.fill函数合并两个数据列不同的dataframe)
  2. CentOS6.5 iscsi配置
  3. python 爬虫源代码-python 爬虫-1:下载网页源代码
  4. 本地搭建docker私服
  5. r语言和metawin_如何创建R的HelloWorld包(Windows或Linux环境下)
  6. 伍迷随想冷饭集 之 瞻前顾后之随想
  7. 字符内存转成字符串_字符串内存内部
  8. python requests编码的问题_python requests 编码问题
  9. [置顶] EasyUI提交表单
  10. [Ext JS6]视图模型和数据绑定
  11. MD5加密以及产生唯一的ID
  12. asp.net 母版页使用方法
  13. latex附录中放python代码_LaTeX 里「添加程序代码」的完美解决方案
  14. 一起学爬虫(Python) — 07
  15. 2021年美国大学生数学建模竞赛C题思路分析
  16. python实现嗅探
  17. java web简单的网上名片管理系统
  18. 手机屏幕显示正常但是触摸有一部分出问题,是内屏坏了吗?保修期内手机该不该走官方售后?
  19. 肖秀荣真的是“yyds”吗?会被反押题吗?今年还会押中原题吗
  20. 【Hexo】nexT主题使用攻略基础——添加分类、标签及关于

热门文章

  1. 王学岗Linux系统的使用和Linux命令
  2. storm drpc学习
  3. C语言打印数组以及打印注意事项
  4. Conflux生态基金代付规则调整公告(20211111)
  5. xshell生成公钥私钥并实现登录
  6. 郴州市三中2021高考成绩查询,2021郴州高中排名前十最新名单
  7. 郴州市三中2021高考成绩查询,2021年郴州高中录取分数线是多少及高中排名榜
  8. 【Windows】Antimalware Service Executable cpu占用过高
  9. 升级python2升级到python3_Centos6.6升级python2到python3
  10. python中文相似度_基于TF-IDF、余弦相似度算法实现文本相似度算法的Python应用