将所有图像保存在drawable-nodpi中

时间:2015-08-17 15:58:59

标签: android android-imageview android-drawable android-bitmap

我的应用会显示大量图片。而不是调整所有这些(当我调整大小时,它太小而且不占用所有ImageView),是否有任何理由我无法将这些图像放在drawable-nodpi中然后使用以下代码调整它们的大小:

public class SingleGameTixFragment extends Fragment {
    int arenaX, arenaY, pricesX, pricesY;
    ImageView mArenaImageView, mPricesImageView;

    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 (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

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


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.tickets_singlegametix, container,  false);

        mPricesImageView = (ImageView)v.findViewById(R.id.tickets_singleGameTix_prices);

        mPricesImageView.setImageResource(R.drawable.tickets_singlegametix_prices);

        mArenaImageView = (ImageView)v.findViewById(R.id.tickets_singleGameTix_arena);
        //mArenaImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.arena, arenaImageView.getWidth(), arenaImageView.getHeight()));

        mArenaImageView.setImageResource(R.drawable.tickets_arena);

        //ImageView backgroundImageView = (ImageView)v.findViewById(R.id.backgroundImageView);
        //backgroundImageView.setImageResource(R.drawable.crowdbackground);

        v.post(new Runnable() {
            @Override
            public void run() {
                arenaX = mArenaImageView.getWidth();
                arenaY = mArenaImageView.getHeight();
                pricesX = mPricesImageView.getWidth();
                pricesY = mPricesImageView.getHeight();

                mPricesImageView.setImageBitmap(
                        decodeSampledBitmapFromResource(getResources(), R.drawable.singleprices, arenaX, arenaY));
            }
        });

        return v;
    }
}

这些图像中的大多数都很大,没有缩放,会导致OOM错误。但是,使用上面的代码我还没有出错。

2 个答案:

答案 0 :(得分:0)

出于同样的原因,我使用了同样的技术并取得了良好的效果,尽管二次幂采样方法并不像全尺寸缩放操作那样平滑。

根据图像的类型,您可以考虑使用VectorDrawable。

答案 1 :(得分:0)

如果您不想将图片放在不同的文件夹中,只需将其放在drawable文件夹中即可。创建一个drawable文件夹,并将所有dpi独立的内容放在那里。