Android Drawable及其相关类的使用
作者:bigmazhiyu 发布时间:2023-10-18 09:32:38
标签:Android,Drawable
一个让人赏心悦目的界面对软件来说非常重要,因此图形图像资源也显得非常重要。本讲就要谈一谈Android中处理图形图像的最重要的一个类Drawable。Drawable就是一个可以画的对象的抽象(有点别扭,你凑合看吧),下面是它的继承关系,可以看到BitmapDrawable,AnimationDrawable等对象都是它的子类。
最简单的使用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中
2、res\main.xml的内容如下:
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/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的使用-设置壁纸">
<button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="查看图片A" android:id="@+id/Button01">
</button>
<button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="查看图片B" android:id="@+id/Button02">
</button>
<button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="设置图片A为壁纸" android:id="@+id/Button03">
</button>
<button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="设置图片B为壁纸" android:id="@+id/Button04">
</button>
<button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="恢复默认壁纸" android:id="@+id/Button05">
</button>
<imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ImageView01">
</imageview>
</textview></linearlayout>
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的内容如下(设置权限)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/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>
</activity>
</application>
<uses -sdk="" android:minsdkversion="8">
<uses -permission="" android:name="android.permission.SET_WALLPAPER"></uses>
</uses></manifest>
5、运行程序,查看结果:
点击“查看图片A”按钮,ImageView载入图片A并显示出来
点击”设置图片B为壁纸”按钮,可以看到图片B已经成为桌面壁纸。 好了本讲就到这里。


猜你喜欢
- java 中链表的定义与使用方法Java实现链表主要依靠引用传递,引用可以理解为地址,链表的遍历多使用递归,这里我存在一个疑问同一个类的不同
- 也许是本人不才,初识Maven时,被各种不明所以的教程搞得一头雾水,而在后来的使用中,我发现Maven大部分功能没有想象的那么困难。本片文章
- 在使用struts多模块的,找到一些小技巧和经验,与大家分享一下。 关于多module的配置就不说了,只需要用不同的config
- 在C#中,得益于强大的GC机制,使得我们开发程序变得非常简单,很多时候我们只需要管使用,而并不需要关心什么时候释放资源。但是,GC有的时并不
- Thread parameterThread_t = null; private void Print_DetailForm_S
- 本文实例为大家分享了java顺时针打印矩阵的具体代码,供大家参考,具体内容如下github:剑指offer编程题 import j
- 引用Spring官方文档的说法介绍一下@Conditional注解:Spring5.0.15版本@Conditional注解官方文档@Con
- 前言翻译自:arkadiuszchmura.com/posts/be-ca…最近我在负责一段代码库,需要在使用 Flo
- 机器跑了一晚上,发现有崩溃现象,由于页面内有动态绘图功能,我怀疑是绘图原因,但是今天上午有人提醒我才想到,是不是间隔调用时DWR产生了内存泄
- 简介在 io 包中,提供了两个与平台无关的数据操作流:数据输出流(DataOutputStream)、数据输入流 (DataInputStr
- 话不多说,请看代码:using System;using System.Web;using System.Drawing;using Sys
- 前言最近接了一个项目,甲方不愿意买服务器,但是呢,项目又必须要用文件功能。所以很巧,最近又刚好看到了Minio这个牛逼的工具。正好借此机会记
- 本文实例为大家分享了Java网络编程TCP实现文件上传的具体代码,供大家参考,具体内容如下上一篇博客,用网络编程TCP 实现聊天,这次实现文
- 导读 Spring Boot方式的项目开发已经逐步成为Java应用开发领域的主流框架,它不仅可以方便地创建生产级的Spring应用
- 实践过程效果代码public partial class Form1 : Form{ public Form1()
- 搭建个SSM框架居然花费了我好长时间!特此记录!需要准备的环境:idea 2017.1jdk1.8Maven 3.3.9请提前将idea与M
- 1、fragment简介我对fragment的理解是基于activity的,对于大多数的基本开始发时,我们最先遇到的就是用activity来
- 1.值类型值类型包括:数值类型,结构体,bool型,用户定义的结构体,枚举,可空类型。值类型的变量直接存储数据,分配在托管栈中。变量会在创建
- 相应的类库可在我的资源页面中找到,关于类成员的说明可通过对象浏览器查看函数说明Imports BitOperatorLibrary.Shif
- 1.阻塞I/O模型阻塞IO模型是常见的IO模型,在读写数据时客户端会发生阻塞。阻塞IO模型的工作流程为:1.1在用户线程发出IO请求之后,内