如何使用BitmapFactory.decodeResource从Res / draw中检索多个图像

时间:2013-12-23 06:31:50

标签: android android-imageview android-image android-bitmap android-background

我需要根据设备的屏幕尺寸来调整图像大小。我可以一次一个地执行单个图像,目前我正在处理18个图像。代码太笨拙而且难以处理。最重要的是在我的代码中,我需要创建一个矩阵来存储一旦创建的图像。 我正在关注此链接 Resize Bitmap image?

如何加载多张图片,以便我可以轻松高效地完成。

1 个答案:

答案 0 :(得分:0)

@SuppressLint("NewApi")
    public static int getDeviceWidth(Activity activity) {
        int deviceWidth = 0;

        Point size = new Point();
        WindowManager windowManager = activity.getWindowManager();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            windowManager.getDefaultDisplay().getSize(size);
            deviceWidth = size.x;
        } else {
            Display display = windowManager.getDefaultDisplay();
            deviceWidth = display.getWidth();
        }
        return deviceWidth;
    }

    @SuppressLint("NewApi")
    public static int getDeviceHeight(Activity activity) {
        int deviceHeight = 0;

        Point size = new Point();
        WindowManager windowManager = activity.getWindowManager();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            windowManager.getDefaultDisplay().getSize(size);
            deviceHeight = size.y;
        } else {
            Display display = windowManager.getDefaultDisplay();
            deviceHeight = display.getHeight();
        }
        return deviceHeight;
    }

//上面的函数获取设备宽度和高度

public static Bitmap getBitmapFromPath(String sdCardPath, Activity activity) {
        // TODO Auto-generated method stub
        return decodeSampledBitmapFromResource(sdCardPath,
                getDeviceWidth(activity), getDeviceHeight(activity));
    }

基于getBitmapFromPath(),您可以将图像设置为特定于设备.. 在检索图像路径时,在活动中调用getBitmapFromPath()。

//编辑回答

final R.drawable drawableResources = new R.drawable();
final Class<R.drawable> c = R.drawable.class;
final Field[] fields = c.getDeclaredFields();

for (int i = 0, max = fields.length; i < max; i++) {
    final int resourceId;
    try {
        resourceId = fields[i].getInt(drawableResources);
    } catch (Exception e) {
        continue;
    }
    /* make use of resourceId for accessing Drawables here */
}