0

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

如何加载多个图像,以便我可以更轻松、更有效地完成它。

4

1 回答 1

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 */
}
于 2013-12-23T06:37:56.723 回答