Android中通过自定义签名控件实现手写签名
生活随笔
收集整理的這篇文章主要介紹了
Android中通过自定义签名控件实现手写签名
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
場景
實現(xiàn)手寫簽名并獲取簽名照片
注:
博客:
BADAO_LIUMANG_QIZHI的博客_霸道流氓氣質(zhì)_CSDN博客-C#,SpringBoot,架構(gòu)之路領域博主
關(guān)注公眾號
霸道的程序猿
獲取編程相關(guān)電子書、教程推送與免費下載。
實現(xiàn)
1、新建空白項目,修改其頁面布局
2、布局文件activity_main.xml代碼
? <?xml version="1.0"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ImageViewandroid:id="@+id/iv_sign"android:layout_width="match_parent"android:layout_height="0dp"android:layout_gravity="center"android:layout_marginBottom="3dp"android:layout_weight="1"android:background="#FFFFFF" /><FrameLayoutandroid:id="@+id/fl_view"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="@color/teal_200" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="@android:drawable/bottom_bar"android:paddingTop="3dp"><Buttonandroid:id="@+id/btn_ok"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="確定"/><Buttonandroid:id="@+id/btn_clear"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="清除"/></LinearLayout> </LinearLayout>?3、修改MainActivity代碼
package com.badao.handwrittensignature;import androidx.appcompat.app.AppCompatActivity; 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.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.Toast;public class MainActivity extends AppCompatActivity {private ImageView imageSign;private SignatureView mView;@Override public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imageSign = findViewById(R.id.iv_sign);FrameLayout frameLayout = findViewById(R.id.fl_view);mView = new SignatureView(this);frameLayout.addView(mView);mView.requestFocus();Button btnClear = findViewById(R.id.btn_clear);btnClear.setOnClickListener((v) -> {mView.clear();});Button btnOk = findViewById(R.id.btn_ok);btnOk.setOnClickListener((v) -> {Bitmap imageBitmap = mView.getCachebBitmap();imageSign.setImageBitmap(imageBitmap);});}/*** 自定義簽名控件*/class SignatureView extends View {//畫筆private Paint paint;//畫布private Canvas cacheCanvas;//位圖private Bitmap cachebBitmap;//圖片保存路徑private Path path;public Path getPath() {return path;}//位圖緩存public Bitmap getCachebBitmap() {return cachebBitmap;}public SignatureView(Context context) {super(context);init();}/*** 初始化*/private void init() {//設置畫筆paint = new Paint();paint.setAntiAlias(true);paint.setStrokeWidth(3);paint.setStyle(Paint.Style.STROKE);paint.setColor(Color.BLACK);path = new Path();//創(chuàng)建位圖cachebBitmap = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);//用自定義位圖構(gòu)建畫布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.BLUE);invalidate();}}@Override protected void onDraw(Canvas canvas) {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) {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;break;}case MotionEvent.ACTION_UP: {cacheCanvas.drawPath(path, paint);path.reset();break;}}invalidate();return true;}} }總結(jié)
以上是生活随笔為你收集整理的Android中通过自定义签名控件实现手写签名的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leaflet中加载离线OSM瓦片地图(
- 下一篇: Openlayers中加载Geoserv