2011-12-08 69 views
4

對於我的申請,我已經從教程修改和創建自定義視圖http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-6-implementing-the-pinch-zoom-gesture/1847安卓:如何查看轉換爲位圖,而不捕獲背景屏幕

現在我正在拍攝中使用雙指縮放功能將圖像放大爲位圖。

部分我能夠通過利用

setDrawingCacheEnabled(true); 
Bitmap bm = Bitmap.createBitmap(finalView.getDrawingCache()); 
setDrawingCacheEnabled(false); 

使用這種方法,我得到兩個縮放的影像和畫面背景中得到位圖。

有什麼辦法只捕獲縮放圖像作爲位圖?

回答

0

在已經搜查了很多,我發現在同一個社區&也感謝從「weakwire」在同一職位所提供的代碼「臨時」的解決方案。

Getting coordinates and width/height from a matrix

想法很簡單,所有的i都做的是通過裁剪從縮放的圖像除去背景屏幕。代碼有點冗長,但爲我工作。

  1. 首先得到原始位大小

    float imgWidth = imgBitmap.getWidth() 
    float imgHeight = imgBitmap.getHeight() 
    
  2. 獲得矩陣(在我的應用程序中使用雙指縮放功能採用矩陣「絕招」)值,用來縮放圖像和獲取scaledWidth &縮放原始圖像的高度。

    float[] values = new float[9]; 
    matrix.getValues(values); 
    float globalX = values[2]; 
    float globalY = values[5]; 
    float scaledWidth = values[0]*imgWidth; 
    float scaledHeight = values[4]*imgHeight; 
    
    // If zoomed Image gets out of screen, I'm trying to crop the image available in the screen by considering image from (0,0) 
    
    if(globalX<0) 
    globalX = 0; 
    
    if(globalY<0) 
    globalY = 0; 
    
  3. 終於攻克了查看和裁剪背景 在我的情況(「finalView」是自定義視圖名稱,其中捏縮放發生)

    setDrawingCacheEnabled(false); 
    destroyDrawingCache(); 
    finalView.setDrawingCacheEnabled(true); 
    Bitmap bm = Bitmap.createBitmap(finalView.getDrawingCache()); 
    finalView.setDrawingCacheEnabled(false);  
    
    //Final Image Width & Height 
    
    int finalWidth = Math.min((int)width,(int)finalView.getWidth()); 
    int finalHeight = Math.min((int)height,(int)finalView.getHeight());   
    

    //作物得到縮放圖像

    Bitmap finalBitmap = Bitmap.createBitmap(bm, (int)globalX, (int)globalY, finalWidth ,finalHeight); 
    

雖然這是一個臨時的解決方案,它爲我工作。如果有其他解決方案,請發佈。

0

嘗試

setDrawingCacheEnabled(false) 
finalView.setDrawingCacheEnabled(true); 
Bitmap bm = Bitmap.createBitmap(finalView.getDrawingCache()); 
finalView.setDrawingCacheEnabled(false); 
+0

沒有,它不工作.. –

+0

嘗試destroyDrawingCache的第一行,請 – weakwire

+0

我已經試過setDrawingCacheEnabled(假)之後; \t \t \t destroyDrawingCache(); –

0

這個原始的答案here,只需更換設置背景部分。希望這有助於

public static Bitmap getBitmapFromView(View view) { 
    if (view == null) return null; 
    //Define a bitmap with the same size as the view 
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); 
    //Bind a canvas to it 
    Canvas canvas = new Canvas(returnedBitmap); 
    // draw white background on the canvas 
    canvas.drawColor(Color.WHITE); 
    // draw the view on the canvas 
    view.draw(canvas); 
    //return the bitmap 
    return returnedBitmap; 
}