【问题标题】:How to resize an image array?如何调整图像数组的大小?
【发布时间】:2019-08-05 07:32:53
【问题描述】:
@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: 分配 9042060 字节失败 分配 8030424 个空闲字节和 7MB 直到 OOM

【问题讨论】:

    标签: android out-of-memory


    【解决方案1】:

    可以使用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;
    }
    

    别忘了添加依赖项。

    分级:

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

    马文:

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

    【讨论】:

    • 很高兴它对您有所帮助。如果您对此有任何疑问,请随时询问。
    • 非常感谢老兄......它正在工作
    【解决方案2】:

    您可以只使用较小的图像。

    1. Google 实际上已经发布了一份关于避免 OutOfMemoryErrors here 的指南,这将很有帮助,尽管我也必须使用较小的图像尺寸。
    2. 几乎可以肯定的一种方法是在 ma​​nifest 中的 application 标记之间设置 android:largeHeap="true"。这会增加您的堆大小,但可能会使您的应用稍微滞后。
    3. 如果您有大量图片,请使用 WebP 图片进行加载。

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

    【讨论】:

    猜你喜欢
    • 2013-10-24
    • 2021-04-21
    • 2018-12-18
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多