android 杂项-备忘

Bitmap 与 Drawable之间的转换

转换Bitmap to Drawable

1
2
Bitmap bitmap = new Bitmap (...);       
Drawable drawable = new BitmapDrawable(bitmap);

转换Drawable to Bitmap

1
2
Drawable d = ImagesList.get(0);    
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
发表在 Android开发 | 标签为 | 留下评论

模拟器快捷键

F1/PgUp Menu key 菜单键
F2/PgDown Star key 星号键
F3 Call key 发送拨号键
F4 End Call key 结束通话或者说红键
Home Home key Home键
ESC Back Key 后退键
F7 Power button 电源键
F8 Disable/Enable all networking 禁止/启用所有网络
F9 Start tracing (only with -trace) 开始跟踪
F10 Stop tracing (only with -trace) 停止跟踪
Keypad 2468 Dpad arrows 方向键
Keypad 5 Dpad center 导航中建/OK键
Keypad 79 Rotate device skin 旋转设备外观
Keypad + Volume Up key 音量增加键
Keypad – Volume Down key 音量降低键
Keypad / Decrease onion alpha 除法
Keypad * Increase onion alpha 乘法
Ctrl-K Switch between ‘unicode’ and ‘raw’ keyboard modes 切换unicode和原始键盘模式

发表在 Android开发 | 标签为 | 留下评论

Android布局属性详解

各种Layout用到的一些重要的属性:

  第一类:属性值为true或false

  android:layout_centerHrizontal //水平居中

  android:layout_centerVertical //垂直居中

  android:layout_centerInparent //相对于父元素完全居中

  android:layout_alignParentBottom //贴紧父元素的下边缘

  android:layout_alignParentLeft //贴紧父元素的左边缘

  android:layout_alignParentRight //贴紧父元素的右边缘

  android:layout_alignParentTop //贴紧父元素的上边缘

  android:layout_alignWithParentIfMissing
//如果对应的兄弟元素找不到的话就以父元素做参照物

  第二类:属性值必须为id的引用名“@id/id-name”

  android:layout_below 在某元素的下方

  android:layout_above 在某元素的的上方

  android:layout_toLeftOf 在某元素的左边

  android:layout_toRightOf 在某元素的右边

  android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐

  android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐

  android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐

  android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐

  第三类:属性值为具体的像素值,如30dip,40px

  android:layout_marginBottom 离某元素底边缘的距离

  android:layout_marginLeft 离某元素左边缘的距离

  android:layout_marginRight 离某元素右边缘的距离

  android:layout_marginTop 离某元素上边缘的距离

  EditText的android:hint

  设置EditText为空时输入框内的提示信息。

  android:gravity

  android:gravity属性是对该view 内容的限定.比如一个button 上面的text. 你可以设置该text 在view的靠左,靠右等位置.以button为例,android:gravity=”right”则button上面的文字靠右

  android:layout_gravity

  android:layout_gravity是用来设置该view相对与起父view 的位置.比如一个button 在linearlayout里,你想把该button放在靠左、靠右等位置就可以通过该属性设置.以button为例,android:layout_gravity=”right”则button靠右

  android:layout_alignParentRight

  使当前控件的右端和父控件的右端对齐。这里属性值只能为true或false,默认false。

  android:scaleType:

  android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType / android:scaleType值的意义区别:

  CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示

  CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)

  CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽

  FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示

  FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置

  FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置

  FIT_XY / fitXY 把图片不按比例扩大/缩小到View的大小显示

  MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。

发表在 Android开发 | 标签为 , | 留下评论

开机动画

首先来看一下开机动画的过程:
Android 开机会出现3个画面:
a. Bootloader启动,出现小企鹅画面(reboot)(Android 1.5及以上版本已经取消加载图片);
b. Android平台启动初始化,出现”A N D R I O D”文字字样画面;
c. Android平台图形系统启动,出现含闪动的ANDROID字样的动画图片(start)。

a. Bootloader显示的小企鹅画面(reboot)(Android 1.5及以上版本已经取消加载图片);
Linux Kernel引导启动后,加载该图片。
logo.c中定义nologo,在fb_find_logo(int depth)函数中根据nologo的值判断是否需要加载相应图片。
代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
static int nologo; 
module_param(nologo, bool, 0); 
MODULE_PARM_DESC(nologo, "Disables startup logo"); 
/* logo's are marked __initdata. Use __init_refok to tell 
* modpost that it is intended that this function uses data 
* marked __initdata. 
*/ 
const struct linux_logo * __init_refok fb_find_logo(int depth) { 
    const struct linux_logo *logo = NULL; 
    if (nologo) 
        return NULL; 
        ...... 
}

