Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android實現手寫簽名

Android實現手寫簽名

編輯:關於Android編程

本文實例為大家分享了Android手寫簽名的實現方法,產品要求用戶可以在app上簽協議。。所以得弄個手寫簽名版,參考了一些資料自己寫了個PaintView去繼承View,實現簽名功能。

package com.****.*****.widget;
 
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
 
/**
 * This view implements the drawing canvas.
 * <p/>
 * It handles all of the input events and drawing functions.
 * 簽名版
 */
public class PaintView extends View {
 private Paint paint;
 private Canvas cacheCanvas;
 private Bitmap cachebBitmap;
 private Path path;
 private OnMoveLisener lisener;
 
 
 public void setSize(int width,int height,OnMoveLisener lisener) {
  this.lisener=lisener;
  init(width,height);
 }
 
 public PaintView(Context context, AttributeSet attrs) {
  super(context, attrs);
  //init(0,0);
 }
 
 public Bitmap getCachebBitmap() {
  return cachebBitmap;
 }
 
 private void init(int width,int height) {
  paint = new Paint();
  paint.setAntiAlias(true);
  paint.setStrokeWidth(3);
  paint.setStyle(Paint.Style.STROKE);
  paint.setColor(Color.BLACK);
  path = new Path();
  cachebBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  cacheCanvas = new Canvas(cachebBitmap);
  cacheCanvas.drawColor(Color.WHITE);
 }
 
 public void clear() {
  if (cacheCanvas != null) {
 
   paint.setColor(Color.WHITE);
   cacheCanvas.drawPaint(paint);
   paint.setColor(Color.BLACK);
   cacheCanvas.drawColor(Color.WHITE);
   invalidate();
  }
 }
 
 
 @Override
 protected void onDraw(Canvas canvas) {
  // canvas.drawColor(BRUSH_COLOR);
  canvas.drawBitmap(cachebBitmap, 0, 0, null);
  canvas.drawPath(path, paint);
 }
 
 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
 
  int curW = cachebBitmap != null ? cachebBitmap.getWidth() : 0;
  int curH = cachebBitmap != null ? cachebBitmap.getHeight() : 0;
  if (curW >= w && curH >= h) {
   return;
  }
 
  if (curW < w)
   curW = w;
  if (curH < h)
   curH = h;
 
  Bitmap newBitmap = Bitmap.createBitmap(curW, curH, Bitmap.Config.ARGB_8888);
  Canvas newCanvas = new Canvas();
  newCanvas.setBitmap(newBitmap);
  if (cachebBitmap != null) {
   newCanvas.drawBitmap(cachebBitmap, 0, 0, null);
  }
  cachebBitmap = newBitmap;
  cacheCanvas = newCanvas;
 }
 
 private float cur_x, cur_y;
 
 @Override
 public boolean onTouchEvent(MotionEvent event) {
 
  getParent().requestDisallowInterceptTouchEvent(true);// 通知父控件勿攔截本控件touch事件
 
  float x = event.getX();
  float y = event.getY();
 
  switch (event.getAction()) {
   case MotionEvent.ACTION_DOWN: {
    cur_x = x;
    cur_y = y;
    path.moveTo(cur_x, cur_y);
    break;
   }
 
   case MotionEvent.ACTION_MOVE: {
    path.quadTo(cur_x, cur_y, x, y);
    cur_x = x;
    cur_y = y;
    lisener.hideWords();//隱藏提醒的文字
    break;
   }
 
   case MotionEvent.ACTION_UP: {
    cacheCanvas.drawPath(path, paint);
    path.reset();
    break;
   }
  }
 
  invalidate();
 
  return true;
 }
 
 
 public interface OnMoveLisener{
  void hideWords();//主界面回調後隱藏提示文字
 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved