aeroxr1
aeroxr1

Reputation: 1074

Android: how can I add different layer to a image view dynamically?

I have one fragment layout with a background image. Programmatically I have to add the different layer to this image because I want to add some particular details depending of some data. There are three different details that it can change in 3 different way. ( there is a thread that works at 5/10hz and it loads the right details)

The following is my first implementation. In this version, I have done all of works in XML layout and in a programmatically way, when the user clicks on a certain button, I set the visibility of each image view. I have seen that is much expensive, and sometimes the inflate layout return null:

   <RelativeLayout  
       android:id="@+id/container"
       android:layout_width="match_parent"
       android:layout_height="match_parent" 
       android:background="@drawable/background"
     >

     <ImageView 
        android:id="@+id/layer1"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:visibility="gone"
        android:src="@drawable/ballgreen"/>
<ImageView 
        android:id="@+id/layer2"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:visibility="gone"
         android:src="@drawable/ballred"/>
<ImageView 
        android:id="@+id/layer3"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:visibility="gone"
          android:src="@drawable/ballyellow"/>

            ... 6 more image view layer....

</RelativeLayout>

Then I thought to use an async task to decode all drawable image, make a bitmap fusion and in a post execute load the result bitmap in a single image view. But the asynctask is not the better way to implement, because the user can click on that button many times and each time I have to call new asynctask ..

Are there a clever ways to implement all of this?

Goodbye and thanks a lot :)

EDIT : each layer is a detail that thread can add to that image. For example : a car, a tree, a red ball ecc ecc. In the xml example for me each imageview is a layer, and this is wrong. But I don't know the clever way to implement ..

EDIT2 : Moreover I know the maximum amount of layer, but there are many combinations. there are 3 point and for each point I can choice between 3 differents details

Edit3: i add some details , there is a thread that change laYer and not the user

Upvotes: 0

Views: 1878

Answers (1)

Snild Dolkow
Snild Dolkow

Reputation: 6866

Use the LayerDrawable class, adding new layers as the user enables them. I don't think you can remove layers from it, but you can create a new LayerDrawable each time the user disables a layer. Or you can use setDrawable(int, Drawable) to replace a layer.

This means you don't have to complicate your layouts, and should get rid of those pesky extra inflation/measure/layout passes.

As for the image loading / async task stuff: use an LRU cache for your bitmaps. When a layer is enabled, check the cache, and if it's there, just use it. Otherwise, use an async task to load it.

Upvotes: 1

Related Questions