相关代码在:
/kernel/drivers/video/fbmem.c
/kernel/drivers/video/logo/logo.c
/kernel/drivers/video/logo/Kconfig
/kernel/include/linux/linux_logo.h

b. 开机文字(“A N D R I O D”)
Android 系统启动后,init.c中main()调用load_565rle_image()函数读取/initlogo.rle(一张565 rle压缩的位图),如果读取成功,则在/dev/graphics/fb0显示Logo图片;如果读取失败,则将/dev/tty0设为TEXT模式,并打开/dev/tty0,输出文本“A N D R I O D”字样。
定义加载图片文件名称

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#define INIT_IMAGE_FILE "/initlogo.rle" 
int load_565rle_image( char *file_name ); 
#endif 
init.c中main()加载/initlogo.rle文件。 
 
if( load_565rle_image(INIT_IMAGE_FILE) ) {//加载initlogo.rle文件 
    fd = open("/dev/tty0", O_WRONLY);//将/dev/tty0设为text模式 
    if (fd >= 0) { 
        const char *msg; 
            msg = "\n" 
        "\n" 
        "\n" 
        "\n" 
        "\n" 
        "\n" 
        "\n"  // console is 40 cols x 30 lines 
        "\n" 
        "\n" 
        "\n" 
        "\n" 
        "\n" 
        "\n" 
        "\n" 
        "             A N D R O I D "; 
        write(fd, msg, strlen(msg)); 
        close(fd); 
    } 
}

相关代码:
/system/core/init/init.c
/system/core/init/init.h
/system/core/init/init.rc
/system/core/init/logo.c
*.rle文件的制作步骤:
a. 使用GIMP或者Advanced Batch Converter软件,将图象转换为RAW格式;
b. 使用android自带的rgb2565工具,将RAW格式文件转换为RLE格式(如:
rgb2565 -rle < initlogo.raw > initlogo.rle)。

c、开机动画(闪动的ANDROID字样的动画图片)
Android 1.5版本:Android的系统登录动画类似于Windows系统的滚动条,是由前景和背景两张PNG图片组成,这两张图片存在于手机或模拟器/system/framework /framework-res.apk文件当中,对应原文件位于/frameworks/base/core/res/assets/images/。前景图片(android-logo-mask.png)上的Android文字部分镂空,背景图片(android-logo-shine.png)则是简单的纹理。系统登录时,前景图片在最上层显示,程序代码(BootAnimation.android())控制背景图片连续滚动,透过前景图片文字镂空部分滚动显示背景纹理,从而实现动画效果。
相关代码:
/frameworks/base/libs/surfaceflinger/BootAnimation.h
/frameworks/base/libs/surfaceflinger/BootAnimation.cpp
/frameworks/base/core/res/assets/images/android-logo-mask.png Android默认的前景图片,文字部分镂空,大小256×64
/frameworks/base/core/res/assets/images/android-logo-shine.png Android默认的背景图片,有动感效果,大小512×64

Android 1.6及以上版本:
init.c解析init.rc(其中定义服务:“service bootanim/system/bin/bootanimation”),bootanim 服务由SurfaceFlinger.readyToRun()(property_set(“ctl.start”, “bootanim”);)执行开机动画、bootFinished()(property_set(“ctl.stop”, “bootanim”);)执行停止开机动画。
BootAnimation.h和BootAnimation.cpp文件放到了/frameworks/base/cmds/bootanimation目录下了,增加了一个入口文件bootanimation_main.cpp。Android.mk文件中可以看到,将开机动画从原来的SurfaceFlinger里提取出来了,生成可执行文件:bootanimation。Android.mk
代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//=============Android.mk====================== 
LOCAL_PATH:= $(call my-dir) 
include $(CLEAR_VARS) 
LOCAL_SRC_FILES:= \ 
    bootanimation_main.cpp \ 
    BootAnimation.cpp 
# need "-lrt" on Linux simulator to pick up clock_gettime 
ifeq ($(TARGET_SIMULATOR),true) 
    ifeq ($(HOST_OS),linux) 
        LOCAL_LDLIBS += -lrt 
    endif 
endif 
LOCAL_SHARED_LIBRARIES := \ 
    libcutils \ 
    libutils \ 
    libui \ 
    libcorecg \ 
    libsgl \ 
    libEGL \ 
    libGLESv1_CM \ 
    libmedia   
LOCAL_C_INCLUDES := \ 
    $(call include-path-for, corecg graphics) 
LOCAL_MODULE:= bootanimation 
include $(BUILD_EXECUTABLE) 
//==========================================

