3.App Resources-Resource Types/Drawable Resources

1. Drawable Resources

  There are several different types of drawables as follow

2. Bitmap

  A bitmap image. Android supports bitmap files in a three formats: .png (preferred), .jpg (acceptable), .gif (discouraged).

  Bitmap files may be automatically optimized with lossless image compression by the aapt tool during the build process.

  If you plan on reading an image as a bit stream in order to convert it to a bitmap, put your images in the res/raw/ folder instead, where

    they will not be optimized.

3. Nine-Patch

  A NinePatch is a PNG image in which you can define stretchable regions that Android scales when content within the View exceeds the normal

    image bounds. You typically assign this type of image as the background of a View that has at least one dimension set to "wrap_content",

    and when the View grows to accomodate the content, the Nine-Patch image is also scaled to match the size of the View. An example

    use of a Nine-Patch image is the background used by Android‘s standard Button widget, which must stretch to accommodate the text

    (or image) inside the button.

4. Layer List

  A LayerDrawable is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list

    —the last drawable in the list is drawn on top.

//res/drawable/layers.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
      <bitmap android:src="@drawable/android_red"
        android:gravity="center" />
    </item>
    <item android:top="10dp" android:left="10dp">
      <bitmap android:src="@drawable/android_green"
        android:gravity="center" />
    </item>
    <item android:top="20dp" android:left="20dp">
      <bitmap android:src="@drawable/android_blue"
        android:gravity="center" />
    </item>
</layer-list>

   This layout XML applies the drawable to a View:

<ImageView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/layers" />

  The result is a stack of increasingly offset images:

  

5. State List

  A StateListDrawable is a drawable object defined in XML that uses a several different images to represent the same graphic, depending

    on the state of the object

//res/drawable/button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>
<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@drawable/button" />

6. Transition Drawable 

  A TransitionDrawable is a drawable object that can cross-fade between the two drawable resources.

  Each drawable is represented by an <item> element inside a single <transition> element. No more than two items are supported. To

    transition forward, call startTransition(). To transition backward, call reverseTransition().

//res/drawable/transition.xml
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/on" />
    <item android:drawable="@drawable/off" />
</transition>
<ImageButton
    android:id="@+id/button"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/transition" />
//And the following code performs a 500ms transition from the first item to the second:
ImageButton button = (ImageButton) findViewById(R.id.button);
TransitionDrawable drawable = (TransitionDrawable) button.getDrawable();
drawable.startTransition(500);

7. ...

时间: 2024-07-30 23:06:56

3.App Resources-Resource Types/Drawable Resources的相关文章

More Resource Types

This page defines more types of resources you can externalize, including: Bool XML resource that carries a boolean value. Color XML resource that carries a color value (a hexadecimal color). Dimension XML resource that carries a dimension value (with

简单谈谈Resource,Drawable和Bitmap之间的转换

一直接触这些东西,还是归个类整理一下比较好. Resource -> Drawable Drawable draw1 = this.getResources().getDrawable(R.drawable.icon); Drawable -> Bitmap 1. static Bitmap drawableToBitmap(Drawable drawable) // drawable 转换成bitmap { int width = drawable.getIntrinsicWidth();/

3.App Resources-Resource Types/String Resources

1. String //saved at res/values/strings.xml<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello!</string> </resources> //This layout XML applies a string to a View:<Te

Android系统Resource之Drawable

1.android系统提供的Drawable资源非常丰富,而且效果随着版本提升以有增多.从笔者测试来看,在使用系统的提供的Drawable时,高度和宽度最好使用wrap_content,这样防止有的图片失真. resource reference: In Java: R.drawable.filename In XML: @[package:]drawable/filename [email protected]:drawable/***介绍: alert_light_frame: alert_

Android - Resource 之 Drawable小结

本篇直接选择性地翻译官方开发指南 ============================= Drawable有十个类型,如下 - Bitmap file:这个简单,也可以用xml来更详细的定义,先按下不写.... - Nine-Patch File: 这个是一个自定义绽放区域的Bitmap,一般以.9.png为后缀,可能用 SDK/tools/draw9patch.bat来生成,实际上这个工具在图片外边缘添加一个一像素的边框用来标记“缩放区域”,如下 调整一下后,会得到这样的效果: - Lay

android.graphics.drawable.Drawable注释翻译

/* * Copyright (C) 2006 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://w

Android Context 是什么?

andorid 开发(42)  版权声明:本文为博主原创文章,未经博主允许不得转载. [转载请注明出处:http://blog.csdn.net/feiduclear_up CSDN 废墟的树] PS:修该了一些有误区的地方. 引言 Context对于Android开发人员来说并不陌生,项目中我们会经常使用Context来获取APP资源,创建UI,获取系统Service服务,启动Activity,绑定Service,发送广播,获取APP信息等等.那么Context到底是什么?Context又是怎

.net烦恼小问题之XML读取异常

现有一xml: <?xml version="1.0" encoding="utf-8"?> <Package xmlns="http://schemas.microsoft.com/appx/2010/manifest" xmlns:build="http://schemas.microsoft.com/developer/appx/2012/build" IgnorableNamespaces="

About Oracle Cluster Registry

Oracle Cluster Registry (OCR) is a file that contains information about the cluster node list and instance-to-node mapping information. OCR also contains information about Oracle Clusterware resource profiles for resources that you have customized. T