本文实例讲述了AndroID编程实现的手写板和涂鸦功能。分享给大家供大家参考,具体如下:
下面仿一个AndroID手写板和涂鸦的功能,直接上代码:
write_pad.xml
<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
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,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
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,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,originWIDth,originHeight,matrix,true); return resizedBitmap; } //清除画板 public voID clear() { if (mCanvas != null) { mPath.reset(); mCanvas.drawcolor(color.transparent,PorterDuff.Mode.CLEAR); invalIDate(); } }}
WritePadDialog.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
package com.jackIE.handwriting;/** * 监听手写板对话框 * @author chengcj1 * */public interface WriteDialogListener { public voID onPaintDone(Object object);}
效果如下:
更多关于AndroID相关内容感兴趣的读者可查看本站专题:《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家AndroID程序设计有所帮助。
您可能感兴趣的文章:Android编程实现在Bitmap上涂鸦效果Android 开发实例简单涂鸦板Android 使用Path实现涂鸦功能Android实现Path平滑的涂鸦效果实例android实现简单的画画板实例代码Android采用双缓冲技术实现画板Android自定义SurfaceView实现画板功能Android实现画板、写字板功能(附源码下载)Android多媒体之画画板开发案例分享双缓冲技术实现Android 画板应用Android画画板的制作方法 总结以上是内存溢出为你收集整理的Android编程实现的手写板和涂鸦功能全部内容,希望文章能够帮你解决Android编程实现的手写板和涂鸦功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)