我用手指做了一个绘图的例子,但之后我想将它保存到SD卡中

时间:2015-08-21 10:05:13

标签: android eclipse

这是我的自定义视图类:

package com.example.drawwithfinger;

public class CustomTextview extends View {

    Paint paint;
    Bitmap bitmap;
    Canvas canvas;
    Path path;
    Paint BitmapPaint;
    Context context;
    private Context Context;
    public CustomTextview(Context context, AttributeSet attrs) {
    super(context, attrs);
    Context=context;
    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setDither(true);
    paint.setColor(0xFFFF0000);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setStrokeWidth(20);
    path = new Path();
    BitmapPaint = new Paint();
    BitmapPaint.setColor(Color.RED);
    }
    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    canvas = new Canvas(bitmap);
    }
    @Override
    public void draw(Canvas canvas) 
    {
    super.draw(canvas);
    canvas.drawBitmap(bitmap, 0, 0, BitmapPaint);
    canvas.drawPath(path, paint);
    }
    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;
    private void touch_start(float x, float y) {
    path.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) {
    path.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
    mX = x;
    mY = y;
    }
    }
    private void touch_up() {
    path.lineTo(mX, mY);
    canvas.drawPath(path, paint);
    }
    @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();
    break;
    }
    return true;
    }

}

这是我在画布上绘制的代码。此代码创建一个应用程序,允许您用手指绘制,我想将手指打印的图像保存到SD卡上。请帮我这样做。解释一下,对于这样的事情,我应该在我的主要活动课中写什么。

1 个答案:

答案 0 :(得分:0)

试试这个......

  YourCustomTextView.setDrawingCacheEnabled(true);
  Bitmap bitmap = content.getDrawingCache();
  File file,f;                    
 if    (android.os.Environment.getExternalStorageState().eq uals(android.os.Environment.MEDIA_MOUNTED)) 
 {  
        file =new   File(android.os.Environment.getExternalStorageDirectory(),"TTImages_cache");
       if(!file.exists())
      {
       file.mkdirs();

       } 
     f = new File(file.getAbsolutePath()+file.seperator+ "filename"+".png");
   }
     FileOutputStream ostream = new    FileOutputStream(f);                                   
     bitmap.compress(CompressFormat.PNG, 10, ostream);
  ostream.close();

} 
  catch (Exception e){
  e.printStackTrace();
 }

主要许可

     <uses-permission. android:name="android.permission.WRITE_EXTERNAL_STORAGE" />