如何调整图像数组的大小?

How to resize an image array?

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ImageView iv_images;

    final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.imageadapter1, null);

    iv_images = (ImageView) convertView.findViewById(R.id.iv_images1);

    iv_images.setImageResource(images[position]);

    return convertView;
}

java.lang.OutOfMemoryError: Failed to allocate a 9042060 byte allocation with 8030424 free bytes and 7MB until OOM

您可以使用较小的图片。

  1. Google 实际上已经发布了避免 OutOfMemoryErrors 的指南 here 这将有很大帮助,尽管我也不得不使用较小的图像尺寸。
  2. 一种几乎肯定有效的方法是在 manifest[=25= 中设置 android:largeHeap="true" ],在您的 application 标签之间。这会增加您的堆大小,但可能会使您的应用程序有点滞后。
  3. 如果您有大量图片,请使用 WebP 图片加载。

你可以试试这个link https://developer.android.com/topic/performance/graphics/load-bitmap

您可以使用 Picasso

等库
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ImageView iv_images;

    final LayoutInflater inflater = (LayoutInflater) 
    mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.imageadapter1, null);

    iv_images = (ImageView) convertView.findViewById(R.id.iv_images1);

    Picasso.get()
       .load(images[position])
       .resize(YOUR_WIDTH, YOUR_HEIGHT)
       .centerCrop()
       .into(iv_images)

    return convertView;
}

别忘了添加依赖项。

Gradle:

implementation 'com.squareup.picasso:picasso:2.71828'

专家:

<dependency>
  <groupId>com.squareup.picasso</groupId>
  <artifactId>picasso</artifactId>
  <version>2.71828</version>
</dependency>