Image Resources for Android resources (fade in, embed)

Source: Internet
Author: User

Today, a few of the remaining knowledge of image resources to comb. Fading resources can toggle two images (currently only two images are supported) as Image status and image level resources, and make the two images switch with a fade effect. As with the switch bulb described in the previous blog post, it would be better to add a fade effect.

A cross_fade.xml file is created below in the Res/drawable directory, which reads as follows:

<?xml version= "1.0" encoding= "UTF-8"?><!--transition tag can only have two item tags--><transition xmlns:android= " Http://schemas.android.com/apk/res/android ">    <item android:drawable=" @drawable/lamp_off "></item >     <item android:drawable= "@drawable/lamp_on" ></item></transition>

Reference the resource file in the Main_layout file with the following code:
  <imageview      android:id= "@+id/imageview_lamp"      android:layout_width= "fill_parent"      android:layout_ height= "Wrap_content"      android:src= "@drawable/cross_fade"      />
From the first image (the first item ' Switch to the 2nd image to use the Transitiondrawable.starttransition method, switch from the 2nd image to the 1th image using the Transitiondrawable.reversetransition method, as shown in the following code:

public class Mainactivity extends Activity {private ImageView ivlamp; @SuppressLint ("Newapi") @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); /resources res=getresources ();//drawable drawable=res.getdrawable (r.drawable.bitmap_test);//TextView txt= ( TextView) Findviewbyid (R.id.textview);//txt.setbackground (drawable); ivlamp= (ImageView) Findviewbyid ( R.id.imageview_lamp);//Set level to 8, display lamp_off.png//ivlamp.setimagelevel (8);} Click event method for the Turn on light button public void onclick_lampon (view view) {//Set level is 15, showing Lamp_on.png//ivlamp.setimagelevel (15); Transitiondrawable drawable= (transitiondrawable) ivlamp.getdrawable ();//switch from the first image to the 2nd image. which uses 1 seconds (1000 milliseconds) time to complete the Fade effect drawable.starttransition (1000);} Click event method for the off light button public void Onclick_lampoff (view view) {//Set level is 6, showing lamp_off.png//ivlamp.setimagelevel (6); Transitiondrawable drawable= (transitiondrawable) ivlamp.getdrawable ();//switch from 2nd image to 1th image. which uses 1 seconds (1000 milliseconds) to complete the fade effect drawable.reversetransition (1000);}} 


The effect of the fade is as follows:

If the image you are displaying requires less than the view that loads the image, consider using an embedded image resource. The embedded image resource is a file in XML format with only one <inset> tag. Use the following 4 properties to set the distance between the image and the upper and lower left and right 4 directions.

Android:insettop: The distance from the top of the image.

Android:insetright: The distance from the right side of the image.

Android:insetbottom: The distance from the bottom edge of the image.

Android:insetleft: Distance from the left of the image.

This embedded image resource is well understood, so we don't give an example here.

Reprint Please specify source: http://blog.csdn.net/android_jiangjun/article/details/33360661


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.