package com.bluedatax.w65.chart;
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.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by bdx108 on 15/12/10.
*/
public class SignView extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder mSurfaceHolder = null;
private Paint mPaint;
private MyPath myPath = null;
private List myPaths;
private int currentColor = Color.BLACK;
private int currentSize = 5;
private Bitmap bmp;
public SignView(Context context) {
super(context);
init();
}
public SignView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Canvas canvas = mSurfaceHolder.lockCanvas();
canvas.drawColor(Color.WHITE);
mSurfaceHolder.unlockCanvasAndPost(canvas);
myPaths = new ArrayList();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
private void init() {
mSurfaceHolder = this.getHolder();
mSurfaceHolder.addCallback(this);
this.setFocusable(true);
mPaint = new Paint();
mPaint.setColor(Color.WHITE);
mPaint.setStrokeWidth(6);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_CANCEL) {
return false;
}
float touchX = event.getRawX();
float touchY = event.getRawY();
switch (action) {
case MotionEvent.ACTION_DOWN:
setCurAction(touchX, touchY);
break;
case MotionEvent.ACTION_MOVE:
Canvas canvas = mSurfaceHolder.lockCanvas();
canvas.drawColor(Color.WHITE);
for (MyPath a : myPaths) {
a.draw(canvas);
}
myPath.move(touchX, touchY);
myPath.draw(canvas);
mSurfaceHolder.unlockCanvasAndPost(canvas);
break;
case MotionEvent.ACTION_UP:
myPaths.add(myPath);
myPath = null;
break;
default:
break;
}
return super.onTouchEvent(event);
}
public void setCurAction(float x, float y) {
myPath = new MyPath(x, y, currentSize, currentColor);
}
public boolean back(){
myPaths.clear();
Canvas canvas = mSurfaceHolder.lockCanvas();
canvas.drawColor(Color.WHITE);
mSurfaceHolder.unlockCanvasAndPost(canvas);
return true;
}
/**
* 获取画布的截图
* @return
*/
public Bitmap getBitmap() {
bmp = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
draw(canvas);
return bmp;
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
canvas.drawColor(Color.TRANSPARENT);
for (MyPath a : myPaths) {
a.draw(canvas);
}
canvas.drawBitmap(bmp, 0, 0, mPaint);
}
}
class MyPath {
Path path;
int size;
Paint paint;
MyPath(float x, float y, int size, int color) {
path = new Path();
paint = new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(size);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
this.size = size;
path.moveTo(x, y);
path.lineTo(x, y);
}
public void draw(Canvas canvas) {
canvas.drawPath(path, paint);
}
public void move(float mx, float my) {
path.lineTo(mx, my);
}
}
package com.bluedatax.w65.utils.sign;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import com.bluedatax.w65.BaseActivity;
import com.bluedatax.w65.R;
import com.bluedatax.w65.chart.SignView;
/**
* Created by bdx108 on 15/12/11.
*/
public class Sign extends BaseActivity implements View.OnClickListener{
private SignView mMyView;
private Button mButtonClear;
private Button mButtonSave;
private ImageView mImageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign);
mMyView= (SignView) findViewById(R.id.myview);
mButtonClear= (Button) findViewById(R.id.clear);
mButtonClear.setOnClickListener(this);
mButtonSave= (Button) findViewById(R.id.save);
mButtonSave.setOnClickListener(this);
mImageView= (ImageView) findViewById(R.id.image_sign);
String path=Environment.getExternalStorageDirectory()+ "/w65/sign/"+ "sign.png";
if (path!=null){
mImageView.setImageBitmap(getDiskBitmap(path));
Toast.makeText(getApplicationContext(),path,Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(),"zanshi没有照片",Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return mMyView.onTouchEvent(event);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.clear:
mMyView.back();
break;
case R.id.save:
saveSign();
mImageView.setImageBitmap(mMyView.getBitmap());
break;
default:
break;
}
}
private File saveSign() {
String path = Environment.getExternalStorageDirectory().toString()+ "/w65/sign/";
if (!new File(path).exists()) {
new File(path).mkdirs();
}
File signFile=new File(path+"sign.png");
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(signFile));
mMyView.getBitmap().compress(Bitmap.CompressFormat.PNG, 80, bos);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(this, "图片保存成功,路径为" + path, Toast.LENGTH_LONG).show();
Log.d("12345678",path.toString());
return signFile;
}
public void saveBitmapToJPG(Bitmap bitmap, File photo) throws IOException {
Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(bitmap, 0, 0, null);
OutputStream stream = new FileOutputStream(photo);
newBitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
stream.close();
}
private Bitmap getDiskBitmap(String pathString)
{
Bitmap bitmap = null;
try
{
File file = new File(pathString);
if(file.exists())
{
bitmap = BitmapFactory.decodeFile(pathString);
}
} catch (Exception e)
{
}
return bitmap;
}
}