我想将画布绘图保存为位图图像

时间:2013-08-31 17:14:18

标签: android canvas bitmap drawing

在这个类中,我使用画布绘制,我想将它保存到设备上的位图图像,以便稍后查看。此外,我对位置感到担心,因为我计划在同一应用程序的库视图中打开图像。

这是Drawing类:

public class Drawing extends View {

    private Paint paint = new Paint();
    private Path path = new Path();

    private ArrayList<Paint> paints = new ArrayList<Paint>();
    private ArrayList<Path> paths = new ArrayList<Path>();
    private int moodColor, brushColor = Color.WHITE;
    Context context;

    public Drawing(Context context) {
        this( context, null);
        context = this.context;
        paints.add(paint);
        paths.add(path);
    }

    public void setMoodColor(int moodColor) {
        this.moodColor = moodColor;
        switch (moodColor)
        {
        case 1:
        {
            brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.tense);
            Toast.makeText(context, brushColor, Toast.LENGTH_SHORT).show();
        }
        case 2:
        {
            brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.nervous);
        }
        case 3:
        {
            brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.stressed);
        }
        case 4:
        {
            brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.upset);
        }
        case 5:
        {
            brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.alert);
        }
        case 6:
        {
            brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.excited);
        }
        case 7:
        {
            brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.elated);
        }
        case 8:
        {
            brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.happy);
        }
        case 9:
        {
            brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.sad);
        }
        case 10:
        {
            brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.depressed);
        }
        case 11:
        {
            brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.bored);
        }
        case 12:
        {
            brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.fatigued);
        }
        case 13:
        {
            brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.contented);
        }
        case 14:
        {
            brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.serene);
        }
        case 15:
        {
            brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.relaxed);
        }
        case 16:
        {
            brushColor = context.getResources().getColor(com.tibike.doodleme.R.color.calm);
        }
        default:
            brushColor = Color.BLACK;
            //Toast.makeText(context, "Selected " + brushColor, Toast.LENGTH_SHORT).show();
        }
        paint = new Paint();
        paint.setAntiAlias(true); 
        paint.setStrokeWidth(5f);
        paint.setStyle(Paint.Style.STROKE); 
        paint.setStrokeJoin(Paint.Join.ROUND); 
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setColor(brushColor);
        path = new Path(); 
        paints.add(paint);
        paths.add(path);
    }



    public Drawing(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setBackgroundColor(Color.WHITE);
        paint.setColor(brushColor);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(5f);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        Toast.makeText(context, "Click on 'Mood' to choose color", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawPath(path, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float eventX = event.getX(); 
        float eventY = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                path.moveTo(eventX, eventY); return true;
            case MotionEvent.ACTION_MOVE:
                path.lineTo(eventX, eventY); break;
            default:
                return false;
    }
    invalidate(); return true;
    }


}

1 个答案:

答案 0 :(得分:1)

将其存储在外部存储空间

在清单中添加权限

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

然后

DrawingView dv;
dv = new DrawingView(ActivityName.this);
dv.setDrawingCacheEnabled(true);
save();

保存

public void save()
      {
       Bitmap bitmap = dv.getDrawingCache();    
       String path = Environment.getExternalStorageDirectory().getAbsolutePath(); 
                File file = new File(path+File.separator+"name"+".png");    
              Toast.makeText(getApplicationContext(), file.getAbsolutePath(),Toast.LENGTH_LONG).show();
                try 
                {
                    if(!file.exists())

                {
                    file.createNewFile();
                }
                    FileOutputStream ostream = new FileOutputStream(file);
                    bitmap.compress(CompressFormat.PNG, 10, ostream);

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

      }

存储内置手机内存

http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

您可以直接在设备的内部存储上保存文件。默认情况下,保存到内部存储的文件对应用程序是私有的,而其他应用程序无法访问它们(用户也无法访问)。当用户卸载您的应用程序时,将删除这些文件。

DrawingView dv;
dv = new DrawingView(ActivityName.this);
dv.setDrawingCacheEnabled(true);
save();

保存

    public void save()
    {
      Bitmap bitmap = dv.getDrawingCache();

        try 
            {

                FileOutputStream fos = openFileOutput("name.png", Context.MODE_PRIVATE);
                bitmap.compress(CompressFormat.PNG, 10, fos);

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

  }