Drawable使用入门

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

[/url]

最简单的使用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中

[url=http://android.yaohuiji.com/wp-content/uploads/2010/08/file_exlorper.jpg]

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


01.<?xml version="1.0" encoding="utf-8"?>

02.<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">

03.<textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:textsize="20sp" android:text="Drawable的使用-设置壁纸">

04.<button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="查看图片A" android:id="@+id/Button01">

05.</button>

06.<button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="查看图片B" android:id="@+id/Button02">

07.</button>

08.<button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="设置图片A为壁纸" android:id="@+id/Button03">

09.</button>

10.<button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="设置图片B为壁纸" android:id="@+id/Button04">

11.</button>

12.<button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="恢复默认壁纸" android:id="@+id/Button05">

13.</button>

14.<imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ImageView01">

15.</imageview>

16.</textview></linearlayout>
复制代码3、MainDrawable.java的内容如下:


01.package android.basic.lesson23;

02.

03.import java.io.IOException;

04.

05.import android.app.Activity;

06.import android.graphics.BitmapFactory;

07.import android.graphics.drawable.Drawable;

08.import android.os.Bundle;

09.import android.view.View;

10.import android.view.View.OnClickListener;

11.import android.widget.Button;

12.import android.widget.ImageView;

13.

14.public class MainDrawable extends Activity {

15.        /** Called when the activity is first created. */

16.        @Override

17.        public void onCreate(Bundle savedInstanceState) {

18.                super.onCreate(savedInstanceState);

19.                setContentView(R.layout.main);

20.

21.                //定义UI组件

22.                Button b1 = (Button) findViewById(R.id.Button01);

23.                Button b2 = (Button) findViewById(R.id.Button02);

24.                Button b3 = (Button) findViewById(R.id.Button03);

25.                Button b4 = (Button) findViewById(R.id.Button04);

26.                Button b5 = (Button) findViewById(R.id.Button05);

27.                final ImageView iv= (ImageView)findViewById(R.id.ImageView01);

28.

29.                //定义按钮点击监听器

30.                OnClickListener ocl = new OnClickListener() {

31.

32.                        @Override

33.                        public void onClick(View v) {

34.

35.                                switch (v.getId()) {

36.                                case R.id.Button01:

37.                                        //给ImageView设置图片,从存储卡中获取图片为Drawable,然后把Drawable设置为ImageView的背景

38.                                        iv.setBackgroundDrawable(Drawable.createFromPath("/sdcard/a.jpg"));

39.                                        break;

40.                                case R.id.Button02:

41.                                        iv.setBackgroundDrawable(Drawable.createFromPath("/sdcard/b.jpg"));

42.                                        break;

43.                                case R.id.Button03:

44.                                        try {

45.                                                //Activity的父类ContextWrapper有这个setWallpaper方法,当然使用此方法需要有android.permission.SET_WALLPAPER权限

46.                                                setWallpaper(BitmapFactory.decodeFile("/sdcard/a.jpg"));

47.                                        } catch (IOException e1) {

48.                                                e1.printStackTrace();

49.                                        }

50.                                        break;

51.                                case R.id.Button04:

52.                                        try {

53.                                                setWallpaper(BitmapFactory.decodeFile("/sdcard/b.jpg"));

54.                                        } catch (IOException e1) {

55.                                                e1.printStackTrace();

56.                                        }

57.                                        break;

58.                                case R.id.Button05:

59.                                        try {

60.                                                //Activity的父类ContextWrapper有这个clearWallpaper方法,作用是恢复默认壁纸,当然使用此方法需要有android.permission.SET_WALLPAPER权限

61.                                                clearWallpaper();

62.                                        } catch (IOException e) {

63.                                                e.printStackTrace();

64.                                        }

65.                                        break;

66.                                }

67.

68.                        }

69.

70.                };

71.

72.                //给按钮们绑定点击监听器

73.                b1.setOnClickListener(ocl);

74.                b2.setOnClickListener(ocl);

75.                b3.setOnClickListener(ocl);

76.                b4.setOnClickListener(ocl);

77.                b5.setOnClickListener(ocl);

78.        }

79.

80.}
复制代码4、AndroidManifest.xml的内容如下(设置权限):


01.<?xml version="1.0" encoding="utf-8"?>

02.<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android.basic.lesson23" android:versioncode="1" android:versionname="1.0">

03.    <application android:icon="@drawable/icon" android:label="@string/app_name">

04.        <activity android:label="@string/app_name" android:name=".MainDrawable">

05.            <intent -filter="">

06.                <action android:name="android.intent.action.MAIN">

07.                <category android:name="android.intent.category.LAUNCHER">

08.            </category></action></intent>

09.        </activity>

10.

11.    </application>

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

13.

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

15.</uses></manifest> 
复制代码5、运行程序,查看结果:[/url]点击“查看图片A”按钮,ImageView载入图片A并显示出来[url=http://android.yaohuiji.com/wp-content/uploads/2010/08/image20.png]点击”设置图片B为壁纸”按钮,可以看到图片B已经成为桌面壁纸。 好了本讲就到这里。


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值