The use of Android drawable and its related classes _android

Source: Internet
Author: User

A pleasing interface is very important for software, so graphic image resources are also very important. This talk is about one of the most important class drawable to deal with graphic images in Android. Drawable is an abstraction of an object that can be drawn (a bit awkward, you can do it), the following is its inheritance relationship, you see bitmapdrawable,animationdrawable and so on objects are its subclasses.

The easiest way to use drawable resources is to put the picture in the Res\drawable directory of the Android project, and the programming environment will automatically create a reference to this resource in the R class. You can use this reference to access the resource object. An icon for an application, for example, can be referenced in Java code with R.drawable.icon, which can be referenced in XML in @drawable/icon.

So if the picture resource is not in the project but how to use it in the SDcard, let's take a look at the following example to learn about the use of drawable, and by the way learn about the use of bitmap and bitmapfactory.

1, create Project lesson23_drawable, the name of the main acitivity is, copy a.jpg and b.jpg two files to SDcard

2, the contents of the Res\main.xml are as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "" Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " > <textview android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:textsize= "20SP" android:text= "drawable Use-Settings wallpaper" > <button android:layout_width= "wrap_content" android:layout_height= " Content "Android:textsize=" 20sp "android:text=" View Picture a "android:id=" @+id/button01 "> </button> <button Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:textsize= "20sp" android:text= " View Picture B "Android:id=" @+id/button02 "> </button> <button android:layout_width=" Wrap_content "android:layout _height= "Wrap_content" android:textsize= "20SP" android:text= "set Picture A is wallpaper" android:id= "@+id/button03" > </button > <button android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:textSize= "20SP" android:text= "set picture B is Wallpaper" android:id= "@+id/button04" > </button> <button android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:textsize= "20sp" android:text= "restore Default Wallpaper" android:id= "@+id /button05 "> </button> <imageview android:layout_width=" wrap_content "android:layout_height=" Wrap_ Content "android:id=" @+id/imageview01 "> </imageview> </textview></linearlayout>

The contents of

3 and are as follows:

Package android.basic.lesson23;

Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;

Import Android.widget.ImageView; The public class Maindrawable extends activity {/** called the ' when ' is the ' The activity ' is a-a-created
        c void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

        Setcontentview (R.layout.main);
        Defines the UI component Button B1 = (Button) Findviewbyid (R.ID.BUTTON01);
        Button B2 = (button) Findviewbyid (R.ID.BUTTON02);
        Button B3 = (button) Findviewbyid (R.ID.BUTTON03);
        button b4 = (button) Findviewbyid (;
        button B5 = (button) Findviewbyid (R.ID.BUTTON05);

        Final ImageView iv= (ImageView) Findviewbyid (R.ID.IMAGEVIEW01); Define button click Listener OnclicklisTener OCL = new Onclicklistener () {@Override public void OnClick (View v) {Swit CH (v.getid ()) {case R.ID.BUTTON01://Set picture for ImageView, get picture from memory card as drawable, then set drawable
                    Background iv.setbackgrounddrawable for ImageView (Drawable.createfrompath ("/sdcard/a.jpg"));
                    Case (Drawable.createfrompath ("/sdcard/b.jpg"));
                Break Case {//activity parent class Contextwrapper have this setwallpaper method, of course, use this method
                    Need to have Android.permission.SET_WALLPAPER permission SetWallpaper (Bitmapfactory.decodefile ("/sdcard/a.jpg"));
                    catch (IOException E1) {e1.printstacktrace ();
                } break;
               Case {         SetWallpaper (Bitmapfactory.decodefile ("/sdcard/b.jpg"));
                    catch (IOException E1) {e1.printstacktrace ();
                } break; Case {//activity parent class Contextwrapper has this clearwallpaper method, which is to restore
                    The default wallpaper, of course, uses this method to have Android.permission.SET_WALLPAPER permission clearwallpaper ();
                    catch (IOException e) {e.printstacktrace ();
                } break;



        To the button to bind fixed-point hit listener b1.setonclicklistener (OCL);
        B2.setonclicklistener (OCL);
        B3.setonclicklistener (OCL);
        B4.setonclicklistener (OCL);
    B5.setonclicklistener (OCL); }


4, Androidmanifest.xml content is as follows (set permissions)

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android=
"" Android "package=" Android.basic.lesson23 "android:versioncode=" 1 "android:versionname=" 1.0 ">
  < Application android:icon= "@drawable/icon" android:label= "@string/app_name" > <activity android:label=
    "@ String/app_name "Android:name=". Maindrawable ">
      <intent-filter=" ">
        <action android:name=" Android.intent.action.MAIN ">
        <category android:name= "Android.intent.category.LAUNCHER" >
      </category></action></ intent>

  <uses-sdk= "" android:minsdkversion= "8" >

<uses-permission= "" Android:name= "Android.permission.SET_WALLPAPER" ></uses>

5, run the program to see the results:

Click "View Picture a" button, ImageView load picture A and show it

Click the "set Picture B as Wallpaper" button to see that picture B has become a desktop wallpaper. All right, here's the story.

The above is to the Android drawable data collation, follow-up continue to add, thank you for your support!

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: 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.