Android Drawable及其相关类的使用

一个让人赏心悦目的界面对软件来说非常重要,因此图形图像资源也显得非常重要。本讲就要谈一谈Android中处理图形图像的最重要的一个类Drawable。Drawable就是一个可以画的对象的抽象(有点别扭,你凑合看吧),下面是它的继承关系,可以看到BitmapDrawable,AnimationDrawable等对象都是它的子类。

Android Drawable及其相关类的使用_第1张图片

最简单的使用Drawable资源的方法是,把图片放入Android工程的res\drawable目录下,编程环境会自动在R类里为此资源创建一个引用。你可以使用此引用访问该资源对象。譬如对应用程序的图标,在Java代码中可以用R.drawable.icon引用到它,在XML中可以用@drawable/icon引用到它。

那么如果图片资源不在项目中而是在SDCard中时如何使用呢,我们看一下下面的例子学习一下Drawable的使用,并且顺便学习一下Bitmap和BitmapFactory的使用。

1、创建项目 Lesson23_Drawable,主Acitivity的名字是 MainDrawable.java,拷贝a.jpg和b.jpg两个文件到sdcard中

Android Drawable及其相关类的使用_第2张图片

2、res\main.xml的内容如下:











3、MainDrawable.java的内容如下:

package android.basic.lesson23;

import java.io.IOException;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainDrawable extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //定义UI组件
        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(R.id.Button04);
        Button b5 = (Button) findViewById(R.id.Button05);
        final ImageView iv= (ImageView)findViewById(R.id.ImageView01);

        //定义按钮点击监听器
        OnClickListener ocl = new OnClickListener() {

            @Override
            public void onClick(View v) {

                switch (v.getId()) {
                case R.id.Button01:
                    //给ImageView设置图片,从存储卡中获取图片为Drawable,然后把Drawable设置为ImageView的背景
                    iv.setBackgroundDrawable(Drawable.createFromPath("/sdcard/a.jpg"));
                    break;
                case R.id.Button02:
                    iv.setBackgroundDrawable(Drawable.createFromPath("/sdcard/b.jpg"));
                    break;
                case R.id.Button03:
                    try {
                        //Activity的父类ContextWrapper有这个setWallpaper方法,当然使用此方法需要有android.permission.SET_WALLPAPER权限
                        setWallpaper(BitmapFactory.decodeFile("/sdcard/a.jpg"));
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    break;
                case R.id.Button04:
                    try {
                        setWallpaper(BitmapFactory.decodeFile("/sdcard/b.jpg"));
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    break;
                case R.id.Button05:
                    try {
                        //Activity的父类ContextWrapper有这个clearWallpaper方法,作用是恢复默认壁纸,当然使用此方法需要有android.permission.SET_WALLPAPER权限
                        clearWallpaper();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
                }

            }

        };

        //给按钮们绑定点击监听器
        b1.setOnClickListener(ocl);
        b2.setOnClickListener(ocl);
        b3.setOnClickListener(ocl);
        b4.setOnClickListener(ocl);
        b5.setOnClickListener(ocl);
    }

}

4、AndroidManifest.xml的内容如下(设置权限)



  
    
      
        
        
      
    

  
  


 

5、运行程序,查看结果: 

Android Drawable及其相关类的使用_第3张图片

点击“查看图片A”按钮,ImageView载入图片A并显示出来 

Android Drawable及其相关类的使用_第4张图片

点击”设置图片B为壁纸”按钮,可以看到图片B已经成为桌面壁纸。 好了本讲就到这里。

以上就是对Android Drawable 的资料整理,后续继续补充,谢谢大家的支持!

你可能感兴趣的:(Android Drawable及其相关类的使用)