触摸轨迹绘图并保存图片

将某人代码给稍微改变一下,利用双缓冲技术来实现,并将生成图片保存下来。

package ViewTool;


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;


import com.example.zkc.bloodfillform.R;


import sqlitetool.BloodQuestionType;
import android.R.color;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.PathEffect;
import android.os.Environment;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;


public class AutoGraphView extends LinearLayout {


private BloodQuestionType bqt;


MyView mView;


static final int BACKGROUND_COLOR = Color.WHITE;
private Context context;
private String filePath;
private Button bt_ok;


private TextView tv_demo;


private Button bt_clear;
FrameLayout frameLayout;


public AutoGraphView(Context context, BloodQuestionType bqt) {
super(context);
this.bqt = bqt;
this.context = context;
filePath = context.getApplicationContext().getFilesDir()
.getAbsolutePath()
+ "/";
filePath = Environment.getExternalStorageDirectory().toString() + "/";
init();


}


public AutoGraphView(Context context, AttributeSet attrs) {
super(context, attrs);


}


private void init() {
removeAllViews();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.autograph, this);
tv_demo = (TextView) findViewById(R.id.tv_demo);
tv_demo.setText(bqt.getInfoTitle() + "(" + bqt.getInfoDemo() + ")");
bt_ok = (Button) findViewById(R.id.bt_ok);
bt_ok.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {

saveBitmap2file(mView.getCachebBitmap(), "test1.jpg");


}
});


bt_clear = (Button) findViewById(R.id.bt_clear);
bt_clear.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mView.clear();
}
});
mView = new MyView(context);
frameLayout = (FrameLayout) findViewById(R.id.tablet_view);


frameLayout.addView(mView);
// mView.setBackgroundColor(Color.argb(255, 0, 0, 0));
mView.requestFocus();


}
public static Bitmap getViewBitmap(View v) {
       v.clearFocus(); //
       v.setPressed(false); //
       // 能画缓存就返回false
       boolean willNotCache = v.willNotCacheDrawing();
       v.setWillNotCacheDrawing(false);
       int color = v.getDrawingCacheBackgroundColor();
       v.setDrawingCacheBackgroundColor(0);
       if (color != 0) {
           v.destroyDrawingCache();
       }
       v.buildDrawingCache();
       Bitmap cacheBitmap = v.getDrawingCache();
       if (cacheBitmap == null) {
           // Log.e(TAG, "failed getViewBitmap(" + v + ")", new
           // RuntimeException());
           return null;
       }
       Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
       // Restore the view
       v.destroyDrawingCache();
       v.setWillNotCacheDrawing(willNotCache);
       v.setDrawingCacheBackgroundColor(color);
       return bitmap;
   }


public static Bitmap convertViewToBitmap(View view) {
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();


return bitmap;
}


