jar包中含有Android图片文件以及按钮等selector.xml的替代方案
来源:互联网 发布:pb潜入sql取值 编辑:程序博客网 时间:2024/04/27 06:06
原文地址:http://www.cnblogs.com/aimo/archive/2012/04/26/2472071.html
在做SDK时,需求为Jar,即图片等都必须打包进入Jar中。
经过一个上午的努力,成果如下:
1.除9.png外的资源图片 都可以打包入Jar中,并能正常解析使用。
2.由StateListDrawable来取代selector.xml实现按钮的不同点击状态的效果图。
3.总之,全部入一个Jar中,无其它文件
实现:
图片全部入assets目录,使用以下代码把jpg/png转为Drawable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* 读取指定asset目录中的图片文件为 Drawable
*
* @param context
* @param imageFileName
* @return null if exception happened.
*/
public
static
Drawable getDrawableFromAssets(Context context,
String imageFileName) {
Drawable result =
null
;
AssetManager assetManager = context.getAssets();
InputStream is =
null
;
try
{
is = assetManager.open(imageFileName);
result = Drawable.createFromStream(is,
null
);
is.close();
is =
null
;
}
catch
(IOException e) {
e.printStackTrace();
}
return
result;
}
绑定按钮的状态图片:
1
2
3
4
5
6
7
8
private
void
bindSelectDrawable(Context context) {
bindButtonDrawable(context, mPositiveButton, POSITVE_BTN_NORMAL_IMG,
POSITVE_BTN_PRESSED_IMG);
bindButtonDrawable(context, mNeutralButton, NEUTRAL_BTN_NORMAL_IMG,
NEUTRAL_BTN_PRESSED_IMG);
bindButtonDrawable(context, mNegativeButton, NEGATIVE_BTN_NORMAL_IMG,
NEGATIVE_BTN_PRESSED_IMG);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
*
* @param context
* @param button
* @param nornalImageFileName
* @param pressedImageFileName
*/
private
void
bindButtonDrawable(Context context, Button button,
String nornalImageFileName, String pressedImageFileName) {
StateListDrawable stateListDrawable =
new
StateListDrawable();
Drawable normalDrawable = ApkUtil.getDrawableFromAssets(context,
nornalImageFileName);
Drawable pressedDrawable = ApkUtil.getDrawableFromAssets(context,
pressedImageFileName);
stateListDrawable.addState(
new
int
[] { android.R.attr.state_active },
normalDrawable);
stateListDrawable.addState(
new
int
[] { android.R.attr.state_pressed,
android.R.attr.state_enabled }, pressedDrawable);
stateListDrawable.addState(
new
int
[] { android.R.attr.state_focused,
android.R.attr.state_enabled }, normalDrawable);
stateListDrawable.addState(
new
int
[] { android.R.attr.state_enabled },
normalDrawable);
button.setBackgroundDrawable(stateListDrawable);
}
至于动态布局来取代layout.xml就不说了,比较简单。
0 0
- jar包中含有Android图片文件以及按钮等selector.xml的替代方案
- jar包中含有Android图片文件以及按钮等selector.xml的替代方案
- android中打包含有Activity以及资源文件的jar包在工程中调用
- Android中打包含有Activity以及资源文件的jar包在工程中调用
- Android中打包含有Activity以及资源文件的jar包在工程中调用
- android中打包含有Activity以及资源文件的jar包在工程中调用
- android中打包含有Activity以及资源文件的jar包在工程中调用
- Android工程依赖:android中打包含有Activity以及资源文件的jar包在工程中调用
- Android工程依赖:android中打包含有Activity以及资源文件的jar包在工程中调用
- Android动态加载外部jar包及jar包中图片等资源文件
- 搜索jar包中含有指定字符串的文件
- Android XML 文件 中 的 selector、shape
- Android进阶:打jar包获取assets中的资源 解决selector XML文件不能解析的问题
- eclipse导出jar包 jar包中如何导入图片和音乐等资源文件
- 打jar包获取assets中的资源 解决selector XML文件不能解析的问题
- android 使用xml selector设置按钮点击效果图片
- Android笔记- 使用XML selector 设置按钮点击效果图片
- android中 timer的替代方案!
- linux二进制安装mysql5.6
- Object.Instantiate 实例
- 深入浅出异步IO模型
- vim常用方法
- Mac OS安装mysql
- jar包中含有Android图片文件以及按钮等selector.xml的替代方案
- Redis数据备份与恢复
- !立即执行!
- 如何使Wpf浏览器应用程序被完全信任运行
- EcShop 本地安装测试
- spring事务提交问题
- 神奇的LISP语言
- redis 主从配置实例
- 深入理解java'中的String