高分辨率屏幕上的Android位图自定义视图性能较差

时间:2012-08-01 07:46:36

标签: android bitmap resolution custom-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除外)。

抱歉我的英语很差。

1 个答案:

答案 0 :(得分:0)

我的选择: 1,使用'xxx.9.png'格式pics资源。 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);
祝你好运! ^ - ^