Android實(shí)現(xiàn)手寫簽名
本文實(shí)例為大家分享了Android手寫簽名的實(shí)現(xiàn)方法,產(chǎn)品要求用戶可以在app上簽協(xié)議。。所以得弄個(gè)手寫簽名版,參考了一些資料自己寫了個(gè)PaintView去繼承View,實(shí)現(xiàn)簽名功能。
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();//主界面回調(diào)后隱藏提示文字
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
Android自定義View實(shí)現(xiàn)打字機(jī)效果
最近在做Android開發(fā)的時(shí)候,需要做類似于打字機(jī)打字的效果,字一個(gè)一個(gè)地蹦出來,顯示每一個(gè)字都帶有打字的聲音。現(xiàn)在分享給大家,有需要的可以參考借鑒。2016-08-08
Android人臉識(shí)別Demo豎屏YUV方向調(diào)整和圖片保存(分享)
下面小編就為大家分享一篇Android人臉識(shí)別Demo實(shí)現(xiàn)豎屏YUV方向調(diào)整和圖片保存的方法。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
Android開發(fā)之拼音轉(zhuǎn)換工具類PinyinUtils示例
這篇文章主要介紹了Android開發(fā)之拼音轉(zhuǎn)換工具類PinyinUtils,涉及Android基于pinyin4j-2.5.0.jar包文件實(shí)現(xiàn)漢字轉(zhuǎn)拼音功能的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
Android開發(fā)壁紙的驗(yàn)證設(shè)置和確認(rèn)功能實(shí)現(xiàn)demo
android?wallpaper包括鎖屏壁紙和桌面壁紙,壁紙又區(qū)分靜態(tài)和動(dòng)態(tài)兩種。本文詳細(xì)介紹靜態(tài)壁紙?jiān)O(shè)置和確認(rèn),有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-04-04
Android Fragment 和 FragmentManager 的代碼分析
這篇文章主要介紹了Android Fragment 和 FragmentManager 的代碼分析,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-01-01
Android Studio去除界面默認(rèn)標(biāo)題欄的方法
這篇文章主要介紹了Android Studio去除界面默認(rèn)標(biāo)題欄的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2007-09-09
Android使用Scrolling機(jī)制實(shí)現(xiàn)Tab吸頂效果
app 首頁中經(jīng)常要實(shí)現(xiàn)首頁頭卡共享,tab 吸頂,內(nèi)容區(qū)通過 ViewPager 切換的需求,以前往往是利用事件處理來完成,但是這些也有一定的弊端和滑動(dòng)方面不如意的地方,本文我們利用NestedScrolling機(jī)制來實(shí)現(xiàn),感興趣的朋友可以參考下2024-01-01
Android自定義控件實(shí)現(xiàn)時(shí)鐘效果
這篇文章主要介紹了Android自定義控件實(shí)現(xiàn)時(shí)鐘效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12

