這篇文章將為大家詳細(xì)講解有關(guān)android如何實(shí)現(xiàn)手寫(xiě)簽名功能,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)公司主要從事網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)漾濞,十多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):18980820575
具體內(nèi)容如下
代碼簡(jiǎn)單,就不發(fā)demo了,直接貼代碼
package com.xx; 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.support.v7.app.AppCompatActivity; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.FrameLayout; import android.widget.ImageView; import com.xx.R; /** * Description: 簽名類(lèi) * Copyright: Copyright (c)2018 * Company: * author: Corwin * version: 1.0 * date: 2018/9/5 18:32 * Modification History: * Date Author Version Description * ------------------------------------------------------------------ * 2018/9/5 Corwin 1.0 1.0 Version */ public class SignatureActivity extends AppCompatActivity { private ImageView imageSign; private SignatureView mView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_signature); 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 { //畫(huà)筆 private Paint paint; //畫(huà)布 private Canvas cacheCanvas; //位圖 private Bitmap cachebBitmap; //圖片保存路徑 private Path path; //位圖緩存 public Bitmap getCachebBitmap() { return cachebBitmap; } public SignatureView(Context context) { super(context); init(); } /** * 初始化 */ private void init() { //設(shè)置畫(huà)筆 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)建畫(huà)布 cacheCanvas = new Canvas(cachebBitmap); //設(shè)置畫(huà)布為白色 cacheCanvas.drawColor(Color.WHITE); } /** * 清除畫(huà)板,重置畫(huà)筆 */ 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.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; } } }
布局文件:
<?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" > <ImageView android: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="@color/white" /> <FrameLayout android:id="@+id/fl_view" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@color/white" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:drawable/bottom_bar" android:paddingTop="3dp" > <Button android:id="@+id/btn_ok" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="確定" /> <Button android:id="@+id/btn_clear" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="清除" /> </LinearLayout> </LinearLayout>
關(guān)于“android如何實(shí)現(xiàn)手寫(xiě)簽名功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
名稱欄目:android如何實(shí)現(xiàn)手寫(xiě)簽名功能
地址分享:http://www.muchs.cn/article34/ipppse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站改版、手機(jī)網(wǎng)站建設(shè)、電子商務(wù)、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)