Android仿銀行客戶簽名并且保存簽名的截圖文件并命名為本地時(shí)間
首先需要一個(gè)自定義view用來簽字使用,可以修改顏色和畫筆的粗細(xì),可以擦拭重新畫
package com.android.tcm.view; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffXfermode; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class SignView extends View { private Paint paint; private Canvas cacheCanvas; private Bitmap cachebBitmap; private Path path; static final int BACKGROUND_COLOR = Color.WHITE; static final int BRUSH_COLOR = Color.BLACK; public SignView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); // initView(context); // TODO Auto-generated constructor stub } public SignView(Context context, AttributeSet attrs) { super(context, attrs); // initView(context); // TODO Auto-generated constructor stub } public SignView(Context context) { super(context); // initView(context); // TODO Auto-generated constructor stub } public void initView(Context context) { } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub super.onMeasure(widthMeasureSpec, heightMeasureSpec); paint = new Paint(); paint.setAntiAlias(true); paint.setStrokeWidth(3); paint.setStyle(Paint.Style.STROKE); paint.setColor(Color.RED); path = new Path(); cachebBitmap = Bitmap.createBitmap( MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec), Config.ARGB_8888); cacheCanvas = new Canvas(cachebBitmap); cacheCanvas.drawColor(Color.TRANSPARENT); } public Bitmap getCachebBitmap() { return cachebBitmap; } public void clear() { if (cacheCanvas != null) { paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR)); cacheCanvas.drawPaint(paint); paint = new Paint(); paint.setAntiAlias(true); paint.setStrokeWidth(3); paint.setStyle(Paint.Style.STROKE); paint.setColor(Color.RED); 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, 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: { if(isListener!=null){ isListener.sign(); } 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; } public interface isSignListener{ void sign(); } isSignListener isListener; public void setIsListener(isSignListener isListener) { this.isListener = isListener; } }
布局代碼如下
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="40dp"> <com.android.tcm.view.SignView android:id="@+id/signView" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:orientation="horizontal"> <TextView android:id="@+id/tv_clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:paddingBottom="15dp" android:paddingTop="15dp" android:text="擦除重簽" android:textSize="18sp" /> <TextView android:id="@+id/tv_commit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:gravity="center" android:paddingBottom="15dp" android:paddingTop="15dp" android:text="確認(rèn)" android:textSize="18sp" /> </LinearLayout> </RelativeLayout> </RelativeLayout>
主函數(shù)代碼,用于獲取截圖(id:rl)的,并且把文件保存到本地(文件夾TVC下文件命名為當(dāng)前時(shí)間如20170713 10:31:31.jpg)
package com.android.tcm.activity; import android.graphics.Bitmap; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.RelativeLayout; import android.widget.TextView; import com.android.tcm.R; import com.android.tcm.view.SignView; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; /** * Created by sf. */ public class SignActivity extends Activity { private RelativeLayout rl; private SignView mView; private TextView commit, clear; private Bitmap mSignBitmap; private String signPath; private long time; private String fileName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sign); initView(); } public void initView() { mView = (SignView) findViewById(R.id.signView); commit = (TextView) findViewById(R.id.tv_commit); clear = (TextView) findViewById(R.id.tv_clear); rl= (RelativeLayout) findViewById(R.id.rl); commit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // commit.getDrawingCache();//獲取控件的截圖 // saveSign(BitmapUtil.myShot(SignActivity.this)); rl.setDrawingCacheEnabled(true); saveSign(rl.getDrawingCache()); rl.setDrawingCacheEnabled(false); } }); clear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { mView.clear(); } }); } /** * signPath是圖片保存路徑 * * @param bit */ public void saveSign(Bitmap bit) { time = System.currentTimeMillis(); fileName = getDateTimeFromMillisecond(time); mSignBitmap = bit; signPath = createFile(); } /** * @return */ private String createFile() { ByteArrayOutputStream baos = null; String _path = null; try { String sign_dir = Environment.getExternalStorageDirectory() .getPath() + "/" + "TCM" + "/"; File dir = new File(sign_dir); if (!dir.exists()) { dir.mkdirs(); } _path = sign_dir + fileName + ".jpg"; baos = new ByteArrayOutputStream(); mSignBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] photoBytes = baos.toByteArray(); if (photoBytes != null) { new FileOutputStream(new File(_path)).write(photoBytes); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (baos != null) baos.close(); } catch (IOException e) { e.printStackTrace(); } } return _path; } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); if (mSignBitmap != null) { mSignBitmap.recycle(); } } /** * 將毫秒轉(zhuǎn)化成固定格式的時(shí)間 * 時(shí)間格式: yyyy-MM-dd HH:mm:ss * * @param millisecond * @return */ public static String getDateTimeFromMillisecond(Long millisecond) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(millisecond); String dateStr = simpleDateFormat.format(date); return dateStr; } }
以上所述是小編給大家介紹的Android仿銀行客戶簽名并且保存簽名的截圖文件并命名為本地時(shí)間,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Native.js獲取監(jiān)聽開關(guān)等操作Android藍(lán)牙設(shè)備實(shí)例代碼
- native.js獲取手機(jī)硬件基本信息實(shí)例代碼android版
- Dcloud的native.js直接撥打電話Android實(shí)例代碼
- DCloud的native.js調(diào)用系統(tǒng)分享實(shí)例Android版代碼
- Android中通過view方式獲取當(dāng)前Activity的屏幕截圖實(shí)現(xiàn)方法
- Android中如何獲取視頻文件的截圖、縮略圖
- Android模擬器中窗口截圖存成文件實(shí)現(xiàn)思路及代碼
- 詳解有關(guān)Android截圖與錄屏功能的學(xué)習(xí)
- Android實(shí)現(xiàn)截圖和分享功能的代碼
- Android獲取常用輔助方法(獲取屏幕高度、寬度、密度、通知欄高度、截圖)
- Android實(shí)現(xiàn)拍照截圖功能
- android截圖事件監(jiān)聽的原理與實(shí)現(xiàn)
- Android屏幕及view的截圖實(shí)例詳解
- Android截屏截圖的幾種方法總結(jié)
- Android實(shí)現(xiàn)截圖分享qq 微信功能
- Android實(shí)現(xiàn)從相冊(cè)截圖的功能
- Android 中WebView 截圖的實(shí)現(xiàn)方式
- Android App內(nèi)監(jiān)聽截圖加二維碼功能代碼
- Android 5.0及以上編程實(shí)現(xiàn)屏幕截圖功能的方法
- Android 截圖功能源碼的分析
- Android使用WebView實(shí)現(xiàn)截圖分享功能
- Native.js屏幕截圖實(shí)例代碼
相關(guān)文章
Android?Flutter實(shí)現(xiàn)創(chuàng)意時(shí)鐘的示例代碼
時(shí)鐘這個(gè)東西很奇妙,總能當(dāng)做創(chuàng)意實(shí)現(xiàn)的入口。這篇文章主要介紹了如何通過Android?Flutter實(shí)現(xiàn)一個(gè)創(chuàng)意時(shí)鐘,感興趣的小伙伴可以了解一下2023-03-03Android實(shí)現(xiàn)bitmap指定區(qū)域滑動(dòng)截取功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)bitmap指定區(qū)域滑動(dòng)截取功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09解析Android開發(fā)優(yōu)化之:對(duì)Bitmap的內(nèi)存優(yōu)化詳解
在Android應(yīng)用里,最耗費(fèi)內(nèi)存的就是圖片資源。而且在Android系統(tǒng)中,讀取位圖Bitmap時(shí),分給虛擬機(jī)中的圖片的堆棧大小只有8M,如果超出了,就會(huì)出現(xiàn)OutOfMemory異常。所以,對(duì)于圖片的內(nèi)存優(yōu)化,是Android應(yīng)用開發(fā)中比較重要的內(nèi)容2013-05-05詳解Matisse與Glide--java.lang.NoSuchMethodError:com.bumptech.gl
這篇文章主要介紹了在使用Matisse與glide4.0.0以及4.0.0之后的版本過程中,碰到該問題java.lang.NoSuchMethodError:com.bumptech.glide.RequestManager.load的解決方法2021-08-08Android開發(fā)Jetpack?Compose元素Modifier特性詳解
這篇文章主要為大家介紹了Android開發(fā)Jetpack?Compose元素Modifier特性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10