package com.activity;
import android.app.Activity;
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.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView imageSign;
private PaintView mView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageSign = (ImageView) findViewById(R.id.iv_sign);
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.tablet_view);
mView = new PaintView(this);
frameLayout.addView(mView);
mView.requestFocus();
Button btnClear = (Button) findViewById(R.id.tablet_clear);
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mView.clear();
}
});
Button btnOk = (Button) findViewById(R.id.tablet_ok);
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap imageBitmap = mView.getCachebBitmap();
imageSign.setImageBitmap(imageBitmap);
}
});
}
class PaintView extends View {
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);
paint.setStrokeWidth(3);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLACK);
path = new Path();
cachebBitmap = Bitmap.createBitmap(10, 10, Config.ARGB_8888);
cacheCanvas = new Canvas(cachebBitmap);
cacheCanvas.drawColor(Color.WHITE);
}
public void clear() {
if (cacheCanvas != null) {
paint.setColor(Color.WHITE);
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;
}
private float cur_x, cur_y;
@Override
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;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
SignatureDemo.zip (39个子文件)
SignatureDemo
.project 849B
project.properties 377B
proguard.cfg 1KB
src
com
activity
MainActivity.java 4KB
AndroidManifest.xml 909B
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 3KB
layout
main.xml 1KB
write_pad.xml 2KB
drawable-xhdpi
values
colors.xml 1KB
strings.xml 188B
.settings
org.eclipse.jdt.core.prefs 177B
assets
gen
com
activity
BuildConfig.java 154B
R.java 2KB
.classpath 475B
bin
resources.ap_ 13KB
classes
com
activity
R$layout.class 409B
BuildConfig.class 333B
R$drawable.class 575B
R$attr.class 322B
R$id.class 508B
MainActivity$PaintView.class 3KB
MainActivity$2.class 1KB
R.class 531B
R$string.class 409B
MainActivity$1.class 922B
MainActivity.class 2KB
R$color.class 1017B
AndroidManifest.xml 909B
dexedLibs
HandWritingDemo.apk 20KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 2KB
crunch
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 2KB
classes.dex 9KB
handwriting.apk 21KB
共 39 条
- 1
随意而然
- 粉丝: 89
- 资源: 79
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页