Android签名功能的实现

当你需要绘制某些东西的时候,你实际上需要四个基本组件:
  1.一个Bitmap来存放像素。
  2.一个Canvas来调用绘制函数,向位图中写入内容。
  3.要绘制的基本图元,如矩形(Rect), 路径(Path), 位图(BitMap),或者文字等
  4.一个画笔(Paint),指定了绘制时所用的颜色和样式等。

       Android中提供了一系列二维图绘制的API。 我们可以根据上诉思路和API结合来搞定签名功能。下面我将自己写的实现代码贴出来给大家参考。

自定义View:

package com.dengyy.signaturesdemo;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

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.os.Environment;
import android.view.MotionEvent;
import android.view.View;

/**
 * 签名处理类 
 * dengyy
 */

public class PaintView extends View {
	static final int BACKGROUND_COLOR = Color.WHITE;
	static final int BRUSH_COLOR = Color.BLACK;

	/** 签名桌布尺寸 */
	public static final int SIGN_IMG_WIDTH = 800;
	public static final int SIGN_IMG_HEIGHT = 600;

	private Paint paint;
	private Canvas cacheCanvas;
	private Bitmap cachebBitmap;
	private Path path;

	public Bitmap getCachebBitmap() {
		return cachebBitmap;
	}

	public PaintView(Context context) {
		super(context);
		init();
	}

	private void init() {
		// 创建画笔
		paint = new Paint();
		// 设置是否使用抗锯齿功能,会消耗较大资源,绘制图形速度会变慢。
		paint.setAntiAlias(true);
		// 当画笔样式为STROKE或FILL_OR_STROKE时,设置笔刷的粗细度
		paint.setStrokeWidth(3);
		// 设置画笔的样式,为FILL,FILL_OR_STROKE,或STROKE
		paint.setStyle(Paint.Style.STROKE);
		// 设置绘制的颜色,使用颜色值来表示,该颜色值包括透明度和RGB颜色。
		paint.setColor(Color.BLACK);
		path = new Path();
		cachebBitmap = Bitmap.createBitmap(SIGN_IMG_WIDTH, SIGN_IMG_HEIGHT, Config.ARGB_8888);
		cacheCanvas = new Canvas(cachebBitmap);
		cacheCanvas.drawColor(Color.WHITE);
	}

	public void clear() {
		if (cacheCanvas != null) {
			paint.setColor(BACKGROUND_COLOR);
			cacheCanvas.drawPaint(paint);
			paint.setColor(Color.BLACK);
			cacheCanvas.drawColor(Color.WHITE);
			invalidate();
		}
	}

	protected void onDraw(Canvas canvas) {
		canvas.drawBitmap(cachebBitmap, 0, 0, null);
		canvas.drawPath(path, paint);
	}

	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;

	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;
	}

	/**
	 * 创建手写签名文件
	 * 
	 * @return
	 */
	public String createFile(Bitmap mSignBitmap) {
		ByteArrayOutputStream baos = null;
		String _path = null;
		try {
			String sign_dir = Environment.getExternalStorageDirectory() + File.separator;
			_path = sign_dir + System.currentTimeMillis() + ".png";
			baos = new ByteArrayOutputStream();
			mSignBitmap.compress(Bitmap.CompressFormat.JPEG, 30, 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;
	}
}

 MainActivity:

package com.dengyy.signaturesdemo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

public class MainActivity extends Activity {
	
	private ImageView img_sign;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		img_sign = (ImageView) findViewById(R.id.img_sign);
		
		findViewById(R.id.iv_sign).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				startActivityForResult(new Intent(MainActivity.this,ClientSignatureActivity.class),100);				
			}
		});
		
	}

	/**
	 * 获取签名的图片信息
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// resultCode
		if(resultCode==100){
			if(data.hasExtra("path")){
				//拿到路径
				String path = data.getStringExtra("path");
				//利用BitmapFactory.Options减少图片加载的像素
				BitmapFactory.Options options = new BitmapFactory.Options();
//				options.inSampleSize = 50;
				options.inTempStorage = new byte[5*1024];
				//读取Bitmap
				Bitmap zoombm = BitmapFactory.decodeFile(path, options);
				img_sign.setImageBitmap(zoombm);
			}
		}
	}
}

ClientSignatureActivity :

package com.dengyy.signaturesdemo;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;

public class ClientSignatureActivity extends Activity{

	private Button sign_ok;
	private Button sign_clean;
	private FrameLayout tablet_view;
	
	private PaintView mView;
	
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		requestWindowFeature(Window.FEATURE_PROGRESS);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.signature);
		
		initView();
		
		// 赋值视图
		tablet_view.addView(mView);
		mView.clear();
		// 指定屏幕内的焦点View
		mView.requestFocus();
		mView.clear();
		
		sign_ok.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent data = new Intent();
				String path = mView.createFile(mView.getCachebBitmap());
				data.putExtra("path", path);
				ClientSignatureActivity.this.setResult(100,data);
				ClientSignatureActivity.this.finish();
			}
		});
		
		sign_clean.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(mView!=null)
					mView.clear();
			}
		});
		
	}

	private void initView() {
		//取得窗口属性  
		DisplayMetrics  d = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(d);
		
		sign_ok = (Button) findViewById(R.id.sign_ok);
		sign_clean = (Button) findViewById(R.id.sign_clean);
		tablet_view = (FrameLayout) findViewById(R.id.tablet_view);
		
		mView = new PaintView(this);
		
	}
	
}

布局文件:activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.dengyy.signaturesdemo.MainActivity"
    android:gravity="center"
     >

    <LinearLayout
        android:id="@+id/iv_sign"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/sign"
        android:orientation="vertical"
        android:paddingBottom="3dp"
        android:paddingTop="3dp" >

        <ImageView
            android:id="@+id/img_sign"
            android:layout_width="fill_parent"
            android:layout_height="150dp"
            android:layout_gravity="center" />
    </LinearLayout>

</RelativeLayout>


布局文件: signature.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="fill_parent"
    android:background="#e9e9e9"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_marginBottom="0dp"
        android:background="#ffffff" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="确认签名"
            android:textColor="#000000"
            android:textSize="17sp" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/a"
        android:layout_width="fill_parent"
        android:layout_height="300dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="15dp" >

        <FrameLayout
            android:id="@+id/tablet_view"
            android:layout_width="fill_parent"
            android:layout_height="300dp" >
        </FrameLayout>
    </LinearLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/sign_ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:background="@drawable/tangerine_btn"
            android:text="确认"
            android:textColor="#ffffff" />

        <Button
            android:id="@+id/sign_clean"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:background="@drawable/white_btn"
            android:text="清除"
            android:textColor="#000000" />
    </RelativeLayout>

</LinearLayout>

最后记得在AndroidManifest中加入读写SD卡的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>  

运行图如下:



举一反三 通过修改背景色可以实现刮刮乐功能 

源码可以点击此处下载

猜你喜欢

转载自blog.csdn.net/dengyanyi11/article/details/46874619