2013-02-28 90 views
1

我正在編寫一個應用程序,用戶可以從該圖庫中選擇一個圖像,並使用DrawingSurface在其上選擇畫筆,現在我想採用該程序的截圖。我已經完成了一個活動的屏幕截圖,但我無法截取DrawingSurface的屏幕截圖。下面是我的XML代碼如何以編程方式截取DrawingSurface的屏幕快照

<RelativeLayout 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 

<Holi_app.in.DrawingSurface 
    android:id="@+id/drawingSurface" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 


</LinearLayout> 

</RelativeLayout> 

這是我的Java類

public class DrawingSurface extends SurfaceView implements SurfaceHolder.Callback 
{ 


private Boolean _run; 
protected DrawThread thread; 
Bitmap mBitmap; 
Context context; 
int flag=0; 
String mImagePath; 
File file; 
String savedFilePath = ""; 
private boolean isFileAlreadySaved = false; 

private CommandManager commandManager; 
private Canvas canvas; 

SurfaceView v; 

public DrawingSurface(Context context, AttributeSet attrs) 
{ 
    super(context, attrs); 

    String tag=PreferenceConnector.readString(context, PreferenceConnector.IMAGE_TAG,"ab"); 

    if(tag.equals("ab")) 
    { 
     mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.abdraw); 
    } 


    getHolder().addCallback(this); 


    commandManager = new CommandManager(); 
    thread = new DrawThread(getHolder()); 


} 


class DrawThread extends Thread 
{ 
    private SurfaceHolder mSurfaceHolder; 


    public DrawThread(SurfaceHolder surfaceHolder) 
    { 
     mSurfaceHolder = surfaceHolder; 

    } 

    public void setRunning(boolean run) 
    { 
     _run = run; 
    } 


    DrawingActivity image=new DrawingActivity(); 

    @Override 
    public void run() 
    { 
     Canvas canvas = null; 

     while (_run) 
     { 
      try 
      {     



       canvas = mSurfaceHolder.lockCanvas(null);     
       canvas.drawBitmap(mBitmap, 0, 0, null); 
       commandManager.executeAll(canvas); 
        mSurfaceHolder.unlockCanvasAndPost(canvas); 
      } 
      catch(Exception e) 
      { 
       e.printStackTrace(); 
      } 

     } 

    } 


} 


public void addDrawingPath (DrawingPath drawingPath) 
{ 
    commandManager.addCommand(drawingPath); 
} 

public boolean hasMoreRedo() 
{ 
    return commandManager.hasMoreRedo(); 
} 

public void redo() 
{ 
    commandManager.redo(); 
} 

public void undo() 
{ 
    commandManager.undo(); 
} 

public boolean hasMoreUndo() 
{ 
    return commandManager.hasMoreRedo(); 
} 


public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
{ 
    // TODO Auto-generated method stub 
} 

public void surfaceCreated(SurfaceHolder holder) 
{ 
    // TODO Auto-generated method stub 
    thread.setRunning(true); 
    thread.start(); 
} 

public void surfaceDestroyed(SurfaceHolder holder) 
{ 
    // TODO Auto-generated method stub 
    boolean retry = true; 
    thread.setRunning(false); 
    while (retry) 
    { 
     try 
     { 
      thread.join(); 
      retry = false; 
     } 
     catch (InterruptedException e) 
     { 
      // we will try it again and again... 
     } 
    } 
} 
public void Save(String mImagePath, String savedFilePath2) 
{ 

    this.mImagePath=mImagePath; 
    this.savedFilePath=savedFilePath2; 
    System.out.println("here imagepath"+mImagePath); 
    if(isFileAlreadySaved == false) 
    { 
       this.setDrawingCacheEnabled(true); 
     mBitmap=getDrawingCache(); 
     Calendar currentDate = Calendar.getInstance(); 
      SimpleDateFormat formatter= new SimpleDateFormat("yyyyMMMddHmmss"); 
      String dateNow = formatter.format(currentDate.getTime());   
      file = new File(savedFilePath); 
      FileOutputStream fos; 
      try { 
      fos = new FileOutputStream(file); 
      mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); 
      fos.close(); 
      isFileAlreadySaved = true; 
      } 
      catch (FileNotFoundException e) 
      { 
      Log.e("Panel", "FileNotFoundException", e); 
      } 
      catch (IOException e) { 
      Log.e("Panel", "IOEception", e); 
     } 

} 
    } 

    } 

回答

0

View你可以,如果drawableCache啓用

1

試試這個

調用 getDrawableCache()得到一個視圖的 Bitmap
GameBoard gameBoard = (GameBoard)findViewById(R.id.the_canvas); 
gameBoard.save(); 

GameBoard名稱爲Vie W艙

public void save() { 
     // TODO Auto-generated method stub 

//  File folder = new File(Environment.getExternalStorageDirectory()+"/folder/"); 
//   if(!folder.exists()) folderAppointment.mkdirs(); 
     flag=false; 

      try { 
       this.setDrawingCacheEnabled(true); 
       FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory()+"/filetosave4.jpg")); 
       mBitmap = this.getDrawingCache(); 
       mBitmap.compress(CompressFormat.JPEG, 100, fos); 
       fos.flush(); 
       fos.close(); 
//    Toast.makeText(getContext(), "Saved to "+fos, Toast.LENGTH_LONG).show(); 
       next(); 

      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      finally 
      { 
       Log.d("Flag",""+flag); 
      } 

    } 

嘗試,這也

private void saveBitmap(File file) { 
     try { 
      FileOutputStream fos = new FileOutputStream(file); 
      Bitmap bitmap = context.getSurface().getBitmap(); 
      if (bitmap == null) { 
       return; 
      } 
      bitmap.compress(CompressFormat.PNG, 100, fos); 
     } catch (FileNotFoundException e) { 
      throw new RuntimeException(e); 
     } 
    } 
+0

我已經使用相同的代碼,但其只顯示黑色圖像 – Akhilesh 2013-02-28 14:06:32

+0

做你的位圖的顏色爲透明 – 2013-02-28 14:35:43

+0

不行,我得從SD卡中選擇圖像, ,,並使用DrawingSurface在該圖像上畫刷,使用相同的代碼截取屏幕截圖,其中顯示黑色視圖。 – Akhilesh 2013-03-01 08:25:24

相關問題