1.android开发中如何实现手写输入的记事本
实现手写功能的主要步骤:1. 自定义两个View,一个是TouchView,用于在上面画图,另一个是EditText,用于将手写的字显示在其中,并且,要将两个自定义View通过FrameLayout帧式布局重叠在起,以实现全屏手写的功能。
2 在TouchView中实现写字,并截取画布中的字以Bitmap保存。3. 设置定时器,利用handle更新界面。
下面是实现的细节:1. 手写的界面设计:如上图所示,和上节的画板界面一致,底部分选项菜单栏,有5个选项,分别是调整画笔大小,画笔颜色,撤销,恢复,以及清空,对于这些功能,之后几节再实现。布局文件activity_handwrite.xml
先来看com.example.notes.LineEditText,这个其实和添加记事中的界面一样,就是自定义EditText,并且在字的下面画一条线。LineEditText.javapublic class LineEditText extends EditText { private Rect mRect; private Paint mPaint; public LineEditText(Context context, AttributeSet attrs) { // TODO Auto-generated constructor stub super(context,attrs); mRect = new Rect(); mPaint = new Paint(); mPaint.setColor(Color.GRAY); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //得到EditText的总行数 int lineCount = getLineCount(); Rect r = mRect; Paint p = mPaint; //为每一行设置格式 for(int i = 0; i < lineCount;i++){ //取得每一行的基准Y坐标,并将每一行的界限值写到r中 int baseline = getLineBounds(i, r); //设置每一行的文字带下划线 canvas.drawLine(r.left, baseline+20, r.right, baseline+20, p); } }}另一个就是com.example.notes.TouchView,实现了绘制,及定时更新界面的功能,具体看代码TouchView.javapublic class TouchView extends View { private Bitmap mBitmap,myBitmap; private Canvas mCanvas; private Path mPath; private Paint mBitmapPaint; private Paint mPaint; private Handler bitmapHandler; GetCutBitmapLocation getCutBitmapLocation; private Timer timer; DisplayMetrics dm; private int w,h; public TouchView(Context context) { super(context); dm = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm); w = dm.widthPixels; h = dm.heightPixels; initPaint(); } public TouchView(Context context, AttributeSet attrs) { super(context,attrs); dm = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm); w = dm.widthPixels; h = dm.heightPixels; initPaint(); } //设置handler public void setHandler(Handler mBitmapHandler){ bitmapHandler = mBitmapHandler; } //初始化画笔,画布 private void initPaint(){ mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setDither(true); mPaint.setColor(0xFF00FF00); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); mPaint.setStrokeCap(Paint.Cap.ROUND); mPaint.setStrokeWidth(15); getCutBitmapLocation = new GetCutBitmapLocation(); //画布大小 mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); mCanvas = new Canvas(mBitmap); //所有mCanvas画的东西都被保存在了mBitmap中 mCanvas.drawColor(Color.TRANSPARENT); mPath = new Path(); mBitmapPaint = new Paint(Paint.DITHER_FLAG); timer = new Timer(true); } /** * 处理屏幕显示 */ Handler handler = new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case 1: myBitmap = getCutBitmap(mBitmap); Message message = new Message(); message.what=1; Bundle bundle = new Bundle();; bundle.putParcelable(bitmap,myBitmap); message.setData(bundle); bitmapHandler.sendMessage(message); RefershBitmap(); break; } super.handleMessage(msg); } }; /** * 发送消息给handler更新ACTIVITY */ TimerTask task = new TimerTask() { public void run() { Message message = new Message(); message.what=1; Log.i(线程, 来了); handler.sendMessage(message); } }; //切割画布中的字并返回 public Bitmap getCutBitmap(Bitmap mBitmap){ //得到手写字的四周位置,并向外延伸10px float cutLeft = getCutBitmapLocation.getCutLeft() - 10; float cutTop = getCutBitmapLocation.getCutTop() - 10; float cutRight = getCutBitmapLocation.getCutRight() + 10; float cutBottom = getCutBitmapLocation.getCutBottom() + 10; cutLeft = (0 > cutLeft ? 0 : cutLeft); cutT。
转载请注明出处众文网 » 基于安卓的记事本毕业论文(android开发中如何实现手写输入的记事本)