2013-08-31 136 views
-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; 
    } 


} 
+0

要在外部存儲來存儲? – Raghunandan

+0

@Miklosflame和你的問題真的是......? – pskink

+0

@pskink我不知道如何將它保存爲位圖。我忘了提及,我是新來的android和我發現無法實現它沒有崩潰。 – Miklosflame

回答

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

    }