0

我正在制作一个自定义列表视图项。扩展 View 并覆盖 onDraw 方法。

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 除外)来制作自定义视图。

对不起我糟糕的英语。

4

1 回答 1

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

祝你好运!^-^

于 2012-08-01T08:07:34.953 回答