Drawables

文档内容:

1. Drawables

1. Creating from resource images

2. Creating from resource XML

2. Shape Drawable

3. Nine-patch

Android offers a custom 2D graphics library for drawing and animating shapes and images.
Android提供自定义2D图像库来绘制图形和设置图形动画。
Theandroid.graphics.drawableandandroid.view.animationpackages are where you'll find the common classes used for drawing and animating in two-dimensions.
“android.graphics.drawable”和“android.view.animation”包是这方面操作常用的包。

Drawables

ADrawableis a general abstraction for "something that can be drawn." You'll discover that the Drawable class extends to define a variety of specific kinds of drawable graphics, includingBitmapDrawable,ShapeDrawable,PictureDrawable,LayerDrawable, and several more. Of course, you can also extend these to define your own custom Drawable objects that behave in unique ways.
一个Drawable是对可绘制图形的一个抽象,你会发现Drawable类派生出许多定义特殊图形的类包括:BitmapDrawable,ShapeDrawable,PictureDrawable,LayerDrawable等等还有其他。当然你也可以自己继承这些类来实现特殊用途的图形绘制。

有三中创建可绘制Drawable 的方法,下面将介绍这三种方法。

由资源文件创建

一种简单的从你的工程资源文件中引用你的图形资源,包括JPG,GIF,PNG格式的图形。这种方式你只需要将你的文件放置在res/drawable/文件中,然后直接在工程中通过它的ID号码来引用它。(E.g.,my_image.png的引用ID是:my_image).

Note:放置在res/drawable/的图形在编译期间可能会被aapt工具进行自动的无损压缩。因此,放置在这个文件夹中的图形的二进制码会改变。如果,你想读入一个图形的二进制码一遍将其转变为Bitmap,那么你可以将文件放置在res/raw/文件夹下,保证不被自动优化。

Example code

LinearLayout mLinearLayout;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Create a LinearLayout in which to add the ImageView
mLinearLayout = new LinearLayout(this);

// Instantiate an ImageView and define its properties
ImageView i = new ImageView(this);
i.setImageResource(R.drawable.my_image);
i.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

// Add the ImageView to the layout and set the layout as the content view
mLinearLayout.addView(i);
setContentView(mLinearLayout);
}

另一种情况,你想要创建Drawable来控制你的图形,你可以这样做:

Resources res = mContext.getResources();
Drawable myImage = res.getDrawable(R.drawable.my_image);

Note:每一个资源文件都只有一种状态,不论你用它实例化了多少个实例,对其中的任何一个改变,都会改变其他的文件状态。因此,在你改变之前,要设置tween animation。

Example XML

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="#55ff0000"
android:src="@drawable/my_image"/>

更多信息参照:Resources and Assets.

由xml创建

Example

Here's some XML that defines a TransitionDrawable:

<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/image_expand">
<item android:drawable="@drawable/image_collapse">
</transition>

将该xml文件保存在res/drawable/expand_collapse.xml后, 下面的代码将实例化一个 TransitionDrawable 并将它设置为 ImageView 的内容。

Resources res = mContext.getResources();
TransitionDrawable transition = (TransitionDrawable)res.getDrawable(R.drawable.expand_collapse);
ImageView image = (ImageView) findViewById(R.id.toggle_image);
image.setImageDrawable(transition);

然后可以设置该动作执行 1s :

transition.startTransition(1000);

Shape Drawable

当你想要动态创建2D图形的时候,ShapeDrawable将是很好的选择。它能让你在程序中任意绘制图形。

ShapeDrawable 继承自Drawable, 因此,你可以将它应用与任何想用的Drawable地方—如设置View的背景:setBackgroundDrawable(). 当然,你可以绘制这个图形,然后使其成为自定义的View,可以以任何方式加入你的Layout中。 ShapeDrawable 有自己的draw()方法你可以创建一个在View.onDraw()绘制ShapeDrawable的View的子类。下面实例:

public class CustomDrawableView extends View {
private ShapeDrawable mDrawable;

public CustomDrawableView(Context context) {
super(context);

int x = 10;
int y = 10;
int width = 300;
int height = 50;

mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(x, y, x + width, y + height);
}

protected void onDraw(Canvas canvas) {
mDrawable.draw(canvas);
}
}

创建好的view可以随意设置在视图中:

CustomDrawableView mCustomDrawableView;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCustomDrawableView = new CustomDrawableView(this);

setContentView(mCustomDrawableView);
}

你还可以应用在XML中:

<com.example.shapedrawable.CustomDrawableView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

更多信息,参照:Drawable Resourcesdocument.

9-patch

NinePatchDrawable是可伸展的bitmap图像,Android可以自动设置它的宽高来匹配用它来作为背景的View的background。应用9-patch图形的典型是做Button的背景 —Buttons会随着text 的增加而变化。. 9-patch文件是标准的PNG文件,但是它多了1-像素的边。他必须被存储为.9.png格式,并且保存在文件夹res/drawable/下。

边界定义了可伸展的边和静止不动的边。 你可以绘制 1-pixel 宽的黑边 在左边和上边 (对面的边应该是透明或者全白色).

This NinePatch defines one stretchable area with the left and top lines and the drawable area with the bottom and right lines. In the top image, the dotted grey lines identify the regions of the image that will be replicated in order to stretch the image. The pink rectangle in the bottom image identifies the region in which the contents of the View are allowed. If the contents don't fit in this region, then the image will be stretched so that they do.

TheDraw 9-patchtool offers an extremely handy way to create your NinePatch images, using a WYSIWYG graphics editor. It even raises warnings if the region you've defined for the stretchable area is at risk of producing drawing artifacts as a result of the pixel replication.

Example XML

Here's some sample layout XML that demonstrates how to add a NinePatch image to a couple of buttons. (The NinePatch image is saved asres/drawable/my_button_background.9.png

<Button id="@+id/tiny"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:text="Tiny"
android:textSize="8sp"
android:background="@drawable/my_button_background"/>

<Button id="@+id/big"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:text="Biiiiiiig text!"
android:textSize="30sp"
android:background="@drawable/my_button_background"/>

更多相关文章

  1. Android扫描文件
  2. android子菜单&&组菜单
  3. 使用“aapt dump”查看APK内容
  4. android绘图网格线java写法
  5. 使用Toast显示提示信息框
  6. Android(安卓)RatingBar 使用示例
  7. Linux调试工具之:ftrace
  8. android笔记4-xml解析
  9. No 93 · android xml的生成和解析

随机推荐

  1. Android NDK 是什么
  2. android重启流程
  3. [置顶] [Android基础]Android中Handler的用
  4. Android执行 shell command
  5. Android resource linking failed. error
  6. Android系统升级流程
  7. Android 调用系统相机 失败
  8. Android支持java8的设置方法
  9. android进行录音功能并保存播放
  10. Android中AlertDialog用法实例分析