public boolean saveBitmap2file(Bitmap bmp, String filename) {
CompressFormat format = Bitmap.CompressFormat.JPEG;
int quality = 100;
OutputStream stream = null;


try {
stream = new FileOutputStream(filePath + filename);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


return bmp.compress(format, quality, stream);
}


public class MyView extends SurfaceView implements Callback, Runnable {


/** 每50帧刷新一次屏幕 **/
public static final int TIME_IN_FRAME = 50;
private Canvas cacheCanvas;
private Bitmap cachebBitmap;
/** 游戏画笔 **/
Paint mPaint = null;
Paint mTextPaint = null;
SurfaceHolder mSurfaceHolder = null;


/** 控制游戏更新循环 **/
boolean mRunning = false;


/** 游戏画布 **/
Canvas mCanvas = null;


/** 控制游戏循环 **/
boolean mIsRunning = false;


/** 曲线方向 **/
private Path mPath;


private float mposX, mposY;


public MyView(Context context) {
super(context);
/** 设置当前View拥有控制焦点 **/
this.setFocusable(true);
/** 设置当前View拥有触摸事件 **/
this.setFocusableInTouchMode(true);
/** 拿到SurfaceHolder对象 **/
mSurfaceHolder = this.getHolder();
/** 将mSurfaceHolder添加到Callback回调函数中 **/
mSurfaceHolder.addCallback(this);
/** 创建画布 **/
mCanvas = new Canvas();
/** 创建曲线画笔 **/
mPaint = new Paint();
mPaint.setColor(Color.BLACK);
/** 设置画笔抗锯齿 **/
mPaint.setAntiAlias(true);
/** 画笔的类型 **/
mPaint.setStyle(Paint.Style.STROKE);
/** 设置画笔变为圆滑状 **/
mPaint.setStrokeCap(Paint.Cap.ROUND);
/** 设置线的宽度 **/
mPaint.setStrokeWidth(10);
/** 创建路径对象 **/
mPath = new Path();
/** 创建文字画笔 **/
mTextPaint = new Paint();
/** 设置颜色 **/
mTextPaint.setColor(Color.BLACK);
/** 设置文字大小 **/
mTextPaint.setTextSize(15);
cachebBitmap = Bitmap.createBitmap(650, 360, Config.ARGB_8888);
mCanvas.setBitmap(cachebBitmap);
mCanvas.drawColor(Color.WHITE);
cacheCanvas = new Canvas();
cacheCanvas.setBitmap(cachebBitmap);
cacheCanvas.drawColor(Color.WHITE);
}


public void clear() {
mPath.reset();
if (cacheCanvas != null) {


mTextPaint.setColor(BACKGROUND_COLOR);
cacheCanvas.drawPaint(mTextPaint);
mTextPaint.setColor(Color.BLACK);
cacheCanvas.drawColor(Color.WHITE);
invalidate();
}


}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
// super.onDraw(canvas);
canvas.drawBitmap(cachebBitmap, 0, 0, null);
canvas.drawPath(mPath, mTextPaint);
}
public Bitmap getCachebBitmap() {
// cacheCanvas.drawBitmap(cachebBitmap, 0, 0, null);
return cachebBitmap;
}


@Override
public boolean onTouchEvent(MotionEvent event) {
/** 拿到触摸的状态 **/
int action = event.getAction();
float x = event.getX();
float y = event.getY();
switch (action) {
// 触摸按下的事件
case MotionEvent.ACTION_DOWN:
/** 设置曲线轨迹起点 X Y坐标 **/
mPath.moveTo(x, y);

break;
// 触摸移动的事件
case MotionEvent.ACTION_MOVE:
/** 设置曲线轨迹 **/
// 参数1 起始点X坐标
// 参数2 起始点Y坐标
// 参数3 结束点X坐标
// 参数4 结束点Y坐标
mPath.quadTo(mposX, mposY, x, y);

break;
// 触摸抬起的事件
case MotionEvent.ACTION_UP:
/** 按键抬起后清空路径轨迹 **/
cacheCanvas.drawPath(mPath, mPaint);
// mPath.reset();

break;
}
// 记录当前触摸X Y坐标
//

mposX = x;
mposY = y;
return true;
}


private void Draw() {
/** 清空画布 **/
mCanvas.drawColor(Color.WHITE);
/** 绘制曲线 **/
mCanvas.drawPath(mPath, mPaint);


/** 记录当前触点位置 **/
mCanvas.drawText("当前触笔 X:" + mposX, 0, 20, mTextPaint);
mCanvas.drawText("当前触笔 Y:" + mposY, 0, 40, mTextPaint);





}


@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {


}


@Override
public void surfaceCreated(SurfaceHolder holder) {
/** 开始游戏主循环线程 **/
mIsRunning = true;
new Thread(this).start();
}


@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mIsRunning = false;
}


@Override
public void run() {
while (mIsRunning) {


/** 取得更新游戏之前的时间 **/
long startTime = System.currentTimeMillis();


/** 在这里加上线程安全锁 **/
synchronized (mSurfaceHolder) {
/** 拿到当前画布 然后锁定 **/
mCanvas = mSurfaceHolder.lockCanvas();
Draw();
/** 绘制结束后解锁显示在屏幕上 **/
mSurfaceHolder.unlockCanvasAndPost(mCanvas);

}


/** 取得更新游戏结束的时间 **/
long endTime = System.currentTimeMillis();


/** 计算出游戏一次更新的毫秒数 **/
int diffTime = (int) (endTime - startTime);


/** 确保每次更新时间为50帧 **/
while (diffTime <= TIME_IN_FRAME) {
diffTime = (int) (System.currentTimeMillis() - startTime);
/** 线程等待 **/
Thread.yield();
}


}


}




}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值