大家好
我正在尝试构建一个签名任务应用程序.
用户将在其中创建触摸签名,该位图应保存在手机中.
我创建了2个类文件,其中一个自定义视图与fingerpaint应用程序中的相同,并调用main.xml文件中的视图.
在我的主应用程序类文件中,我有菜单按钮,在单击时保存sdcard中的位图.以下是代码: –
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);}}
自定义视图: –
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; }}
解决方法:
这是执行此 *** 作的源代码
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 nullv.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(); }}
希望这也适合你.
总结以上是内存溢出为你收集整理的在android中的fingerPaint api演示中保存用户制作的图像全部内容,希望文章能够帮你解决在android中的fingerPaint api演示中保存用户制作的图像所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)