Add a new image resource to the resource folder of Android.

Source: Internet
Author: User
Tags sleep function

I have just read a simple Android example of the next frame layout. After a long time, I don't know how to add the image to the drawable in the resource.
For example, how to add a new image resource in the Res/drawable folder of testdemo.

Copy the image to bin \ res \ drawable-hdpi orBin \ res \Click drawable-mdpi and refresh the image in eclipse.

I found a solution for loading image resources online:

Directly copy the image resource to be added, right-click the Res/drawable folder, and select "Paste" to copy the image.

The following is a simple frame layout Android Application example, to achieve a bird flying animation effect, refer to the "big words enterprise Android Application Development practices" 15.2.5 frame layout (framelayout) P110-113 page.CodeAs follows:

Package COM. CCF. birdframe; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. app. activity; import android. graphics. drawable. drawable; import android. view. menu; import android. view. view; import android. widget. framelayout; public class birdactivity extends activity {framelayout frame = NULL; private Boolean flag = true; // This class is called cyclically between two methods, and the interface is updated continuously. Handler {int I = 0; Public void handlemessage (Message MSG) {I ++; // shows show (I % 7) in sequence ); // call the sleep function sleep (50);} public void sleep (long delaymillis) {// determine whether to continue flying if (FLAG) {// actually calls handlemessagesendmessagedelayed (obtainmessage (0), delaymillis );}}} // this method is called to update the foreground image void show (Int J) of the frame layout {// obtain the seven images drawable mybird1 = getresources (). getdrawable (R. drawable. mybird1); drawable mybird2 = getresources (). getdrawable (R. drawable. mybird2); drawable mybird3 = getresources (). getdrawable (R. drawable. mybird3); drawable mybird4 = getresources (). getdrawable (R. drawable. mybird4); drawable mybird5 = getresources (). getdrawable (R. drawable. mybird5); drawable mybird6 = getresources (). getdrawable (R. drawable. mybird6); drawable mybird7 = getresources (). getdrawable (R. drawable. mybird7); // set different foreground switches (j) {Case 0: frame. setforeg Round (mybird1); break; Case 1: frame. setforeground (mybird2); break; Case 2: frame. setforeground (mybird3); break; Case 3: frame. setforeground (mybird4); break; Case 4: frame. setforeground (mybird5); break; Case 5: frame. setforeground (mybird6); break; Case 6: frame. setforeground (mybird7); break ;}@overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontent View (R. layout. activity_bird); frame = (framelayout) findviewbyid (R. id. frame); // create a handler subclass object and call its method final myhandler = new myhandler (); myhandler. sleep (50); // set a click event for the frame. When it is hit, the frame is switched between flying and pausing. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubflag =! Flag; myhandler. Sleep (50 );}});}}

The seven images are as follows:

Mybird1 mybird2 mybird3 mybird4

 

Mybird5 mybird6 mybird7

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.