尊旭网
当前位置: 尊旭网 > 知识 >

drawable

时间:2024-09-11 07:08:00 编辑:阿旭

android 是什么drawable

一.@代表引用资源
1.引用自定义资源。格式:@[package:]type/name
android:text="@string/hello"

2.引用系统资源。格式:@android:type/name
android:textColor="@android:color/opaque_red"
注意:其实@android:type/name是@[package:]type/name 的一个子类
二.@*代表引用系统的非public资源。格式:@*android:type/name
系统资源定义分public和非public。public的声明在:
\platforms\android-8\data\res\values\public.xml
@*android:type/name:可以调用系统定义的所有资源
@android:type/name:只能够调用publi属性的资源。
注意:没在public.xml中声明的资源是google不推荐使用的。
三.?代表引用主题属性
另外一种资源值允许你引用当前主题中的属性的值。这个属性值只能在style资源和XML属性中使用;它允许你通过将它们改变为当前主题提供的标准变化来改变UI元素的外观,而不是提供具体的值。例如:
android:textColor="?android:textDisabledColor"
注意,这和资源引用非常类似,除了我们使用一个"?"前缀代替了"@"。当你使用这个标记时,你就提供了属性资源的名称,它将会在主题中被查找,所以你不需要显示声明这个类型(如果声明,其形式就是?android:attr/android:textDisabledColor)。除了使用这个资源的标识符来查询主题中的值代替原始的资源,其命名语法和"@"形式一致:?[namespace:]type/name,这里类型可选。
四.@+代表在创建或引用资源 。格式:@+type/name
含义:”+”表示在R.java中名为type的内部类中添加一条记录。如"@+id/button"的含义是在R.java 文件中的id 这个静态内部类添加一条常量名为button。该常量就是该资源的标识符。如果标示符(包括系统资源)已经存在则表示引用该标示符。最常用的就是在定义资源ID中,例如:
@+id/资源ID名 新建一个资源ID
@id/资源ID名 应用现有已定义的资源ID,包括系统ID
@android:id/资源ID名 引用系统ID,其等效于@id/资源ID名

android:id="@+id/selectdlg"
android:id="@android:id/text1"
android:id="@id/button3"


Android中drawableStart和drawableLeft的区别

drawableLeft就是指左边
drawableStart指文字阅读开始的方向,也就是我们阅读的方向,在大部分国家是自左向右的阅读方式,此时drawableStart就相当于drawableLeft,但有些文化,像阿拉伯等是从右到左的,此时就相当于drawableRight,


不只这个属性,其他方向性属性都有start和end,如marginLeft,layout_toStartOf,layout_alignStart等等
Android 从4.2开始引入了由右到左文字的全面本地支持布局。在本地RTL支持下,可以为所有用户带来完美的应用体验,不论他们的文字书写方向是由左至右还是由右至左。
当用户切换系统语言到由右至左书写方式时,系统提供自动的应用UI布局和所有可视组件的镜像,包括文字元素的显示和输入。
应用仅需极少改变即可支持RTL布局镜像。如果应用支持这一特性,只要在应用的manifest文件中将所有的”left/right”布局属性改变为对应的”start/end”即可。系统就会根据需要处理UI


怎么把bitmap 和drawable

Bitmap - 称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565、RGB888。作为一种逐像素的显示对象执行效率高,但是缺点也很明显存储效率低。我们理解为一种存储对象比较好。

Drawable - 作为Android平下通用的图形对象,它可以装载常用格式的图像,比如GIF、PNG、JPG,当然也支持BMP,当然还提供一些高级的可视化对象,比如渐变、图形等。

A bitmap is a Drawable. A Drawable is not necessarily a bitmap. Like all thumbs are fingers but not all fingers are thumbs.
Bitmap是Drawable . Drawable不一定是Bitmap .就像拇指是指头,但不是所有的指头都是拇指一样.

The API dictates: API规定:

Though usually not visible to the application, Drawables may take a variety of forms: 尽管通常情况下对于应用是不可见的,Drawables 可以采取很多形式:

Bitmap: the simplest Drawable, a PNG or JPEG image. Bitmap: 简单化的Drawable, PNG 或JPEG图像.
Nine Patch: an extension to the PNG format allows it to specify
information about how to stretch it and place things inside of it.
Shape: contains simple drawing commands instead of a raw bitmap, allowing it to resize better in some cases.
Layers: a compound drawable, which draws multiple underlying drawables on top of each other.
States: a compound drawable that selects one of a set of drawables based on its state.
Levels: a compound drawable that selects one of a set of drawables based on its level.
Scale: a compound drawable with a single child drawable, whose overall size is modified based on the current level.

小结:

对比项 显示清晰度 占用内存 支持缩放 支持色相色差调整 支持旋转 支持透明色 绘制速度 支持像素操作

Bitmap 相同 大 是 是
是 是 慢

Drawable 相同 小 是 否
是 是 快


Drawable在内存占用和绘制速度这两个非常关键的点上胜过Bitmap

?

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

//转换Bitmap to Drawable
Bitmap bitmap = new Bitmap (...);
Drawable drawable = new BitmapDrawable(bitmap);

//转换Drawable to Bitmap
Drawable d = ImagesList.get(0);
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();

//1、Drawable → Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {

Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
//canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}

//2、从资源中获取Bitmap
Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);

//3、Bitmap → byte[]
private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}

//4、 byte[] → Bitmap
private Bitmap Bytes2Bimap(byte[] b){
if(b.length!=0){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
else {
return null;
}
}