Android App Resources

Source: Internet
Author: User
<span id="Label3"></p><p><p>Java has just started to learn people directly in the Java source code using "hello" and 123 types of strings and Integers. But long time will forget the reason of the original definition, experienced may define the string constant resultset.type_forward_only, have experience to know the Reason. This has reduced coupling, and Android has made further improvements to these string constants, numeric constants: the use of various resource string constants, color resources, array resources, Menu resources are centrally placed in the Res folder. The application directly uses the values defined in these Resources.</p></p><p><p><span style="font-size: 24px"><strong>Resource types and Storage methods</strong></span></p></p><p><p>Android app resources can be divided into two main categories:</p></p> <blockquote style="margin: 0 0 0 40px; border: none; padding: 0px"> <blockquote style="margin: 0 0 0 40px; border: none; padding: 0px"> <p><span style="color: #ff6666">1. Native resources not available through the R inventory category. Save under the Assets folder</span></p> <p><span style="color: #ff6666">2. resources available through the R resource list class, saved in the Res folder</span></p> </blockquote> </blockquote><p><p>Resource Storage:</p></p><p><p><span style="color: #ff6666">/res/animator</span> <span style="white-space: pre"></span> Storing XML files that define property animations</p></p><p><p><span style="color: #ff6666">/res/anim</span> <span style="white-space: pre"></span> Defining an XML file for motion tweens<br></p></p><p><p><span style="color: #ff6666">/res/color</span> <span style="white-space: pre"></span> Storing an XML file that defines a color list under different colors<br></p></p><p><p><span style="color: #ff6666">/res/drawable</span> <span style="white-space: pre"></span> store bitmaps (png,jpg,gif) and Drawable objects (bitmapdrawable,ninepatchdrawable,statelistdrawable, Animationdrawable,drawable)<br></p></p><p><p><span style="color: #ff6666">/res/layout</span> <span style="white-space: pre"></span> layout files that store various user interfaces<br></p></p><p><p><span style="color: #ff6666">/res/menu</span> <span style="white-space: pre"></span> Resources for the various menus defined by the application, including Options menus, submenus, context Menu Resources<br></p></p><p><p><span style="color: #ff6666">/res/raw<span style="white-space: pre"> </span> </span> stores random types of native resources (such as audio, video, and so on) in Java code to call the resource Object's Openrawresource (int Id) method to get the resource binary input Stream.<br></p></p><p><p><span style="color: #ff6666">/res/values</span> <span style="white-space: pre"></span> An XML file that holds simple values, including strings, arrays, integers, colors, and so On. are stored under the <resources></resources> node. It is recommended that different types be stored in different XML files.</p></p><p><p><br></p></p><p><p><br></p></p><p><p><strong><span style="font-size: 24px">How to Use:</span></strong></p></p><p><p><span style="font-size: 14px"><em>In the Java code</em></span></p></p><p><p><package_name>. R.<resource_type>.<resouece_name></p></p><p><p>For Example: Android. R.layout.simple_expandable_list_item_1 references the package of the R class where the Java program resides, the package name can be omitted</p></p><p><p>In fact, the above notation does not refer to real resource Objects. instead, the API of the Andoird app agrees to replace the application resources directly with the resource list entries of type int. Get Real-world resources with the resource class (android resources to access the explorer). Resource mainly provides two types of methods:</p></p> <blockquote style="margin: 0 0 0 40px; border: none; padding: 0px"> <blockquote style="margin: 0 0 0 40px; border: none; padding: 0px"> <p>GetXxx (int id): Get actual resources based on voluntary list ID</p> <p>Getassets (): get access to Assetmanager objects for resources Under/assets/folder</p> </blockquote> </blockquote><p><p>Resource called by the context GetResource () method to get</p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">Get resourceresources res = getresources ();//get string resource maintitile = Res.gettext (r.string.main_titile);// Get drawable resource res.getdrawable (r.drawable.ic_launcher);//get array resource int [] arr = Res.getintarray (r.array.books);</pre></pre><p><p></p></p><p><p><br></p></p><span style="font-size: 18px"><span style="font-size: 18px"><em>in XML</em></span></span><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><resources><color name= "red" > #ff00 </color><string name= "hello" >hello</string></ Resources></pre></pre>Control reference<p><p></p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"> <textview android:layout_width= "match_parent" android:layout_height= "wrap_content" android: textcolor= "@color/red" > </TextView></pre></pre><br><br><span style="font-size: 18px"><span style="font-size: 18px"><em>Array Resource</em></span></span><p><p></p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><listview android:id= "@+id/list" android:layout_width= "match_parent" android:layout_height= " Wrap_content " android:entries=" @array/test "></ListView></pre></pre><br><pre name="code" class="html"><pre name="code" class="html"><resources> <string-array name= "test" > <item>1</item> <item>2</ item> <item>3</item> <item> @string/hello_world</item> </string-array ></pre></pre><br>The above code can see that resources can be referenced before each other.<p><p></p></p><p><p><br></p></p><p><p><span style="font-size: 18px"><em>Statelistdrawable Resources</em></span></p></p><p><p>Statelistdrawable is used to organize multiple drawable objects. When using statelistdrawable as the background of the target Component. When a foreground picture is present, the Drawable object that is displayed by the Statelistdrawable object will actively switch with the target component state Changing.</p></p><p><p>The root element of the XML file that defines the Statelistdrawable object is <selector..../> that element can include more than one <item..../> can specify</p></p> <blockquote style="margin: 0 0 0 40px; border: none; padding: 0px"> <blockquote style="margin: 0 0 0 40px; border: none; padding: 0px"> <p>Android:color or android:drawable specifies a color or drawable object.</p> <p> </p> <p>Android:state_xxx: specify a specific state (active, checked, last, pressed, and so On)</p> </blockquote> </blockquote><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:id=" @+id/container " android:layout_width=" match_parent " Android : layout_height= "match_parent" android:orientation= "vertical" > <edittext android:layout_width = "match_parent" android:layout_height= "wrap_content" android:text= "demacia" android:textcolor= "@ Drawable/my_color "> </EditText> <edittext android:layout_width=" match_parent " android:layout_height= "wrap_content" android:text= "galen" android:textcolor= "@drawable/my_color" > </EditText></LinearLayout></pre></pre><p><p></p></p><p><p><br></p></p><pre name="code" class="html"><pre name="code" class="html"><?xml version= "1.0" encoding= "utf-8"?<p></p><p>><selector xmlns:android= "http://schemas.android.com/apk/res/android" > <!--get Focus color-->< Item android:state_focused= "true" android:color= "#f44" ></item><!--loses Focus when Color--><item android:state _focused= "false" android:color= "#455" ></item></selector></p></pre></pre><br><span style="font-size: 18px"><span style="font-size: 18px"><em>layerdrawable Resources</em></span></span><p><p>It's a bit similar to Statelistdrawable. Layerdrawable can also include a drawable array, so the system will draw them in the order of the array of these drawable objects, and the Drawable object with the largest index will be drawn at the Top.</p></p><p><p>The root element of the XML file that defines the Layerdrawable object is <layer-list...../>, which can include multiple <item..../> elements that can be specified such as the following properties:</p></p><p><p>android:drawable: Specifies the Drawable object as one of the layerdrawable elements</p></p><p><p>Android:id specifying a unique identity for the Drawable object</p></p><p><p>Android:buttom|top|button: They are used to specify a length Value. Used to specify that the Drawable object is drawn to the specified location of the target Component.</p></p><p><p></p></p><p><p><br></p></p><p><p>Activity_main.xml</p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:id=" @+id/container " android:layout_width=" match_parent " Android : layout_height= "match_parent" android:orientation= "vertical" > <seekbar android:id= "@+id/bar" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:max= " android:progressdrawable= "@drawable/my_bar"/> <imageview android:layout_width= "match_parent" android:layout_height= "match_parent" android:src= "@drawable/layout_logo"/> </ Linearlayout></pre></pre><br>My_bar.xml<p><p></p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><?xml version= "1.0" encoding= "utf-8"? ><layer-list xmlns:android= "http://schemas.android.com/apk/res/ Android > <!--define track background--><item android:id= "@android: id/background" android:drawable= "@drawable/ic_ Launcher "></item><item android:id=" @android: id/progress "android:drawable=" @drawable/b "></item > </layer-list></pre></pre><br>Layout_logo.xml<p><p></p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><?<p></p><p>XML version= "1.0" encoding= "utf-8"? ><layer-list xmlns:android= "http://schemas.android.com/apk/res/android" > <item> <bitmap android:gravity= "center" android:src= "@drawable/ic_launcher"/> </item> <item android:top= "25dp" android:left= "25dp" > <bitmap android:gravity= " Center " android:src=" @drawable/ic_launcher "/> </item> <item android:top=" 50dp "android: left= "50dp" > <bitmap android:gravity= "center" android:src= "@drawable/ic_launcher"/> </item></layer-list></p></pre></pre><br><p><p></p></p><p><p>:</p></p><p><p><br></p></p><p><p><br></p></p><p><p>The above code shows that ImageView src can specify a picture. It can also be a layer (layers), and the layer item defines each displayed bitmap picture and its location <bitmap android:gravity= "center" > Center Here is centered relative to the size of the ImageView space container.</p></p><p><p><br></p></p><p><p><span style="font-size: 18px"><em>Sharpdrawable Resources</em></span></p></p><p><p>Before the UI simple beautification mentioned, this will not say</p></p><p><p><br></p></p><p><p><span style="font-size: 18px"><em>Clipdrawable Resources</em></span></p></p><p><p>Clipdrawable represents a "picture fragment" from another bitmap, defining the Clipdrawable object using the <clip....../> element in the XML file, with the following syntax:</p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><?xml version= "1.0" encoding= "utf-8"? ><clip xmlns:android= "http://schemas.android.com/apk/res/android" android:drawable= "@drawable/ic_launcher" android:cliporientation= "horizontal|vertical" android: gravity= "top|bottom|right" ></clip></pre></pre><br>When using the clipdrawable object, you can call the Setlevel (int Level) method to set the size of the intercept, and the 0 truncated picture is Empty. 10000, capture the entire picture<p><p></p></p><p><p>Take advantage of the level of slowly unfolding scenery, when you click on the image, you actively display all</p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">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);//gets the Clipdrawable object displayed by the image final clipdrawable drawable = (clipdrawable) img.getdrawable (); final Handler Handler = new Handler () {@Overridepublic void handlemessage (Message msg) {super.handlemessage (msg); if (msg.what= =0x123) {//change The value of level of clipdrawable drawable.setlevel (drawable.getlevel () +20);}}; Final Timer timer = new Timer (); timer.schedule (new timertask () {@Overridepublic void run () {message msg = new Message (); ms G.what=0x123;//sends a message informing the application to change the level value of the Clipdrawable object Handler.sendmessage (msg), if (drawable.getlevel () >=10000) { Timer.cancel (),}}}, 0, img.setonclicklistener (new onclicklistener () {@Overridepublic void OnClick (View V) { Drawable.setlevel (10000);}});}}</pre></pre>The above code is in fact the moment after the change in the value of the reduction in the display of the formation of the slowly expanding Effect.<br><br><p><p></p></p><p><p><span style="font-size: 18px"><em>Animationdrawable Resources</em></span></p></p><p><p>Animationdrawable represents an animation that supports frame animations and tweened animations.</p></p><p><p></p></p><p><p>The XML resource file that defines the tweened animation takes the <set..../> element as the root Element. This element can specify A:lpha transparency, scale scaling, translate displacement, ratate Rotation. The animation holds the path res/animi. ADT does not have this path when creating an Android app, and it needs to be created Manually.</p></p><p><p>Mainactivity.java</p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="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 Animation resource final Animation Animation = animationutils.loadanimation (this, R.anim.set_test);//set the end state after the animation is complete Animation.setfillafter (true); Button bn = (button) Findviewbyid (r.id.bn); bn.settext ("start animation"); bn.setonclicklistener (new onclicklistener () {@ overridepublic void OnClick (View v) {//starts animation imageview1.startanimation (animation);}});}}</pre></pre><br>Set_test.xml<p><p></p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><?<p></p><p>XML version= "1.0" encoding= "utf-8"? ><set xmlns:android= "http://schemas.android.com/apk/res/android" android:interpolator= "@android: anim/linear_interpolator" android:duration= "> <!--define Zoom transform-- > <scale android:fromxscale= "1.0" android:toxscale= "1.4" android:fromyscale= "1.0" Android : toyscale= "0.6" android:pivotx= "50%" android:pivoty= "50%" android:fillafter= "true" android: duration= "" "/><!--defined Displacement transformation--><translate android:fromxdelta=" 130 " android:toxdelta=" android:fromydelta= "android:toydelta=" -80 "android:duration=" /></set></p></pre></pre><br><span style="font-size: 18px">property <span style="font-size: 18px"><em>Animation Resources</em></span></span><p><p></p></p><p><p>Animation represents an attribute Animation. But it is just an abstract class, which generally uses its subclasses: animatorset, valueanimator, objectanimator, Timeanimator.</p></p><p><p>Need to animator Folder. There was no time to create the Project. Manually Created.</p></p><p><p>Defining property Animation An XML resource can be a root element in a random one of three elements</p></p><p><p><set...../> It is a parent element that is used to include other <objectAnimator> or <set.../> child Elements. The resource defined by this property represents the Animatorset object</p></p><p><p><objectAnimator.../> for defining Objectanimtor animations</p></p><p><p><animator.../> for defining Valueanimator animations</p></p><p><p>The background color has been changing the demo</p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">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 Animation resource Objectanimator Coloranim = (objectanimator) animatorinflater.loadanimator (context, r.animator.object_test); Coloranim.setevaluator (new argbevaluator ());//apply Property Animation Coloranim.settarget to the view itself (this);//start animation Coloranim.start ();}}}</pre></pre>Res/animator/object_test.xml<p><p></p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><?<p></p><p>XML version= "1.0" encoding= "utf-8"?</p><p>><objectanimator xmlns:android= "http://schemas.android.com/apk/res/android" android:propertyname= " BackgroundColor " android:duration=" " android:valuefrom=" #FF8080 " android:valueto=" #8080FF " android:valuetype= "inttype" android:repeatcount= "infinite" android:repeatmode= "reverse" > </objectAnimator></p></pre></pre><br><br><p><p>Summarize:</p></p><p><p>Resource Name <span style="white-space: pre"></span> folder <span style="white-space: pre"></span> root node <span style="white-space: pre"></span></p></p><p><p>Statelistdrawable <span style="white-space: pre">drawable <selector..../></span></p></p><p><p>Layerdrawable<span style="white-space: pre"> drawable <layer-list..../></span></p></p><p><p>Property Animation<span style="white-space: pre"> Animator </span><objectAnimator.../><br></p></p><p><p>Sharpdrawable<span style="white-space: pre"> <span style="white-space: pre">drawable </span><shape...../></span><br></p></p><p><p>Animationdrawable <span style="white-space: pre">anim <set..../></span><br></p></p><br> <blockquote style="margin: 0 0 0 40px; border: none; padding: 0px"> <blockquote style="margin: 0 0 0 40px; border: none; padding: 0px"> <p><br></p> </blockquote> </blockquote> <p><p>Android App Resources</p></p></span>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.