0

Android Emulator 显示空白屏幕,图像仍然打开(假设有 150,150 张图片)。换句话说,我的 150,150 个拇指在空白屏幕后面

我的图像大于 150*150,我按照下面的链接有效地加载大位图,但它仍然不起作用。

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

这些是我在 imageAdapter 中的代码

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if(reqHeight + reqWidth == 0){

            return inSampleSize;
        }
        else if (height > reqHeight || width > reqWidth) {

            // Calculate ratios of height and width to requested height and width
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);

            // Choose the smallest ratio as inSampleSize value, this will guarantee
            // a final image with both dimensions larger than or equal to the
            // requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }

        return inSampleSize;
    }

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

我还添加了完整的 imageAdapter 类以获取更多信息。

package com.example.first;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.util.Log;

public class ImageAdapter extends BaseAdapter{

    private Context mContext;
    public Integer[] mThumbIds = {
            R.drawable.h18, R.drawable.h17, R.drawable.h16, R.drawable.h15, 



    };
    public ImageAdapter(Context c) {
        mContext = c;
    }

    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {

        return mThumbIds[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if(reqHeight + reqWidth == 0){

            return inSampleSize;
        }
        else if (height > reqHeight || width > reqWidth) {

            // Calculate ratios of height and width to requested height and width
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);

            // Choose the smallest ratio as inSampleSize value, this will guarantee
            // a final image with both dimensions larger than or equal to the
            // requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }

        return inSampleSize;
    }

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageview;
        if(convertView == null){
            imageview = new ImageView(mContext);
            imageview.setScaleType(ImageView.ScaleType.FIT_XY);
            imageview.setLayoutParams(new GridView.LayoutParams(150,150));
            imageview.setPadding(8, 8, 8, 8);



        }else{
            imageview = (ImageView) convertView;
        }



        imageview.setImageBitmap(decodeSampledBitmapFromResource( null, mThumbIds[position], 150, 150));
        return imageview;
    }

    private Resources getResources() {
        // TODO Auto-generated method stub
        return null;
    }



}

对此的任何帮助表示赞赏。

4

1 回答 1

0

您正在调用您的 Bitmap-loadernull而不是您的Resources对象。

更换

decodeSampledBitmapFromResource( null, mThumbIds[position], 150, 150)

decodeSampledBitmapFromResource( mContext.getResources(), mThumbIds[position], 150, 150)

应该做的伎俩。

于 2013-04-29T20:39:46.117 回答