android上的一个签名功能,先上效果图
SignActivity.java
package com.cool.hello.sign;
import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.cool.hello.R;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.security.Permission;
public class SignActivity extends AppCompatActivity {
private TouchView mTouchView;
private Bitmap bitmap;
String imagePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign);
initView();
File externalFilesDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
imagePath = externalFilesDir.getAbsolutePath();
}
private void initView() {
mTouchView = (TouchView) findViewById(R.id.touchView);
}
/event handler//
/**
* 保存
* @param view
*/
public void save(View view){
bitmap = mTouchView.getBitmap();
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
saveToSdCard();
}else {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
}
}
private void saveToSdCard() {
File file = new File(imagePath + File.separator + "sign.jpg");
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG,70,fileOutputStream);
Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 清空
* @param view
*/
public void clear(View view){
mTouchView.clear();
}
/**
* 取消
* @param view
*/
public void cancel(View view){
mTouchView.clear();
finish();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
saveToSdCard();
}else {
Toast.makeText(this,"授权失败",Toast.LENGTH_SHORT).show();
}
}
}
activity_sign.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:orientation="vertical">
<com.cool.hello.sign.TouchView
android:id="@+id/touchView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="save"
android:text="保存" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="clear"
android:text="清空" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="cancel"
android:text="取消" />
</LinearLayout>
</LinearLayout>
TouchView.java
package com.cool.hello.sign;
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.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by cool on 2017/2/7.
*/
public class TouchView extends View {
private int mWidth;//屏幕的宽
private int mHight;//屏幕的高
private Paint mPaint;
private Path mPath;
private Bitmap mBitmap;
private Canvas mCanvas;
public TouchView(Context context) {
this(context, null);
}
public TouchView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TouchView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mPaint = new Paint();
mPaint.setAntiAlias(true); // 去除锯齿
mPaint.setStrokeWidth(5);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.BLACK);
mPath = new Path();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap,0,0,null);
canvas.drawPath(mPath,mPaint);
}
float startX;
float startY;
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
mPath.moveTo(startX,startY);
break;
case MotionEvent.ACTION_MOVE:
float currentX = event.getX();
float currentY = event.getY();
mPath.lineTo(currentX,currentY);
break;
case MotionEvent.ACTION_UP:
mCanvas.drawPath(mPath,mPaint);
break;
}
invalidate();
return true;
}
public Bitmap getBitmap(){
return mBitmap;
}
public void clear(){
if(mCanvas != null){
mPath.reset();
mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
invalidate();
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mWidth = w;
mHight = h;
mBitmap = Bitmap.createBitmap(mWidth, mHight, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mCanvas.drawColor(Color.WHITE);
}
}