Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using image sprites on android

I have an image (588x449) sprite with a collection of different team logos. In Android I have the corresponding ImageView displaying this:

        <ImageView android:id="@+id/image_team_logo" 
            android:src="@drawable/teamheaderssprite" 
            android:layout_height="25dp" 
            android:layout_marginLeft="5dp" 
            android:layout_marginTop="10dp" 
            android:layout_width="296dp" android:scaleType="matrix"/>   

The above will correctly display the piece of my image in the upper left hand corner. My issue is I want to be able to move the position in the image being displayed so I can jump to another area in the sprite. This is pretty common practice in html/css, I am just not seeing a 'position' type property in android xml.

like image 738
mattyp Avatar asked Apr 20 '11 15:04

mattyp


People also ask

Should I use image sprites?

The use of image sprites is done for two main reasons: For faster page loading since use only single image. It reduce the bandwidth used to load multiple images. This way less data is consume.

How do I use an image as a sprite tag?

You create a defined area with a <a> with display:block; or <div> and use overflow hidden; to hide overflow and position:relative; . Then you place your <img> image sprite inside absolutely positioned, which is possible since you positioned the parent. Then use :hover on the image to change position.

What is the use of image sprites?

An image sprite is a collection of images put into a single image. A web page with many images can take a long time to load and generates multiple server requests. Using image sprites will reduce the number of server requests and save bandwidth.


1 Answers

You can't use sprites in the same way on Android you can with HTML. There is one way I can think of to simulate sprites though: Lay the sprite out entirely horizontally (or vertically), then use a ClipDrawable to define each level as a new part of the sprite. Then define the level of each View as appropriate. (If the images in the sprite aren't all the same size, you may need to use InsetDrawable as well.)

However, I would seriously rethink using sprites in Android. There are reasons why web pages use sprites, and it's not because they're easier - it's because it speeds up webpages because you don't have to make multiple HTTP requests. Since the images will already be in your release APK, you don't gain anything by spriting them.

Not only that, but you're doing some harm by spriting on Android:

  1. Memory constraints on some Android phones are much lower than you might expect. If the entirety of your graphics are in memory at any given time, that lowers the amount of memory you have for everything else.

  2. Sprites will be harder to handle once you start writing for different screen densities (ldpi, mdpi, hdpi).

like image 91
Dan Lew Avatar answered Oct 11 '22 14:10

Dan Lew