2012-08-01 74 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); 

祝你好运! ^ -^