Android Application Resources
Java beginners directly use strings and Integers of the hello and 123 types in the Java source code, but after a long time, they will forget the reason for the definition. Experienced users may define the String constant ResultSet. TYPE_FORWARD_ONLY: If you have experience, you can see the cause. This write method has reduced coupling. Android further improves these string constants and numerical constants: put all the resource string constants, color resources, array resources, and menu Resources in the res directory, and the application uses the values defined in these resources directly.
Resource types and storage methods
Android Application resources can be divided into two categories:
1. native resources that cannot be accessed through the R list class are saved in the assets Directory.
2. resources that can be accessed through the R resource list class are saved in the res directory.
Resource storage:
/Res/animator stores the XML file that defines the property Animation
/Res/anim defines the XML file of the compensation Animation
/Res/color stores the XML file that defines the color list of different colors
/Res/drawable stores Bitmap (png, jpg, gif) and Drawable objects (BItmapDrawable, NinePatchDrawable, StateListDrawable, AnimationDrawable, Drawable)
/Res/layout: stores layout files on various user interfaces
/Res/menu stores the resources of various menus defined by the application, including the option menu, sub menu, and context menu resources.
/Res/raw stores any type of native resources (such as audio and video). In Java code, call the openRawResource (int id) method of the Resource object to obtain the binary input stream of the Resource.
/Res/values: XML files that store simple values, including strings, arrays, integers, and colors. Are stored in Node. We recommend that you store different types in different XML files.
Usage:
Java code
. R. .
For example, android. R. layout. simple_expandable_list_item_1 references the R class package of the Java program. The package name can be omitted.
In fact, the above statement does not reference a real resource object, but allows the Andoird application API to directly use the int type resource list item to replace the application resource. You can use the Resource class to obtain actual resources (Android Resource Access Manager ). Resource mainly provides two methods:
GetXxx (int id): obtains the actual resource based on the voluntary list ID.
GetAssets (): gets the AssetManager object for accessing resources in the/assets/directory.
The Context calls the getResource () method to obtain the Resource.
// Obtain ResourceResources res = getResources (); // obtain the String resource String mainTitile = res. getText (R. string. main_titile); // gets the Drawable resource res. getDrawable (R. drawable. ic_launcher); // retrieves the array resource int [] arr = res. getIntArray (R. array. books );
In XML
#ff00
Hello
Control Reference
Array Resources
1
2
3
@string/hello_world
The code above shows that resources can be referenced by each other before.
StateListDrawable Resource
StateListDrawable is used to organize multiple Drawable objects. When StateListDrawable is used as the background and foreground image of the target component, the Drawable object displayed by multiple StateListDrawable objects automatically switches as the status of the target component changes.
The root element of the XML file that defines the StateListDrawable object is This element can contain multiple You can specify
Android: color or android: drawable specifies the color or Drawable object.
Android: state_xxx: Specifies a specific State (active, checked, last, pressed, etc)
LayerDrawable Resource
Similar to StateListDrawable, LayerDrawable can also contain a Drawable array. Therefore, the system will plot the Drawable objects in the array order, the Drawable object with the largest index will be drawn at the top.
The root element of the XML file that defines the LayerDrawable object is This element can contain multiple You can specify the following attributes:
Android: drawable: Specifies the Drawable object that is one of the LayerDrawable elements.
Android: id specifies a unique identifier for the Drawable object
Android: buttom | top | button: they are used to specify a length value to draw the Drawable object to the specified position of the target component.
Activity_main.xml
My_bar.xml
Layout_logo.xml
:
The code above shows that the src of ImageView can specify an image or a layer. The layer item defines the Bitmap image and position of each display. The center here refers to the center of the container size relative to the ImageView space.
SharpDrawable Resources
I mentioned the simple UI beautification before.
ClipDrawable Resource
ClipDrawable is used to capture an image clip from another bitmap. In the XML file, it is used to define the ClipDrawable object. Element, Syntax:
When you use a ClipDrawable object, you can use the setLevel (int level) method to set the size of the captured image. The value 0 indicates that the captured image is empty, and the value 10000 indicates that the entire image is captured.
When you click an image, the system automatically displays all the scenery displayed by level.
Public class MainActivity extends Activity {private ImageView img; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); img = (ImageView) findViewById (R. id. img); // obtain the ClipDrawable object final ClipDrawable drawable = (ClipDrawable) img displayed in the image. getDrawable (); final Handler handler = new Handler () {@ Overridepublic void handleMessage (Message msg) {super. handleMessage (msg); if (msg. what = 0x123) {// modify the level value of ClipDrawable drawable. setLevel (drawable. getLevel () + 20) ;}}; final Timer timer = new Timer (); timer. schedule (new TimerTask () {@ Overridepublic void run () {Message msg = new Message (); msg. what = 0x123; // send a message to the application to modify the level value of the ClipDrawable Object handler. sendMessage (msg); if (drawable. getLevel ()> = 10000) {timer. cancel () ;}}, 0,300); img. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {drawable. setLevel (10000 );}});}}
The above code is actually a simple display of the effect formed by the value after the cut is changed.
AnimationDrawable Resource
AnimationDrawable represents an animation that supports Frame Animation and compensation animation.
Define the XML resource file of the animation As the root element, this element can specify a: lpha transparency, scale scaling, translate displacement, ratate rotation. Animation storage path res/animi. This path is not included when ADT is used to create an android application. You must manually create this path.
MainActivity. java
Public class MainActivity extends Activity {ImageView imageView1; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); imageView1 = (ImageView) findViewById (R. id. imageView1); // load the Animation resource final animation Animation = AnimationUtils. loadAnimation (this, R. anim. set_test); // you can specify the animation end state after the animation ends. setFillAfter (true); Button bn = (Button) findViewById (R. id. bn); bn. setText (start animation); bn. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// start animation imageView1.startAnimation (animation );}});}}
Set_test.xml
Property Animation Resource
Animation represents an attribute Animation, but it is only an abstract class and usually uses its subclass: AnimatorSet, ValueAnimator, ObjectAnimator, TimeAnimator. The animator directory is required. It is not found during project creation. Create manually.
Defining property animation XML resources can use any of the three elements as the root element.
It is a parent element used to include other Or Child element. The resource defined by this attribute represents the AnimatorSet object.
Used to define ObjectAnimtor Animation
Used to define a ValueAnimator Animation
Demo of constantly changing background color
Public class MainActivity extends Activity {ImageView imageView1; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); LinearLayout l = (LinearLayout) findViewById (R. id. container); l. addView (new MyAnimtionView (this);} public class MyAnimtionView extends View {public MyAnimtionView (Context context) {super (context); // load the animation resource ObjectAnimator colorAnim = (ObjectAnimator) animatorInflater. loadAnimator (context, R. animator. object_test); colorAnim. setEvaluator (new ArgbEvaluator (); // apply the property animation colorAnim to the View itself. setTarget (this); // starts the animation colorAnim. start ();}}}
Res/animator/object_test.xml
Summary:
Resource Name directory Root Node
StateListDrawable Drawable
LayerDrawable Drawable
Property Animation animator
SharpDrawable Drawable
AnimationDrawable anim