简体   繁体   English

高效的位图

[英]Efficient Bitmaps

Recently, while trying to load a image in imageView I ran into "Out of Heap Memory" error. 最近,在尝试在imageView中加载图像时,我遇到了“堆内存不足”错误。 So I looked on internet and found the way to optimise the images. 因此,我在互联网上寻找了优化图像的方法。

I am looking forward to finding the best way to optimise a bitmap. 我期待找到优化位图的最佳方法。

Currently, I have an ImageView which has 200dp as hight and "match_parent" for width. 目前,我有一个ImageView,其高度为200dp,宽度为“ match_parent”。 Basically it fills the screen horizontally and takes 200dp vertically. 基本上,它水平填充屏幕,垂直填充200dp。

I am trying to achieve something like in this image. 我正在尝试实现此图像中的类似功能。

成就

Here is my master plan to do so. 这是我这样做的总体计划。

1) Create a layout for 1 row (ImageView, TextView, Black Translucent bar behind text) 1)为1行创建布局(ImageView,TextView,文本后的黑色半透明条)

2) Use RecyclerView and inflate the data 2)使用RecyclerView并填充数据

Currently I am stuck on Step 1 (not satisfied with code). 目前,我坚持执行步骤1(对代码不满意)。

What I did is create an XML layout for the row. 我所做的是为该行创建XML布局。

<RelativeLayout
android:id="@+id/layout_adventure_fragment"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp">

<ImageView
    android:id="@+id/imageView_adventure_home_screen_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<TextView
    android:background="@drawable/textview_background_gradient"
    android:layout_alignParentBottom="true"
    android:paddingBottom="10dp"
    android:paddingTop="10dp"
    android:paddingLeft="15dp"
    android:textColor="#FFFFFF"
    android:textSize="25sp"
    android:id="@+id/textView_adventure_name_home_screen_fragment"
    android:layout_width="match_parent"
    android:layout_height="55dp"
    android:text="Demo Text"/>

</RelativeLayout>

I'll adjust TextView as per Typography guidelines later. 稍后,我将根据“版式指南”调整TextView。

Ok, so basically ImageView is 200dp tall. 好的,所以ImageView基本上是200dp高。 To compress my images I used the guide at Android training page which uses inSampleSize and loads image in an AsyncTask. 为了压缩图像,我使用了Android培训页面上的指南,该指南使用inSampleSize并将图像加载到AsyncTask中。

To calculate the required height I used something I am not sure if it is correct way or not. 为了计算所需的高度,我不确定是否使用了正确的方法。 Since I care to compress image only for height I passed only height to calculate inSampleSize. 由于我只想压缩图像的高度,因此我只传递了高度来计算inSampleSize。

    DisplayMetrics disp = getApplicationContext().getResources().getDisplayMetrics();
    int density = disp.densityDpi;
    width = disp.widthPixels;
    height = (int)((float)(200.0/160.0) * (float)density);

    Bitmap b =decodeSampledBitmapFromResource(getResources(),R.drawable.shake,height);
    loadBitmap(R.drawable.shake,iV);

Finally I extracted thumbnail from the final bitmap for the required width and height to show user an image which fills the imageView instead of being just in the centre with margins. 最后,我从最终的位图中提取了所需宽度和高度的缩略图,以向用户显示填充imageView的图像,而不是仅在边缘留有空白。

imageView.setImageBitmap(ThumbnailUtils.extractThumbnail(bitmap,width,height));

Full code can be found here : http://pastebin.com/mhKi1sKd 完整的代码可以在这里找到: http : //pastebin.com/mhKi1sKd

What is the best way to optimise the imageView so that in case multiple images are shown in a RecyclerView, Heap memory does not get full and also if there are unnecessary codes in my program that simply take up processing and valuable time? 优化imageView的最佳方法是什么,以便在RecyclerView中显示多个图像时,堆内存不会满,并且程序中是否有不必要的代码仅占用处理时间和宝贵时间?

This is what I have achieved till now. 这是我到目前为止所取得的成就。

实现

Have a look at Picasso library. 看看Picasso图书馆。

Many common pitfalls of image loading on Android are handled automatically by Picasso: 毕加索会自动处理Android上许多常见的图片加载陷阱:

 Handling ImageView recycling and download cancelation in an adapter. Complex image transformations with minimal memory use. Automatic memory and disk caching. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM