Lift ProgressBar, presumably we are familiar with, use is more convenient, directly in the XML file reference, and then add properties, run OK, although the use of ProgressBar is very convenient but in our development of each application basically has its own main style, If you use the system's own effect chart, give people the feeling is and the overall style too does not match, looks very awkward, our own development also feel bad, so think of the custom effect, in fact, custom ProgressBar effect is not difficult, probably can be divided into three steps to go:
A , define the animation set
by using animation-list under the Anim folder.
<?xml version= "1.0" encoding= "UTF-8"?> <animation-list android:oneshot=
"false" xmlns:android= "http://" Schemas.android.com/apk/res/android ">
<item android:duration=" "android:drawable=" @drawable/circle_ 10001 "/>
<item android:duration=" android:drawable= "@drawable/circle_10002"/> <item
android:duration= "android:drawable=" @drawable/circle_10003 "/> <item android:duration=
" "Android: drawable= "@drawable/circle_10004"/>
<item android:duration= "android:drawable=" @drawable/circle_ 10005 "/>
<item android:duration=" android:drawable= "@drawable/circle_10006"/> <item
Android:duration= "android:drawable=", "@drawable/circle_10007"/>
</animation-list>
Definition of style in Style.xml files
[HTML] View plain copy on the code to see a piece of a snippet derived from my Code
<style name= "Circleprogressstyle" parent= "@android: Style/widget.progressbar.large" >
<item name= " Android:indeterminatedrawable "> @anim/anim_progress_circle</item>
</style>
Third, set its style in an XML file that uses ProgressBar
[HTML] View plain copy on the code to see a piece of a snippet derived from my Code
<progressbar
android:layout_width= "50dip"
android:layout_height= "50dip"
style= "@style Circleprogressstyle "/>
Through the above steps, basically can achieve their desired effect, which I have always been used in the development of the way, of course, the use of other methods can also achieve this effect, today I mainly want to talk about using animationdrawable and imageview combination to achieve the above effect , so the above methods are no longer detailed (if you want to know other ways, leave your email, to your letter).
Now to get to the point, first look at the project structure
Under the Drawable folder, create a new Circle.xml file that reads as follows:
[HTML] View plain copy on the code to see a piece of a snippet derived from my Code
<?xml version= "1.0" encoding= "UTF-8"?> <animation-list android:oneshot= "false" xmlns:android= "Http://schem"
As.android.com/apk/res/android "> <item android:duration=" android:drawable= "@drawable/circle_10001"/> <item android:duration= "android:drawable=" @drawable/circle_10002 "/> <item android:duration=" 50 " android:drawable= "@drawable/circle_10003"/> <item android:duration= "android:drawable=" @drawable/circle_ 10004 "/> <item android:duration=" android:drawable= "@drawable/circle_10005"/> <item android:duration = "Android:drawable=" "@drawable/circle_10006"/> <item android:duration= "android:drawable=" @drawable circle_10007 "/> <item android:duration=" android:drawable= "@drawable/circle_10008"/> <item android:d uration= "android:drawable=" @drawable/circle_10009 "/> <item android:duration=" "android:drawable=" drawable/circle_10010 "/> <item android:duration=" 50 "android:drawable= "@drawable/circle_10011"/> <item android:duration= "android:drawable=" @drawable/circle_ 10012 "/> <item android:duration=" android:drawable= "@drawable/circle_10013"/> <item android:duration = "Android:drawable=" "@drawable/circle_10014"/> <item android:duration= "android:drawable=" @drawable
circle_10015 "/> </animation-list>
In the Main.xml file to do a simple layout, there is no need to annotate and explain, you read it, the contents are as follows:
[HTML] View plain copy on the code to see a piece of a snippet derived from my Code
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/" Apk/res/android "
android:orientation=" vertical "
android:layout_width=" fill_parent "
android:layout_" height= "Fill_parent"
android:background= "#ffffff" >
<imageview
android:id= "@+id/imageview"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_gravity = "Center"
android:src= "@drawable/circle"/>
<button
android:layout_width= "Wrap_content"
android:layout_height= "wrap_content"
android:onclick= "Start"
android:text= "rotate"/>
</ Linearlayout>
Code implemented in Mainactivity:
public class Mainactivity extends activity {
private imageview ImageView;
Private animationdrawable animationdrawable;
@Override public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
Initwedgits ();
}
/**
* Initialize components/
private void Initwedgits () {
try {
ImageView = (ImageView) Findviewbyid ( R.id.imageview);
Animationdrawable = (animationdrawable) imageview.getdrawable ();
} catch (Exception e) {
e.printstacktrace ();
}
}
public void Start (view view) {
try {
animationdrawable.start ();
} catch (Exception e) {
E.printstacktrace ();}}
After the code is written, run the program, in the absence of a click button imageview display a static diagram, when the button is clicked, you can achieve the rotation of the picture, the effect is as follows:
(forgot to do the animation effect chart, whining ...) To fill in later)
You can see that as long as we click on the button, the picture is left there spinning, isn't it amazing? Hurrying feet and ProgressBar the same effect, and do not have to define the style in the Style.xml file, immediately feel this method than the use of ProgressBar better (narcissism, (*^__^*) Hee ... Don't throw bricks, but careful boot will find a problem, why put the Animationdrawable.start () method in the Start () method? Why not just put it in the Initwedgits ()? Well, to reassure everyone, now I'm commenting out the code in the Start () method, putting Animationdrawable.start () directly in the Initwedgits () method, and modifying the code as follows:
public class Mainactivity extends activity {private ImageView ImageView;
Private Animationdrawable animationdrawable;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Initwedgits ();
/** * Initialize components/private void Initwedgits () {try {ImageView = (ImageView) Findviewbyid (R.id.imageview);
Animationdrawable = (animationdrawable) imageview.getdrawable ();
Animationdrawable.start ();
catch (Exception e) {e.printstacktrace ();
} public void Start (view view) {try {//Animationdrawable.start ();
catch (Exception e) {e.printstacktrace (); }
}
}
At this time, you will be sure to think, regardless of the click button after running (in fact, you put the button is rotten, the picture will not turn, because I note it off, (*^__^*) Hee Xi ...) The picture will rotate, well, we run the program, and you'll find that the picture doesn't rotate as we think. Hey? Odd strange, how does the picture not rotate? is not where to write wrong, so from beginning to end to check the code, when the code has been determined no problem, you will be depressed, it is estimated to think is not because there is no refresh reason AH (I just started to think, whining ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ So hurriedly use the view Refresh method, all have tried, the picture still can not rotate, you are more depressed, how can this be? Is it not possible to call Animationdrawable.start () in the OnCreate method? And then tried to put the Animationdrawable.start () method into the Onresume () method, but the result picture or not rotate, finally use the last trick and put the code into the Onwindowfocuschanged (Boolean Hasfocus) method, such a try, the picture finally rotated, oh, so happy, then you will be very happy, because later use the words directly in Onwindowfocuschanged (Boolean hasfocus) method to invoke the Animationdrawable start () method, you will no longer need to use other events to trigger the rotation of the picture, and then you'll be wondering why Animationdrawable's start () method has to be put here to execute it. Out of habit you should think of looking for the official documents to look, find AH find Ah, finally found, the official document of the description of the cut figure is as follows:
Oh, that's what it's all about. Do not call the Animationdrawable start () method in the OnCreate method because the animationdrawable at this time is not fully attached to the view window. If you want to play the animation immediately rather than using events to trigger (which is the start we used to call animationdrawable in the Start () method), you need to be in Onwindowfocuschanged (Boolean Hasfocus) method to invoke the Animationdrawable start () method, read the official document, you know how to use the later, Hee, happy again ...
old habits, paste code
public class Mainactivity extends activity {private ImageView ImageView;
Private Animationdrawable animationdrawable;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Initwedgits ();
/** * Initialize components/private void Initwedgits () {try {ImageView = (ImageView) Findviewbyid (R.id.imageview);
Animationdrawable = (animationdrawable) imageview.getdrawable ();
catch (Exception e) {e.printstacktrace (); } <p> @override public void onwindowfocuschanged (Boolean hasfocus) {super.onwindowfocuschanged (hasfocus);
lt;/p><p> if (hasfocus) Animationdrawable.start ();
}</p> public void Start (view view) {try {//animationdrawable.start ();
catch (Exception e) {e.printstacktrace (); }
}
}
In addition, I've tried several other ways to see it online, one of the simple implementations is to call the Animationdrawable start () method in the ImageView post () method without having to onwindowfocuschanged ( Boolean Hasfocus) method, the test is really effective, as for the other implementation of the way will no longer post code, if anyone interested in their own hands to ask degrees Niang, after all, their own hands, ample clothing, hehe
All right, that's it. Use ImageView and animationdrawable in a combination of ways to achieve ProgressBar effect explained, thank you for your reading.
SOURCE Download: Http://xiazai.jb51.net/201606/yuanma/Android-ImageView-ProgressBar (jb51.net). rar
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.