2012-08-01 42 views
0

我正在製作自定義列表視圖項目。擴展視圖並重寫onDraw方法。高分辨率屏幕上的Android位圖自定義視圖性能不佳

private Bitmap bmpScaledBackground; //with size:(screenWidth , screenHeight/4) 
@Override 
public void onDraw(Canvas canvas){ 
    canvas.drawBitmap(bmpScaledBackground , 0 , 0 , null); 
    //...more of that 
} 

到目前爲止,它在Galaxy SII等普通手機上運行良好。

但是,談到Galaxy Nexus時,性能很差。我相信這是因爲GN(1280x720)的大分辨率。

在上述情況下,單獨的背景位圖(bmpScaledBackground)大到720x320,需要很長時間繪製。更不用說OOM的風險了。

我在寫信詢問是否有一種更具可擴展性的方式(SurfaceView和OpenGL除外)進行自定義視圖。

對不起,我英文很差。

回答

0

我的選擇: 1,使用'xxx.9.png'格式圖片資源。 2,使用壓縮位圖。

//get opts 
    BitmapFactory.Options opts = new BitmapFactory.Options(); 
    opts.inJustDecodeBounds = true ; 
    Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts); 

    //get a appropriate inSampleSize 
    public static int computeSampleSize(BitmapFactory.Options options, 
     int minSideLength, int maxNumOfPixels) { 
    int initialSize = computeInitialSampleSize(options, minSideLength, 
      maxNumOfPixels); 
    int roundedSize; 
    if (initialSize <= 8) { 
     roundedSize = 1 ; 
     while (roundedSize < initialSize) { 
      roundedSize <<= 1 ; 
     } 
    } else { 
     roundedSize = (initialSize + 7)/8 * 8 ; 
    } 
    return roundedSize; 
} 

private static int computeInitialSampleSize(BitmapFactory.Options options, 
     int minSideLength, int maxNumOfPixels) { 
    double w = options.outWidth; 
    double h = options.outHeight; 
    int lowerBound = (maxNumOfPixels == - 1) ? 1 : 
      (int) Math.ceil(Math.sqrt(w * h/maxNumOfPixels)); 
    int upperBound = (minSideLength == - 1) ? 128 : 
      (int) Math.min(Math.floor(w/minSideLength), 
      Math.floor(h/minSideLength)); 
    if (upperBound < lowerBound) { 
     // return the larger one when there is no overlapping zone. 
     return lowerBound; 
    } 
    if ((maxNumOfPixels == - 1) && 
      (minSideLength == - 1)) { 
     return 1 ; 
    } else if (minSideLength == - 1) { 
     return lowerBound; 
    } else { 
     return upperBound; 
    } 
} 
//last get a well bitmap. 
BitmapFactory.Options opts =new BitmapFactory.Options(); 
opts.inSampleSize = inSampleSize;// inSampleSize should be index value of 2. 
Bitmap wellbmp =null; 
wellbmp = BitmapFactory.decodeResource(getResources(), mImageIds[position],opts); 

祝你好運! ^ -^

相關問題