(1)adb shell后,可以直接运行“bootanimation”来重新看开机动画,它会一直处于动画状态,而不会停止。
(2)adb shell后,命令“setprop ctl.start bootanim”执行开机动画;命令“getprop ctl.start bootanim”停止开机动画。这两句命令分别对应SurfaceFlinger.cpp的两句语句:property_set(“ctl.start”,”bootanim”);和property_set(“ctl.stop”, “bootanim”);
相关文件:
/frameworks/base/cmds/bootanimation/BootAnimation.h
/frameworks/base/cmds/bootanimation/BootAnimation.cpp
/frameworks/base/cmds/bootanimation/bootanimation_main.cpp
/system/core/init/init.c
/system/core/rootdir/init.rc

但是如果简单修改一下源码,就只能放两张png的图片作为开机动画,感觉比较单调。
网上有一种方法,用于真机刷动画使用的。开机动画文件是bootanimation.zip,默认存放在system/media/下,我们自己也可放在data/local/下,如果是data/local/目录下有这文件,则会优先用这个,而且也不需要获取root权限就能放了。

这个压缩包可以在网上随便下一个。解压后可以得到part0 part1和desc.txt,其中desc.txt最重要,打开desc.txt我们可以看到,其中480 852意思是说你开机动画在屏幕先以多少的分辨率显示,我的是480 852,那么不论图片大小、比例都会按照这个分辨率来播放,所以制作的时候请注意,就算要修改也要按照比例来(最大不要超过480 852)。后面的25代表的是帧数,添多少,每秒就按这个数来播放图片!通俗的说就是图片的播放速度。(小提示中国的电视帧数都是25,而欧美的是29)
  part0 part1的作用请看下面
  p 1 0 part0 这句指令就代表这part0文件夹内的图片只按名称顺序播放一次
  p 0 0 part1 这一句指令代表着part1文件夹内的图片会循环反复播放:
  了解了各文件和文件夹的作用,我们再来看一看,动画的组成部分——图片
  图片必须使用PNG格式的,至于是PNG8 还是PNG24 32都无所谓,但必须是PNG的,所有图片必须按名称顺序排列,因为播放的顺序就是按名称的。
  图片和desc.txt都准备好后,就要开始压缩了,压缩格式选择ZIP,压缩方式选择存储(很重要!!!文件过大会导致无法启动该动画)
  desc.txt文件分析:
  480 427 30
  宽 高 帧数
  p 1 0 part0
  标志符 循环次数 阶段切换间隔时间 对应目录名
  p 0 10 part1
  标志符 循环次数 阶段切换间隔时间 对应目录名
  ==================
  标志符:
  必须是: p
  循环次数:
  0 : 表示本阶段无限循环
  阶段切换间隔时间:
  单位是一个帧的持续时间,比如帧数是30,那么帧的持续时间就是1秒/30 = 33.3毫秒。
  阶段切换间隔时间期间开机动画进程进入休眠,把CPU时间让给初始化系统使用。
  也就是间隔长启动会快,但会影响动画效果。
注意图片的大小要统一,最好和自己机子的分辨率相同(如果不会做连接的图片组 直接找个适合自己分辨率的动态图片分解出PNG格式的静态图片,用ImageReady可以批量导出);

我需要的是在源码中修改动画效果,有了上面的就很方便了。
在源码out\target\product\generic\system下建立一个文件夹media,将bootanimation.zip放入然后make编入system.img中就可以了。

发表在 Android源码 | 标签为 | 留下评论

Android 各国语言缩写-各国语言简称

android资源文件夹的写法规则:

语言缩写-国家地区缩写

语言缩写请参阅:

http://www.iso.org/iso/country_codes/iso_3166_code_lists/english_country_names_and_code_elements.htm

