效果图:
开始在网上找的这个签名的功能,然后发觉画出来的线条不顺滑,之前偶然间看到贝塞尔曲线的一片博客,链接:http://blog.csdn.net/eclipsexys/article/details/51956908,然后将贝塞尔给添加进去,就完成了现在这种效果。
上代码:
import android.app.AlertDialog;
import android.app.Dialog;
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.Point;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
static final int BACKGROUND_COLOR = Color.WHITE;
PaintView mView;
Display d;
Point size;
private Button button;
private ImageView image;
private Dialog mCustomViewDialog = null;
private float mX;
private float mY;
private float offset;
private Bitmap mSignBitmap;
private String signPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
image = (ImageView) findViewById(R.id.image);
offset = ViewConfiguration.get(this).getScaledTouchSlop();
WindowManager m = getWindowManager();
d = m.getDefaultDisplay(); // 获取屏幕宽、高用
size = new Point();
this.getWindowManager().getDefaultDisplay().getSize(size);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:
setCustomViewDialog("签名", getApplication(), size, new DialogListener() {
@Override
public void refreshActivity(Object object) {
mSignBitmap = (Bitmap) object;
signPath = createFile();
//可将图片保存下来
image.setImageBitmap(mSignBitmap);
}
});
break;
}
}
/**
* 创建手写签名文件
*
* @return
*/
private String createFile() {
ByteArrayOutputStream baos = null;
String _path = null;
try {
String sign_dir = Environment.getExternalStorageDirectory() + File.separator;
_path = sign_dir + System.currentTimeMillis() + ".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;
}
/**
* 自定义View对话框
*/
public void setCustomViewDialog(String strTitle, Context context, Point size, final DialogListener dialogListener) {
LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数值 ;
p.width = (int) (size.x * 0.8);
p.height = (int) (size.y * 0.5);
mView = new PaintView(context, p);
final View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_login_layout, null);
TextView dialog_title = (TextView) contentView.findViewById(R.id.dialog_title);
dialog_title.setText(strTitle);
Button quxiao = (Button) contentView.findViewById(R.id.btn_no);
Button queding = (Button) contentView.findViewById(R.id.btn_ok);
Button clear = (Button) contentView.findViewById(R.id.btn_qing);
FrameLayout framelayout = (FrameLayout) contentView.findViewById(R.id.framelayout);
framelayout.addView(mView);
mView.requestFocus();
queding.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialogListener.refreshActivity(mView.getCachebBitmap());
mCustomViewDialog.dismiss();
}
});
quxiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCustomViewDialog.dismiss();
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mView.clear();
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(contentView);
mCustomViewDialog = builder.create();
//不关闭写法
mCustomViewDialog.setCanceledOnTouchOutside(false);
mCustomViewDialog.show();
}
public interface DialogListener {
void refreshActivity(Object object);
}
/**
* This view implements the drawing canvas.
* <p/>
* It handles all of the input events and drawing functions.
*/
class PaintView extends View {
private Paint paint;
private Canvas cacheCanvas;
private Bitmap cachebBitmap;
private Path path;
public PaintView(Context context, LayoutParams p) {
super(context);
init(p);
}
public Bitmap getCachebBitmap() {
return cachebBitmap;
}
private void init(LayoutParams p) {
paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLACK);
path = new Path();
cachebBitmap = Bitmap.createBitmap(p.width, (int) (p.height * 0.8), Bitmap.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();
}
}
@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, Bitmap.Config.ARGB_8888);
Canvas newCanvas = new Canvas();
newCanvas.setBitmap(newBitmap);
if (cachebBitmap != null) {
newCanvas.drawBitmap(cachebBitmap, 0, 0, null);
}
cachebBitmap = newBitmap;
cacheCanvas = newCanvas;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
float x = event.getX();
float y = event.getY();
mX = x;
mY = y;
path.moveTo(x, y);
break;
}
case MotionEvent.ACTION_MOVE: {
float x1 = event.getX();
float y1 = event.getY();
float preX = mX;
float preY = mY;
float dx = Math.abs(x1 - preX);
float dy = Math.abs(y1 - preY);
if (dx >= offset || dy >= offset) {
// 贝塞尔曲线的控制点为起点和终点的中点
float cX = (x1 + preX) / 2;
float cY = (y1 + preY) / 2;
path.quadTo(preX, preY, cX, cY);
mX = x1;
mY = y1;
}
invalidate();
break;
}
case MotionEvent.ACTION_UP: {
cacheCanvas.drawPath(path, paint);
path.reset();
break;
}
}
return true;
}
}
}