Android 實(shí)現(xiàn)銀聯(lián)刷卡機(jī)消費(fèi)后手動(dòng)簽名的功能(示例代碼)
幾天前去物管交物業(yè)費(fèi),物管工作人員說(shuō)小區(qū)引進(jìn)高新產(chǎn)品,使用銀行卡消費(fèi)后,不需要拿筆在銀聯(lián)機(jī)上簽名,直接用手指觸摸實(shí)現(xiàn)消費(fèi)簽名,當(dāng)時(shí)心想,果然是高科技,機(jī)子外形如下左圖,簽名如下右圖。

仔細(xì)一看,其實(shí)就是一個(gè)觸摸屏,用戶在上面直接手動(dòng)簽名,實(shí)現(xiàn)這個(gè)功能其實(shí)并不復(fù)雜,我們自定義一個(gè)控件,繼承view,使用 Canvas的drawLine,drawPoint這兩個(gè)方法來(lái)實(shí)現(xiàn)這個(gè)功能。
首先自定義控件 MDrawLineView
package com.view;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
/**
* Created by jiang on 2017/12/25.
*/
public class MDrawLineView extends View {
public MDrawLineView(Context context){
super(context);
}
public MDrawLineView(Context context,AttributeSet attrs){
super(context, attrs);
paint=new Paint(Paint.DITHER_FLAG);//創(chuàng)建一個(gè)畫(huà)筆
if(bitmap==null){
bitmap = Bitmap.createBitmap(900, 1200, Bitmap.Config.ARGB_8888); //設(shè)置位圖的寬高
}
canvas=new Canvas();
canvas.setBitmap(bitmap);
paint.setStyle(Paint.Style.STROKE);//設(shè)置非填充
paint.setStrokeWidth(5);//筆寬5像素
paint.setColor(Color.RED);//設(shè)置為紅筆
paint.setAntiAlias(true);//鋸齒不顯示
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(bitmap==null){
bitmap = Bitmap.createBitmap(900, 1200, Bitmap.Config.ARGB_8888); //設(shè)置位圖的寬高
}
canvas.drawBitmap(bitmap,0,0,null);
}
public void clear(){
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_MOVE: //用戶手指在屏幕上移動(dòng)畫(huà)線
canvas.drawLine(mov_x,mov_y,event.getX(),event.getY(),paint);
invalidate();
break;
case MotionEvent.ACTION_DOWN://用戶手指按下時(shí)畫(huà)起點(diǎn)
mov_x=(int) event.getX();
mov_y=(int) event.getY();
canvas.drawPoint(mov_x,mov_y,paint);
invalidate();
break;
case MotionEvent.ACTION_UP:
break;
}
mov_x=(int) event.getX();
mov_y=(int) event.getY();
return true;
//return super.onTouchEvent(event);
}
private int mov_x;//聲明起點(diǎn)x坐標(biāo)
private int mov_y;//聲明起點(diǎn)y坐標(biāo)
private Paint paint;//聲明畫(huà)筆
private Canvas canvas;//畫(huà)布
private Bitmap bitmap;//位圖
private int blcolor;
}
布局xml代碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:padding="10dp" android:orientation="vertical"> <com.view.MDrawLineView android:id="@+id/mDrawLine" android:layout_width="300dp" android:layout_height="400dp" android:background="@drawable/bg_drawline" /> <Button android:id="@+id/clearBut" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清空" /> </LinearLayout>
背景 bg_drawline .xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!-- 描邊--> <stroke android:width="2dp" android:color="#45c01a"></stroke> <!-- 填充顏色 --> <solid android:color="#fff"></solid> <!-- 圓角 --> <corners android:radius="10dp"></corners> </shape>
activity代碼
package com.cktest;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.view.MDrawLineView;
/**
* Created by jiang on 2017/12/25.
*/
public class DrawLineAct extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_drawline);
mDrawLine = (MDrawLineView) findViewById(R.id.mDrawLine);
clearBut = (Button) findViewById(R.id.clearBut);
clearBut.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.clearBut:
mDrawLine.clear();
break;
}
}
private MDrawLineView mDrawLine;
private Button clearBut;
}
總結(jié)
以上所述是小編給大家介紹的Android 實(shí)現(xiàn)銀聯(lián)刷卡機(jī)消費(fèi)后手動(dòng)簽名的功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
android中實(shí)現(xiàn)背景圖片顏色漸變方法
這篇文章主要介紹了android中實(shí)現(xiàn)背景圖片顏色漸變方法,本文直接使用配置文件實(shí)現(xiàn)了這個(gè)效果,需要的朋友可以參考下2015-05-05
Android 播放視頻常見(jiàn)問(wèn)題小結(jié)
這篇文章主要介紹了Android 播放視頻常見(jiàn)問(wèn)題小結(jié),需要的朋友可以參考下2017-04-04
Android利用Intent實(shí)現(xiàn)記事本功能(NotePad)
這篇文章主要為大家詳細(xì)介紹了Android利用Intent實(shí)現(xiàn)簡(jiǎn)單記事本功能(NotePad)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
Android中實(shí)現(xiàn)開(kāi)機(jī)自動(dòng)啟動(dòng)服務(wù)(service)實(shí)例
這篇文章主要介紹了Android中實(shí)現(xiàn)自動(dòng)啟動(dòng)服務(wù)實(shí)例,并開(kāi)機(jī)自動(dòng)啟用(無(wú)activity),的朋友可以參考下2014-06-06
Android?nonTransitiveRClass資源沖突問(wèn)題淺析
這篇文章主要介紹了Android?nonTransitiveRClass資源沖突問(wèn)題,別搞錯(cuò)了,nonTransitiveRClass不能解決資源沖突,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-12-12
Android Kotlin 基本數(shù)據(jù)類型詳解
Kotlin是一種靜態(tài)類型語(yǔ)言,適用于Android開(kāi)發(fā),Kotlin的基本數(shù)據(jù)類型包括數(shù)值類型、字符類型、布爾類型和數(shù)組類型,本文介紹Android Kotlin 基本數(shù)據(jù)類型,感興趣的朋友一起看看吧2025-03-03
Android未讀消息拖動(dòng)氣泡示例代碼詳解(附源碼)
這篇文章主要介紹了Android未讀消息拖動(dòng)氣泡示例代碼詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02