en 英文 
en_US 英文 (美国) 
ar 阿拉伯文 
ar_AE 阿拉伯文 (阿拉伯联合酋长国) 
ar_BH 阿拉伯文 (巴林) 
ar_DZ 阿拉伯文 (阿尔及利亚) 
ar_EG 阿拉伯文 (埃及) 
ar_IQ 阿拉伯文 (伊拉克) 
ar_JO 阿拉伯文 (约旦) 
ar_KW 阿拉伯文 (科威特) 
ar_LB 阿拉伯文 (黎巴嫩) 
ar_LY 阿拉伯文 (利比亚) 
ar_MA 阿拉伯文 (摩洛哥) 
ar_OM 阿拉伯文 (阿曼) 
ar_QA 阿拉伯文 (卡塔尔) 
ar_SA 阿拉伯文 (沙特阿拉伯) 
ar_SD 阿拉伯文 (苏丹) 
ar_SY 阿拉伯文 (叙利亚) 
ar_TN 阿拉伯文 (突尼斯) 
ar_YE 阿拉伯文 (也门) 
be 白俄罗斯文 
be_BY 白俄罗斯文 (白俄罗斯) 
bg 保加利亚文 
bg_BG 保加利亚文 (保加利亚) 
ca 加泰罗尼亚文 
ca_ES 加泰罗尼亚文 (西班牙) 
ca_ES_EURO 加泰罗尼亚文 (西班牙,Euro) 
cs 捷克文 
cs_CZ 捷克文 (捷克共和国) 
da 丹麦文 
da_DK 丹麦文 (丹麦) 
de 德文 
de_AT 德文 (奥地利) 
de_AT_EURO 德文 (奥地利,Euro) 
de_CH 德文 (瑞士) 
de_DE 德文 (德国) 
de_DE_EURO 德文 (德国,Euro) 
de_LU 德文 (卢森堡) 
de_LU_EURO 德文 (卢森堡,Euro) 
el 希腊文 
el_GR 希腊文 (希腊) 
en_AU 英文 (澳大利亚) 
en_CA 英文 (加拿大) 
en_GB 英文 (英国) 
en_IE 英文 (爱尔兰) 
en_IE_EURO 英文 (爱尔兰,Euro) 
en_NZ 英文 (新西兰) 
en_ZA 英文 (南非) 
es 西班牙文 
es_BO 西班牙文 (玻利维亚) 
es_AR 西班牙文 (阿根廷) 
es_CL 西班牙文 (智利) 
es_CO 西班牙文 (哥伦比亚) 
es_CR 西班牙文 (哥斯达黎加) 
es_DO 西班牙文 (多米尼加共和国) 
es_EC 西班牙文 (厄瓜多尔) 
es_ES 西班牙文 (西班牙) 
es_ES_EURO 西班牙文 (西班牙,Euro) 
es_GT 西班牙文 (危地马拉) 
es_HN 西班牙文 (洪都拉斯) 
es_MX 西班牙文 (墨西哥) 
es_NI 西班牙文 (尼加拉瓜) 
et 爱沙尼亚文 
es_PA 西班牙文 (巴拿马) 
es_PE 西班牙文 (秘鲁) 
es_PR 西班牙文 (波多黎哥) 
es_PY 西班牙文 (巴拉圭) 
es_SV 西班牙文 (萨尔瓦多) 
es_UY 西班牙文 (乌拉圭) 
es_VE 西班牙文 (委内瑞拉) 
et_EE 爱沙尼亚文 (爱沙尼亚) 
fi 芬兰文 
fi_FI 芬兰文 (芬兰) 
fi_FI_EURO 芬兰文 (芬兰,Euro) 
fr 法文 
fr_BE 法文 (比利时) 
fr_BE_EURO 法文 (比利时,Euro) 
fr_CA 法文 (加拿大) 
fr_CH 法文 (瑞士) 
fr_FR 法文 (法国) 
fr_FR_EURO 法文 (法国,Euro) 
fr_LU 法文 (卢森堡) 
fr_LU_EURO 法文 (卢森堡,Euro) 
hr 克罗地亚文 
hr_HR 克罗地亚文 (克罗地亚) 
hu 匈牙利文 
hu_HU 匈牙利文 (匈牙利) 
is 冰岛文 
is_IS 冰岛文 (冰岛) 
it 意大利文 
it_CH 意大利文 (瑞士) 
it_IT 意大利文 (意大利) 
it_IT_EURO 意大利文 (意大利,Euro) 
iw 希伯来文 
iw_IL 希伯来文 (以色列) 
ja 日文 
ja_JP 日文 (日本) 
ko 朝鲜文 
ko_KR 朝鲜文 (南朝鲜) 
lt 立陶宛文 
lt_LT 立陶宛文 (立陶宛) 
lv 拉托维亚文(列托) 
lv_LV 拉托维亚文(列托) (拉脱维亚) 
mk 马其顿文 
mk_MK 马其顿文 (马其顿王国) 
nl 荷兰文 
nl_BE 荷兰文 (比利时) 
nl_BE_EURO 荷兰文 (比利时,Euro) 
nl_NL 荷兰文 (荷兰) 
nl_NL_EURO 荷兰文 (荷兰,Euro) 
no 挪威文 
no_NO 挪威文 (挪威) 
no_NO_NY 挪威文 (挪威,Nynorsk) 
pl 波兰文 
pl_PL 波兰文 (波兰) 
pt 葡萄牙文 
pt_BR 葡萄牙文 (巴西) 
pt_PT 葡萄牙文 (葡萄牙) 
pt_PT_EURO 葡萄牙文 (葡萄牙,Euro) 
ro 罗马尼亚文 
ro_RO 罗马尼亚文 (罗马尼亚) 
ru 俄文 
ru_RU 俄文 (俄罗斯) 
sh 塞波尼斯-克罗地亚文 
sh_YU 塞波尼斯-克罗地亚文 (南斯拉夫) 
sk 斯洛伐克文 
sk_SK 斯洛伐克文 (斯洛伐克) 
sl 斯洛文尼亚文 
sl_SI 斯洛文尼亚文 (斯洛文尼亚) 
sq 阿尔巴尼亚文 
sq_AL 阿尔巴尼亚文 (阿尔巴尼亚) 
sr 塞尔维亚文 
sr_YU 塞尔维亚文 (南斯拉夫) 
sv 瑞典文 
sv_SE 瑞典文 (瑞典) 
th 泰文 
th_TH 泰文 (泰国) 
tr 土耳其文 
tr_TR 土耳其文 (土耳其) 
uk 乌克兰文 
uk_UA 乌克兰文 (乌克兰) 
zh 中文 
zh_CN 中文 (中国) 
zh_HK 中文 (香港) 
zh_TW 中文 (台湾)

