public class DrawView extends View {
/**
* 签名画笔
*/
private Paint paint;
/**
* 签名画布
*/
private Canvas cacheCanvas;
/**
* 画笔路径
*/
private Path path;
/**
* 缓存图片
*/
private Bitmap cacheBitmap;
/**
* 图片宽度
*/
private int width;
/**
* 图片高度
*/
private int height;
/**
* 手指触摸屏幕时的X,Y坐标
*/
private float xDown, yDown;
/**
* 是否正在绘制
*/
private boolean isDrawing = false;
/**
* 默认画笔颜色
*/
private int paintColor = Color.CYAN;
/**
* 默认画板背景色
*/
private int canvasColor = Color.parseColor("#bbccaa");
public DrawView(Context context, int width, int height) {
super(context);
this.width = width;
this.height = height;
initWedgits();
}
/**
* 初始化组件
*/
private void initWedgits() {
try {
paint = new Paint(Paint.DITHER_FLAG);
// 设置抗锯齿
paint.setAntiAlias(true);
// 设置画笔宽度
paint.setStrokeWidth(3);
paint.setDither(true);
// 设置样式
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
// 画笔颜色
paint.setColor(paintColor);
// 绘制路径
path = new Path();
// 创建空缓存图片
cacheBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
// 把画布内容画到空缓存图片上
cacheCanvas = new Canvas(cacheBitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(canvasColor);
canvas.drawBitmap(cacheBitmap, 0, 0, paint);
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// 记录手指摁下屏幕时的X坐标
final float x = event.getX();
// 记录手指摁下屏幕时的Y坐标
final float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 手指摁下时清空之前的设置
path.reset();
// 设置路径起始点
path.moveTo(x, y);
xDown = x;
yDown = y;
isDrawing = true;
break;
case MotionEvent.ACTION_MOVE:
// 移动下一点
path.quadTo(xDown, yDown, x, y);
// 重新设置起点
xDown = x;
yDown = y;
isDrawing = true;
break;
case MotionEvent.ACTION_UP:
path.lineTo(xDown, yDown);
// 手指抬起时绘制路径
cacheCanvas.drawPath(path, paint);
// 路径重置
path.reset();
isDrawing = false;
break;
default:
break;
}
// 刷新界面
invalidate();
return true;
}
/**
* 设置画笔颜色
*
* @param color
* 画笔颜色
*/
public void setPaintColor(int color) {
paintColor = color;
}
/**
* 设置画板颜色
*
* @param color
* 画板颜色
*/
public void setCanvasColor(int color) {
canvasColor = color;
}
/**
* 返回绘画状态
*
* @return【true:正在绘制】【false:绘制完成】
*/
public boolean getDrawState() {
return isDrawing;
}
/**
* 返回Bitmap
*
* @return 返回绘制的图片
*/
public Bitmap getBitmap() {
return cacheBitmap;
}
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff"> <TextView android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请绘制签名" android:textSize="18sp" android:layout_margin="5dip" android:gravity="center" android:textColor="#000000" /> <FrameLayout android:id="@+id/contents" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:layout_gravity="center" android:background="#aabbcc"> </FrameLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="save" android:text="保存签名" /> </LinearLayout>
public class MainActivity extends Activity {
private FrameLayout frameLayout;
private DrawView drawView;
private TextView title;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initWedgits();
}
/**
* 初始化组件
*/
private void initWedgits() {
try {
frameLayout = (FrameLayout) findViewById(R.id.contents);
title = (TextView) findViewById(R.id.title);
title.setText(Html.fromHtml("<b>China中国<tt>中国</tt></b>China真伟大!"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
drawView = new DrawView(MainActivity.this, frameLayout.getWidth(), frameLayout.getHeight());
frameLayout.addView(drawView);
}
/**
* 保存图片
*
* @param view
*/
public void save(View view) {
try {
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/handle.png");
if (file.exists()) {
file.delete();
}
file.createNewFile();
if (drawView.getBitmap().compress(CompressFormat.PNG, 100, new FileOutputStream(file))) {
Toast.makeText(getApplicationContext(), "图片保存成功", 1000).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有