android - 在android中的位图中适合图像

标签 android android-bitmap

我想创建一个具有定义大小的新位图,例如 200x200

Bitmap b = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);

现在我的理解是在该位图中放入 SD 卡的图像,但将 SD 图像调整为我创建的位图的大小。

使用 ImageView 做我能做的事:

<ImageView android:src="@drawable/landscape"
    android:adjustViewBounds="false" />

我查找的结果如下:

enter image description here

提前致谢。

编辑:发布我的代码

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

--

imageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.paisaje, 200, 200));

最佳答案

BitmapFactory.Options 中的

outWidthoutHeight 不设置结果图像的宽度和高度。它们仅在您更改 inSampleSize 并且 inJustDecodeBounds 设置为 false 时发生变化,否则它们用于读取 高度和图像的宽度。

您可以尝试使用 createScaledBitmap 缩放位图并返回新位图。

public Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {

    return Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, resId), reqWidth, reqHeight, true);
}

关于android - 在android中的位图中适合图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40720199/

相关文章:

android - 如何在视频渲染案例中使用 ImageView 进行高效处理?

android - Android 上的多线程问题

android - 撤消git命令 - 在android studio中重置磁头(硬)

android - 如何以编程方式创建 android 形状背景?

android - 如何在Android中设计EditText和Button

在异步任务期间未授予 Android 存储访问框架持久权限

java - UnsupportedOperationException 打开相机时出错 android

java - 广播接收器: Why it doesn't work?

android - Activity 泄露了最初在此处注册的 IntentReceiver com.mopub.mobileads.MoPubView。您是否错过了对 unregisterReceiver() 的调用?

android - 如何从 Imageview 设置通知图标