发表在 Android开发 | 标签为 | 留下评论

获取SD卡和手机存储空间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;
import android.util.Log;
 
public class Memory extends Activity {
	public static final String TAG = "Memory";
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		updateMemoryStatus();
	}
 
	private void updateMemoryStatus() {
		String status = Environment.getExternalStorageState();
		String readOnly = "";
		// 是否只读
		if (status.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
			status = Environment.MEDIA_MOUNTED;
			readOnly = getString(R.string.read_only);
		}
		if (status.equals(Environment.MEDIA_MOUNTED)) {
			try {
				File path = Environment.getExternalStorageDirectory();
				StatFs stat = new StatFs(path.getPath());
				long blockSize = stat.getBlockSize();
				long totalBlocks = stat.getBlockCount();
				long availableBlocks = stat.getAvailableBlocks();
				// SD卡总容量
				String sdSize = formatSize(totalBlocks * blockSize);
				Log.i(TAG, "SD卡总容量: " + sdSize);
				// SD卡剩余容量
				String sdAvail = formatSize(availableBlocks * blockSize)
						+ readOnly;
				Log.i(TAG, "SD卡剩余容量: " + sdAvail);
			} catch (IllegalArgumentException e) {
				status = Environment.MEDIA_REMOVED;
			}
		}
		File path = Environment.getDataDirectory();
		StatFs stat = new StatFs(path.getPath());
		long blockSize = stat.getBlockSize();
		long availableBlocks = stat.getAvailableBlocks();
		// 手机内存剩余容量
		String memoryAvail = formatSize(availableBlocks * blockSize);
		Log.i(TAG, "手机内存剩余容量: " + memoryAvail);
		long totalBlocks = stat.getBlockCount();
		// 手机内存总容量
		String memorySize = formatSize(totalBlocks * blockSize);
		Log.i(TAG, "手机内存总容量: " + memorySize);
	}
 
	// 格式化 转化为.MB格式
	private String formatSize(long size) {
		return Formatter.formatFileSize(this, size);
	}
}
发表在 Android开发 | 标签为 , | 留下评论

“Copy” did not complete normally. Please see the log for more information.

在用android日志的时候总是弹出一个Problem Occurred错误提示窗口,

内容为:”Copy” did not complete normally. Please see the log for more information. 
Reason:Argument  not valid  
 

我用的是有道词典,退出有道词典或者关闭划词翻译就可以了。

如果是其他的翻译工具,关闭划词翻译功能。

发表在 Android开发 | 标签为 | 留下评论

Android中attr自定义属性详解

首先在你需要使用自定义属性的布局文件中定义标签,当然也可以使用系统默认标签android:

1
2
3
4
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:zkx=http://schemas.android.com/apk/res/com.zkx.test
        android:orientation="vertical" android:layout_width="fill_parent";
        android:layout_height="wrap_content">

第二行是自定义标签。

格式如上,其中“xmlns:zkx”冒号后面是标签名,在下面使用时(只对当前文件可用)

1
    <TextView  zkx:属性名/>

“com.zkx.test”是你的工程包名。

一、reference:参考指定Theme中资源ID。

1.定义:

