保存在android的fingerPaint API演示由用户图像 [英] Save the image made by user in fingerPaint api demos in android
问题描述
嘿,伙计们 我想建立一个签名任务的应用程序。 在其中用户将创建触摸的签名和该位图应保存在手机中。 我创建了具有自定义视图一样之一fingerpaint应用,并呼吁main.xml中的文件的视图2类文件之一。
在我的主应用程序类文件,我有菜单按钮,在点击保存位图的SD卡。以下是code: -
包org.testCircle;
进口android.app.Activity;
进口android.graphics.Bitmap;
进口android.graphics.Canvas;
进口android.os.Bundle;
进口android.provider.MediaStore.Images;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.widget.TextView;
进口android.widget.Toast;
公共类testCircle延伸活动{
TextView的电视;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
//的setContentView(新customView(本));
的setContentView(R.layout.main);
}
公共布尔onCreateOptionsMenu(功能菜单){
super.onCreateOptionsMenu(菜单);
menu.add(0,1,0,保存)setShortcut('3','c')中。
返回true;
}
公共布尔prepareOptionsMenu(功能菜单)在{
super.on prepareOptionsMenu(菜单);
返回true;
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
情况1:
//新ColorPickerDialog(这个,这个,mPaint.getColor())显示()。
fingerPaint CV =新fingerPaint(本);
位图viewBitmap = Bitmap.createBitmap(480,800,Bitmap.Config.ARGB_8888);
帆布油画=新的Canvas(viewBitmap);
cv.draw(画布);
字符串URL = Images.Media.insertImage(getContentResolver(),viewBitmap,称号,空);
Toast.makeText(testCircle.this,网址,Toast.LENGTH_LONG).show();
返回true;
}
返回super.onOptionsItemSelected(项目);
}
}
自定义视图: -
包org.testCircle;
进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.Canvas;
进口android.graphics.Paint;
进口android.graphics.Path;
进口android.os.Bundle;
进口android.util.AttributeSet;
进口android.view.MotionEvent;
进口android.view.View;
公共类fingerPaint扩展视图{
涂料mPaint;
私有静态最终浮动MINP = 0.25f;
私有静态最终浮动MAXP = 0.75f;
私有静态位图mBitmap;
私人帆布mCanvas;
私人路径的mpath;
私人油漆mBitmapPaint;
公共fingerPaint(上下文C){
超级(C);
mPaint =新的油漆();
mPaint.setAntiAlias(真正的);
mPaint.setDither(真正的);
mPaint.setColor(0xFFFF0000地址);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
mBitmap = Bitmap.createBitmap(320,480,Bitmap.Config.ARGB_8888);
mCanvas =新的Canvas(mBitmap);
的mpath =新路径();
mBitmapPaint =新的油漆(Paint.DITHER_FLAG);
}
公共fingerPaint(上下文C,AttributeSet中的ATTRS){
超级(C,ATTRS);
mPaint =新的油漆();
mPaint.setAntiAlias(真正的);
mPaint.setDither(真正的);
mPaint.setColor(0xFFFF0000地址);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
mBitmap = Bitmap.createBitmap(320,480,Bitmap.Config.ARGB_8888);
mCanvas =新的Canvas(mBitmap);
的mpath =新路径();
mBitmapPaint =新的油漆(Paint.DITHER_FLAG);
}
公共无效onerase(){
mCanvas = NULL;
}
@覆盖
保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
super.onSizeChanged(W,H,oldw,oldh);
}
@覆盖
保护无效的OnDraw(帆布油画){
canvas.drawColor(0xFFAAAAAA);
canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);
canvas.drawPath(的mpath,mPaint);
}
私人浮动MX,我的;
私有静态最终浮动TOUCH_TOLERANCE = 4;
私人无效touch_start(浮X,浮动Y){
mPath.reset();
mPath.moveTo(X,Y);
MX = X;
我= Y;
}
私人无效TOUCH_MOVE(浮X,浮动Y){
浮DX = Math.abs(X - MX);
浮DY = Math.abs(Y - 我的);
如果(DX> = TOUCH_TOLERANCE || DY> = TOUCH_TOLERANCE){
mPath.quadTo(MX,MY,(X + MX)/ 2,(Y +我)/ 2);
MX = X;
我= Y;
}
mCanvas.drawBitmap(mBitmap,MX,我,mPaint);
}
私人无效touch_up(){
mPath.lineTo(MX,MY);
//提交路径,我们的屏幕外
mCanvas.drawPath(的mpath,mPaint);
//杀了这个,所以我们不重复抽奖
mPath.reset();
}
@覆盖
公共布尔的onTouchEvent(MotionEvent事件){
浮X = event.getX();
浮动Y = event.getY();
开关(event.getAction()){
案例MotionEvent.ACTION_DOWN:
touch_start(X,Y);
无效();
打破;
案例MotionEvent.ACTION_MOVE:
TOUCH_MOVE(X,Y);
无效();
打破;
案例MotionEvent.ACTION_UP:
润色();
无效();
叠B =新包();
打破;
}
返回true;
}
}
下面是源$ C $ C要做到这一点
的LinearLayout V =(的LinearLayout)findViewById(R.id.mainLayout);
v.setDrawingCacheEnabled(真正的);
//这是最重要的code :)
//如果没有它的视图将有一个
// 0,0尺寸,将位图
//为空
v.measure(MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED),MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));
v.layout(0,0,v.getWidth(),v.getHeight());
v.buildDrawingCache(真正的);
位图BM = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(假);
如果(BM!= NULL){
尝试 {
。字符串路径= Environment.getExternalStorageDirectory()的toString();
OutputStream的FOUT = NULL;
档案文件=新的文件(路径,screentest.jpg);
FOUT =新的FileOutputStream(文件);
bm.com preSS(Bitmap.Com pressFormat.JPEG,85,FOUT);
fOut.flush();
fOut.close();
Log.e(的ImagePath,映像路径:+ MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName()));
}
赶上(例外五){
e.printStackTrace();
}
}
希望这将与您一起还。
Hey guys I am trying to build a signature task application . In which the user will create the signature on touch and that bitmap should be saved in the phone. I have created 2 class files one having the custom view the same as the one in fingerpaint application and calling the view in main.xml file.
In my main application class file i have menu button which on click saves the bitmap in sdcard . The following is the code :-
package org.testCircle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class testCircle extends Activity {
TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(new customView(this));
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, 1, 0, "save").setShortcut('3', 'c');
return true;
}
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
//new ColorPickerDialog(this, this, mPaint.getColor()).show();
fingerPaint cv = new fingerPaint(this);
Bitmap viewBitmap = Bitmap.createBitmap(480, 800,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(viewBitmap);
cv.draw(canvas);
String url = Images.Media.insertImage(getContentResolver(), viewBitmap, "title", null);
Toast.makeText(testCircle.this, url, Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Custom View :-
package org.testCircle;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class fingerPaint extends View {
Paint mPaint;
private static final float MINP = 0.25f;
private static final float MAXP = 0.75f;
private static Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
public fingerPaint(Context c) {
super(c);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFF0000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}
public fingerPaint(Context c , AttributeSet attrs){
super(c , attrs);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFF0000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}
public void onerase(){
mCanvas=null;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFAAAAAA);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
mCanvas.drawBitmap(mBitmap, mX, mY, mPaint);
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
Bundle b = new Bundle();
break;
}
return true;
}
}
Here is the source code to do that
LinearLayout v = (LinearLayout) findViewById(R.id.mainLayout);
v.setDrawingCacheEnabled(true);
// this is the important code :)
// Without it the view will have a
// dimension of 0,0 and the bitmap will
// be null
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getWidth(), v.getHeight());
v.buildDrawingCache(true);
Bitmap bm = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
if (bm != null) {
try {
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "screentest.jpg");
fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
Log.e("ImagePath", "Image Path : " + MediaStore.Images.Media.insertImage( getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName()));
}
catch (Exception e) {
e.printStackTrace();
}
}
Hope this will work with you also.
这篇关于保存在android的fingerPaint API演示由用户图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!