-1

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

这是绘图类:

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;
    }


}
4

1 回答 1

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();
            }

  }
于 2013-08-31T18:10:05.810 回答