1
2
3
    <declare-styleable name="My">
        <attr name="label" format="reference" >
    </declare-styleable>

2.使用:

1
    <Buttonzkx:label="@string/label" >

二、Color:颜色

1.定义:

1
2
3
    <declare-styleable name="My">
        <attr name="textColor" format="color" />
    </declare-styleable>

2.使用:

1
    <Button zkx:textColor="#ff0000"/>

三、boolean:布尔值

1.定义:

1
2
3
    <declare-styleable name="My">
        <attr name="isVisible" format="boolean" />
    </declare-styleable>

2.使用:

1
    <Button zkx:isVisible="false"/>

四、dimension:尺寸值

1.定义:

1
2
3
    <declare-styleable name="My">
        <attr name="myWidth" format="dimension" />
    </declare-styleable>

2.使用:

1
    <Button zkx:myWidth="100dip"/>

五、float:浮点型

1.定义:

1
2
3
    <declare-styleable name="My">
        <attr name="fromAlpha" format="float" />
    </declare-styleable>

2.使用:

1
    <alpha zkx:fromAlpha="0.3"/>

六、integer:整型

1.定义:

1
2
3
    <declare-styleable name="My">
        <attr name="frameDuration" format="integer" />
    </declare-styleable>

2.使用:

1
    <animated-rotate zkx:framesCount="22"/>

七、string:字符串

1.定义:

1
2
3
    <declare-styleable name="My">
        <attr name="Name" format="string" />
    </declare-styleable>

2.使用:

1
    <rotate zkx:pivotX="200%"/>

八、fraction:百分数

1.定义:

1
2
3
    <declare-styleable name="My">
        <attr name="pivotX" format="fraction" />
    </declare-styleable>

2.使用:

1
    <rotate zkx:Name="My name is zhang kun xiang"/>

九、enum:枚举

1.定义:

1
2
3
4
5
    <declare-styleable name="My">
        <attr name="language">
            <enum name="English" value="1"/>
        </attr>
    </declare-styleable>

2.使用:

1
    <Button zkx:language="English"/>

十、flag:位或运算

1.定义:

1
2
3
4
5
6
    <declare-styleable name="My">
        <attr name="windowSoftInputMode">
    	<flag name="stateUnspecified" value="1" />
    	<flag name = "adjustNothing" value = "0x30" />
        </attr>
    </declare-styleable>

2.使用:

1
    <activity android:windowSoftInputMode="stateUnspecified | adjustNothing">

属性定义时可以指定多种类型值:

1
2
3
    <declare-styleable name = "名称">    
	<attr name="background" format="reference|color" />
    </declare-styleable>

使用:

1
    <ImageView android:background = "@drawable/图片ID|#00FF00"/>
发表在 Android开发 | 标签为 , | 留下评论

自定义控件之Dialog

简单模仿android3.0的Dialog。
先上图。当然,我写的都是比较简单的,但是按照这种方法,各种布局都是可以实现的。




DialogView:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
public class DialogView extends LinearLayout{
 
	public static final int WIDTH = 32;
 
	private Context mContext;
 
	private LinearLayout contentView;
	private ImageView icon;
	private TextView title;
 
	private TextView ok;
	private TextView cancel;
	private ImageView img;
 
	private LayoutInflater inflater;
 
	public DialogView(Context c) {
		this(c, null);
	}
 
	public DialogView(Context c, AttributeSet a) {
		super(c, a);
 
		this.setBackgroundResource(R.drawable.dialog_full_holo_dark);
		this.setPadding(60, 50, 60, 55);
		this.setGravity(Gravity.CENTER_HORIZONTAL);
		this.setOrientation(LinearLayout.VERTICAL);
 
		mContext = c;
		inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 
		//标题布局
		LinearLayout tLayout = new LinearLayout(c);
		tLayout.setOrientation(LinearLayout.HORIZONTAL);
		tLayout.setGravity(Gravity.CENTER_VERTICAL);
 
		//标题图片
		icon = new ImageView(c);
		tLayout.addView(icon);
 
		//标题文字
		title = new TextView(c);
		title.setPadding(10, 10, 10, 10);
		title.setTextColor(Color.WHITE);
		tLayout.addView(title);
 
		this.addView(tLayout);
 
		//标题线
		ImageView hr = new ImageView(c);
		hr.setBackgroundColor(Color.parseColor("#6698fd"));
		hr.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 4));
		this.addView(hr);
 
		//内容
		contentView = new LinearLayout(c);
		this.addView(contentView);
 
		//底部按钮布局
		LinearLayout bLayout = (LinearLayout)inflater.inflate(R.layout.dialog_button, null);
 
		ok = (TextView) bLayout.findViewById(R.id.dlg_ok);
		ok.setVisibility(View.GONE);
		cancel = (TextView) bLayout.findViewById(R.id.dlg_cancel);
		cancel.setVisibility(View.GONE);
		img = (ImageView) bLayout.findViewById(R.id.dlg_img);
		img.setVisibility(View.GONE);
 
		this.addView(bLayout);
	}
 
	public void setMessage(int res) {
		setMessage().setText(res);
	}
 
	public void setMessage(String text) {
		setMessage().setText(text);
	}
 
	public void setMessage(CharSequence ch) {
		setMessage().setText(ch);
	}
 
	/**
	 * 单条消息
	 */
	private TextView setMessage() {
		LinearLayout layout = new LinearLayout(mContext);
		layout.setOrientation(LinearLayout.HORIZONTAL);
		layout.setGravity(Gravity.CENTER_VERTICAL);
		TextView tv = new TextView(mContext);
		tv.setPadding(5, 10, 10, 10);
		layout.addView(tv);
		addContentView(layout);
		return tv;
	}
 
	/**
	 * 确定按钮的监听事件
	 * @param listener
	 */
	public void setOkListener(OnClickListener listener) {
		ok.setVisibility(View.VISIBLE);
		ok.setOnClickListener(listener);
		updateImage();
	}
 
	/**
	 * 取消按钮的监听事件
	 * @param listener
	 */
	public void setCancelListener(OnClickListener listener) {
		cancel.setVisibility(View.VISIBLE);
		cancel.setOnClickListener(listener);
		updateImage();
	}
 
	private void updateImage() {
		if (ok.getVisibility() == cancel.getVisibility() && ok.getVisibility() == View.VISIBLE) {
			img.setVisibility(View.VISIBLE);
		}
	}
 
	public void setIcon(int res) {
		Drawable d = getResources().getDrawable(res);
 
		setIcon(d);
	}
 
	/**
	 * 设置标题图片
	 * @param d
	 */
	public void setIcon(Drawable d) {
		int width = d.getIntrinsicWidth();
		int height = d.getIntrinsicHeight();
		boolean flag = width > height;
		int value;
		//判断图片是否超过设定大小
		if (WIDTH < (value = flag ? width : height)) {
			Matrix m = new Matrix();
			float degrees = (float)WIDTH / (float)value;
			m.postScale(degrees, degrees);
			Bitmap bmp = ((BitmapDrawable)d).getBitmap();
			bmp = Bitmap.createBitmap(bmp, 0, 0, width, height, m, true);
 
			d = new BitmapDrawable(bmp);
		}
		this.icon.setImageDrawable(d);
	}
 
	public TextView getTitle() {
		return title;
	}
 
	public void setTitle(String text) {
		this.title.setText(text);
	}
 
	public void setTitle(CharSequence ch) {
		this.title.setText(ch);
	}
 
	public void setTitle(int res) {
		this.title.setText(res);
	}
 
	public LinearLayout getContentView() {
		return contentView;
	}
 
	public void setContentView(LinearLayout contentView) {
		this.contentView = contentView;
	}
 
	public void addContentView(View view) {
		this.contentView.addView(view);
	}
 
	public void addContentView(int res) {
		this.contentView.addView(inflater.inflate(res, null));
	}
 
}

MyDialog:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
public class MyDialog extends Activity implements OnClickListener{
 
	public static final int BTN1 = R.id.btn1;
	public static final int BTN2 = R.id.btn2;
	public static final int BTN3 = R.id.btn3;
	public static final int BTN4 = R.id.btn4;
 
	private Button mBtn1, mBtn2, mBtn3, mBtn4;
 
	private Dialog mDialog;
	private DialogView mDialogView;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        init();
 
        mDialog = new Dialog(this, R.style.FullHeightDialog);
    }
 
    private void init() {
    	mBtn1 = (Button) findViewById(BTN1);
        mBtn2 = (Button) findViewById(BTN2);
        mBtn3 = (Button) findViewById(BTN3);
        mBtn4 = (Button) findViewById(BTN4);
 
        mBtn1.setOnClickListener(this);
        mBtn2.setOnClickListener(this);
        mBtn3.setOnClickListener(this);
        mBtn4.setOnClickListener(this);
    }
 
    @Override
    public void onClick(View v) {
    	int id = v.getId();
    	switch(id) {
    	case BTN1:
    		showDialogByValue(BTN1);
    		break;
    	case BTN2:
    		showDialogByValue(BTN2);
    		break;
    	case BTN3:
    		showDialogByValue(BTN3);
    		break;
    	case BTN4:
    		showDialogByValue(BTN4);
    		break;
    	}
 
    }
 
    private void showDialogByValue(int value) {
    	mDialogView = new DialogView(this);
    	mDialogView.setTitle(getString(R.string.title_text));
		mDialogView.setIcon(R.drawable.ic_refresh_holo_dark);
    	switch(value) {
    	case BTN1:
    		mDialogView.setMessage(getString(R.string.message_text));
    		break;
    	case BTN2:
    		mDialogView.setMessage(getString(R.string.message_text));
    		mDialogView.setOkListener(new OnClickListener() {
    			@Override
    			public void onClick(View v) {
    				Toast.makeText(MyDialog.this, getString(R.string.dlg_ok), Toast.LENGTH_LONG).show();
    				mDialog.dismiss();
    			}
    		});
    		break;
    	case BTN3:
    		mDialogView.setMessage(getString(R.string.message_text));
    		mDialogView.setOkListener(new OnClickListener() {
    			@Override
    			public void onClick(View v) {
    				Toast.makeText(MyDialog.this, getString(R.string.dlg_ok), Toast.LENGTH_LONG).show();
    				mDialog.dismiss();
    			}
    		});
    		mDialogView.setCancelListener(new OnClickListener() {
    			@Override
    			public void onClick(View v) {
    				Toast.makeText(MyDialog.this, getString(R.string.dlg_cancel), Toast.LENGTH_LONG).show();
    				mDialog.dismiss();
    			}
    		});
    		break;
    	case BTN4:
    		ListView lv = new ListView(this);
    		ArrayList<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
    		for (int i = 0; i < 10; i++) {
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("key", "num " + i);
				list.add(map);
			}
    		SimpleAdapter adapter = new SimpleAdapter(this, list, android.R.layout.simple_list_item_1,
    				 new String[]{"key"}, new int[]{android.R.id.text1});
    		lv.setAdapter(adapter);
    		mDialogView.addContentView(lv);
    		break;
    	}
    	mDialog.setContentView(mDialogView);
    	mDialog.show();
    }
 
}

FullHeightDialog:

1
2
3
4
5
6
7
8
<resources>
	<style name="FullHeightDialog"
	    parent="android:style/Theme.Dialog">
	    <item name="android:windowFrame">@null</item><!--边框-->
	    <item name="android:windowNoTitle">true</item>
 	    <item name="android:windowBackground">@drawable/filled_box</item>	<!-- -->
  	</style>
</resources>
发表在 Android开发 | 标签为 , | 留下评论

onCreateContextMenu之灵活使用

功能实现:在Activity中有多个View时,只设定某个或几个ListView的onCreateContextMenu事件,并且可以取消掉该事件。

在onCreate时注册该ListView

[java]
<pre>private ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
       
lv = (ListView)findViewById(R.id.lv);
//lv.set…
registerForContextMenu(lv);
[/java]

 

在不需要时取消:

[java]
@Override
public void onClick(View v) {
if (v == btn) {
    unregisterForContextMenu(lv);
   }
}
[/java]

 

一旦被注册,在上下文需要显示的时候onCreateContextMenu都会被触发

[java]
<pre>

@Override
public void onCreateContextMenu(ContextMenu menu,

View v, ContextMenuInfo menuInfo) {
    if (v == lv) { //判断是哪个ListView

//获取当前长按的下标
    int i = ((AdapterContextMenuInfo)menuInfo).position; 

//具体操作
       menu.setHeaderTitle(list.get(i).get("TITLE").toString())
.setHeaderIcon(R.drawable.icon);

menu.add(0, menu.FIRST, Menu.NONE, "Item 1");

if (i == 2) {

menu.add(0, menu.FIRST+1, Menu.NONE, "Item 2").setCheckable(true);

       menu.add(0, menu.FIRST+2, Menu.NONE, "Item 3").setShortcut(’3′, ’3′);

} else {

SubMenu sub = menu.addSubMenu("Submenu");

       sub.add("Submenu Item");

}
    }
    super.onCreateContextMenu(menu, v, menuInfo);

[/java]

和Menu类相同, onCreateContextMenu也支持add方法,所以可以用和Activity的Menu相同的方法填入上下文菜单项——包括子菜单,但它们俩都不支持图标。你也可以指定上下文菜单头条的文本和图标。

 上下文菜单的处理:

[java]

@Override
public boolean onContextItemSelected(MenuItem item) {
     // TODO Auto-generated method stub
     return super.onContextItemSelected(item);
}

[/java]

转载源于:

http://www.20864.com/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值