Android Ui-pulltorrefresh Custom drop-down refresh animation
If you think this article is good, trouble cast a vote, 2014 blog star vote address: Http://vote.blog.csdn.net/blogstar2014/details?username=wwj_748#content
This blog post to share is how to use to modify the open source project Pulltorrefresh drop-down refresh animation to meet the specific needs of our development, our more common kind of drop-down refresh style may be the following:
It's the drop-down list. Two arrows flip up and down, change the date text and refresh state, this is the most common mode.
Another is the rotation of the animation, is the time to refresh a circle of rotation, similar to the Southern Weekend Reader (note: It is a product of the company, you have a lot of support O (∩_∩) o):
GitHub Address: Https://github.com/devilWwj/Android-PullToRefresh
After downloading, import into the workspace, imported into the target project, do not know how to import the related items of the children's shoes please self-Baidu.
One of the effects I want to implement here is to play a frame animation when the drop-down is refreshed
To add an animated list:
<?xml version= "1.0" encoding= "Utf-8"?><!--root tag is animation-list, where oneshot represents whether to show only once, set to false will not stop looping animation root tag under , each picture in the animation is declared by the item tag android:duration indicates the length of time that the picture was used--><animation-list xmlns:android= "/http Schemas.android.com/apk/res/android " android:oneshot=" false " > <item android:drawable=" @ Drawable/loading1 "android:duration=" ></item> <item android:drawable= "@drawable/loading2" android:duration= "></item> <item android:drawable=" @drawable/loading3 "android:duration=" 150 "></item> <item android:drawable=" @drawable/loading4 "android:duration=" ></item> </animation-list>
To modify the drop-down refresh layout:
/pulltorefresh/res/layout/pull_to_refresh_header_simple.xml
<?xml version= "1.0" encoding= "Utf-8"? ><merge xmlns:android= "Http://schemas.android.com/apk/res/android" > <framelayout android:id= "@+id/fl_inner" android:layout_width= "Fill_parent" Android:layout_ height= "Wrap_content" android:paddingbottom= "@dimen/header_footer_top_bottom_padding" android:paddingleft= "@ Dimen/header_footer_left_right_padding "android:paddingright=" @dimen/header_footer_left_right_padding "Andro id:paddingtop= "@dimen/header_footer_top_bottom_padding" > <framelayout android:layout_width= "wrap_ Content "android:layout_height=" wrap_content "android:layout_gravity=" Center_horizontal "> <imageview android:id= "@+id/pull_to_refresh_image" android:layout_width= "Wrap_conten T "android:layout_height=" wrap_content "android:layout_gravity=" center "Andro id:src= "@drawable/loading1" /> </FrameLayout> </FrameLayout></merge>
Add a custom load layout
/pulltorefresh/src/com/handmark/pulltorefresh/library/internal/tweenanimloadinglayout.java
Package Com.handmark.pulltorefresh.library.internal;import Com.handmark.pulltorefresh.library.r;import Com.handmark.pulltorefresh.library.pulltorefreshbase.mode;import Com.handmark.pulltorefresh.library.pulltorefreshbase.orientation;import Android.content.context;import Android.content.res.typedarray;import Android.graphics.drawable.animationdrawable;import Android.graphics.drawable.drawable;import android.view.view;/** * @date 2015/1/8 * @author Wuwenjie * @desc frame animation load Layout */pub Lic class Tweenanimloadinglayout extends Loadinglayout {private animationdrawable Animationdrawable;public Tweenanimloadinglayout (context context, mode mode,orientation scrolldirection, TypedArray attrs) {Super (context, mode, Scrolldirection, attrs);//Initialize Mheaderimage.setimageresource (r.drawable.refresh_anim); animationdrawable = ( animationdrawable) mheaderimage.getdrawable ();} Default picture @overrideprotected int Getdefaultdrawableresid () {return r.drawable.loading1;} @Overrideprotected void Onloadingdrawableset (drawable imagedrawable) {//no-op} @Overrideprotected void Onpullimpl (float scaleoflayout) {//no-op}//drop-down to refresh @overrideprotected void Pulltorefreshimpl () {///no-op}//is being refreshed when callback @overrideprotected void Refreshingimpl () {//Play frame animation Animationdrawable.start ();} Release to refresh @overrideprotected void Releasetorefreshimpl () {///no-op}//reset @overrideprotected void Resetimpl () { Mheaderimage.setvisibility (view.visible); Mheaderimage.clearanimation ();}}
We just need to modify the Lodinglayout code in the Open source project:
/pulltorefresh/src/com/handmark/pulltorefresh/library/internal/loadinglayout.java
/******************************************************************************* * Copyright, Chris Banes. * * Licensed under the Apache License, Version 2.0 (the "License"); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by applicable Law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warra Nties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. /package Com.handmark.pulltorefresh.library.internal;import Android.annotation.suppresslint;import Android.content.context;import Android.content.res.colorstatelist;import Android.content.res.typedarray;import Android.graphics.typeface;import android.graphics.drAwable. Animationdrawable;import Android.graphics.drawable.drawable;import Android.text.textutils;import Android.util.typedvalue;import Android.view.gravity;import Android.view.layoutinflater;import Android.view.View; Import Android.view.viewgroup;import Android.view.animation.interpolator;import Android.view.animation.linearinterpolator;import Android.widget.framelayout;import Android.widget.ImageView; Import Android.widget.linearlayout;import Android.widget.progressbar;import Android.widget.textview;import Com.handmark.pulltorefresh.library.iloadinglayout;import Com.handmark.pulltorefresh.library.pulltorefreshbase.mode;import Com.handmark.pulltorefresh.library.pulltorefreshbase.orientation;import com.handmark.pulltorefresh.library.r;@ Suppresslint ("Viewconstructor") public abstract class Loadinglayout extends Framelayout implements Iloadinglayout { Static final String Log_tag = "Pulltorefresh-loadinglayout"; static final interpolator animation_interpolator = new Linear Interpolator ();p rivate Framelayout minnerlayout;protected final ImageView mheaderimage;protected final ProgressBar mheaderprogress;private Boolean museintrinsicanimation;private final TextView mheadertext;private final TextView msubheadertext;protected Final Mode mmode;protected final Orientation mscrolldirection;private charsequence mpulllabel;private charsequence Mrefreshinglabel;private charsequence mreleaselabel;public loadinglayout (context context, final mode mode, final Orientation scrolldirection, TypedArray attrs) {super (context); Mmode = Mode;mscrolldirection = Scrolldirection;switch ( scrolldirection) {case HORIZONTAL:LayoutInflater.from (context). Inflate (R.layout.pull_to_refresh_header_ horizontal, this); Break;case vertical:default://layoutinflater.from (context). Inflate (R.layout.pull_to_refresh_ Header_vertical, this);//Modify Code Layoutinflater.from (context). Inflate (r.layout.pull_to_refresh_header_simple, this); break;} Minnerlayout = (framelayout) Findviewbyid (r.id.fl_inner); mheadertext = (TextView) minnerLayout.findviewbyid (r.id.pull_to_refresh_text); mheaderprogress = (ProgressBar) Minnerlayout.findviewbyid ( r.id.pull_to_refresh_progress); msubheadertext = (TextView) Minnerlayout.findviewbyid (r.id.pull_to_refresh_sub_ text); mheaderimage = (ImageView) Minnerlayout.findviewbyid (r.id.pull_to_refresh_image); Framelayout.layoutparams LP = (framelayout.layoutparams) minnerlayout.getlayoutparams (); switch (mode) {case PULL_FROM _end:lp.gravity = Scrolldirection = = orientation.vertical? gravity.top:gravity.left;//Load in Labelsmpulllabel = context.getstring (R.string.pull_to_refresh_from_bottom_pull_ label); Mrefreshinglabel = context.getstring (R.string.pull_to_refresh_from_bottom_refreshing_label); MReleaseLabel = Context.getstring (R.string.pull_to_refresh_from_bottom_release_label); Break;case PULL_FROM_START:default: lp.gravity = Scrolldirection = = orientation.vertical? gravity.bottom:gravity.right;//Load in Labelsmpulllabel = context.getstring (R.string.pull_to_refresh_pull_label); MrefreshinglaBel = context.getstring (R.string.pull_to_refresh_refreshing_label); Mreleaselabel = Context.getstring (R.string.pull _to_refresh_release_label); break;} if (Attrs.hasvalue (R.styleable.pulltorefresh_ptrheaderbackground)) {drawable background = attrs.getdrawable ( R.styleable.pulltorefresh_ptrheaderbackground); if (null! = background) {Viewcompat.setbackground (this, background);}} if (Attrs.hasvalue (r.styleable.pulltorefresh_ptrheadertextappearance)) {//typedvalue StyleID = new TypedValue ();// Attrs.getvalue (R.styleable.pulltorefresh_ptrheadertextappearance, StyleID);//settextappearance (styleID.data);// }//if (Attrs.hasvalue (r.styleable.pulltorefresh_ptrsubheadertextappearance)) {//typedvalue StyleID = new TypedValue ( );//attrs.getvalue (R.styleable.pulltorefresh_ptrsubheadertextappearance, StyleID);//setsubtextappearance ( Styleid.data)//}//////Text Color attrs need to BES set after Textappearance attrs//if (Attrs.hasvalue (R.styleable.pullto Refresh_ptrheadertextcolor)) {//colorstatelist colors = atTrs.getcolorstatelist (R.styleable.pulltorefresh_ptrheadertextcolor);//if (null! = colors) {//settextcolor (colors) ;//}//}//if (Attrs.hasvalue (R.styleable.pulltorefresh_ptrheadersubtextcolor)) {//colorstatelist colors = Attrs.getcolorstatelist (R.styleable.pulltorefresh_ptrheadersubtextcolor);//if (null! = colors) {//setsubtextcolor ( colors);//}//}//Try and get defined drawable from attrsdrawable imagedrawable = Null;if (Attrs.hasvalue (r.styleable.pull torefresh_ptrdrawable)) {imagedrawable = attrs.getdrawable (r.styleable.pulltorefresh_ptrdrawable);} Check specific drawable from Attrs, these overrite the generic//drawable attr aboveswitch (mode) {case Pull_from_start :d efault:if (Attrs.hasvalue (R.styleable.pulltorefresh_ptrdrawablestart)) {imagedrawable = Attrs.getDrawable ( R.styleable.pulltorefresh_ptrdrawablestart);} else if (Attrs.hasvalue (r.styleable.pulltorefresh_ptrdrawabletop)) {utils.warndeprecation ("PtrDrawableTop", " Ptrdrawablestart "); imagedrawable = Attrs.getdrawable (r.styleablE.pulltorefresh_ptrdrawabletop);} Break;case pull_from_end:if (Attrs.hasvalue (r.styleable.pulltorefresh_ptrdrawableend)) {imageDrawable = Attrs.getdrawable (r.styleable.pulltorefresh_ptrdrawableend);} else if (Attrs.hasvalue (R.styleable.pulltorefresh_ptrdrawablebottom)) {utils.warndeprecation ("PtrDrawableBottom", "Ptrdrawableend"); imagedrawable = Attrs.getdrawable (R.styleable.pulltorefresh_ptrdrawablebottom);} break;} If we don ' t have a user defined drawable, load the defaultif (null = = imagedrawable) {imagedrawable = Context.getresour CES (). getdrawable (Getdefaultdrawableresid ());} Set drawable, and save Width/heightsetloadingdrawable (imagedrawable); reset ();} Public final void setheight (int height) {viewgroup.layoutparams LP = Getlayoutparams (); lp.height = Height;requestlayout ( );} Public final void SetWidth (int width) {viewgroup.layoutparams LP = Getlayoutparams (); lp.width = Width;requestlayout ();} Public final int getcontentsize () {switch (mscrolldirection) {case Horizontal:return MinnerLayout.getwidth (); case VERTICAL:default:return minnerlayout.getheight ();}} Public final void Hideallviews () {//if (view.visible = = Mheadertext.getvisibility ()) {//mheadertext.setvisibility ( view.invisible)//}//if (view.visible = = Mheaderprogress.getvisibility ()) {//mheaderprogress.setvisibility ( view.invisible)//}//if (view.visible = = Mheaderimage.getvisibility ()) {//mheaderimage.setvisibility ( view.invisible)//}//if (view.visible = = Msubheadertext.getvisibility ()) {//msubheadertext.setvisibility ( view.invisible)//}}public final void Onpull (float scaleoflayout) {if (!museintrinsicanimation) {Onpullimpl ( scaleoflayout);}} Public final void Pulltorefresh () {//if (null! = Mheadertext) {//mheadertext.settext (Mpulllabel);//}//now call the Callba Ckpulltorefreshimpl ();} Public final void refreshing () {if (null! = Mheadertext) {mheadertext.settext (Mrefreshinglabel);} if (museintrinsicanimation) {((animationdrawable) mheaderimage.getdrawable ()). Start ();} else {//now call the Callbackrefreshingimpl();} if (null! = Msubheadertext) {//msubheadertext.setvisibility (view.gone);//}}public final void Releasetorefresh () {// if (null! = Mheadertext) {//mheadertext.settext (Mreleaselabel);//}//now call the Callbackreleasetorefreshimpl ();} Public final void Reset () {//if (null! = Mheadertext) {//mheadertext.settext (Mpulllabel);//}mheaderimage.setvisibility (view.visible), if (museintrinsicanimation) {((animationdrawable) mheaderimage.getdrawable ()). Stop ();} else {//Now Call the Callbackresetimpl ();} if (null! = Msubheadertext) {//if (Textutils.isempty (Msubheadertext.gettext ())) {//msubheadertext.setvisibility ( View.gone),//} else {//msubheadertext.setvisibility (view.visible);//}//}} @Overridepublic void Setlastupdatedlabel ( Charsequence label) {//setsubheadertext (label);} @Overridepublic final void setloadingdrawable (drawable imagedrawable) {//Set drawablemheaderimage.setimagedrawable ( imagedrawable) museintrinsicanimation = (imagedrawable instanceof animationdrawable);//Now call the CallbackoNloadingdrawableset (imagedrawable);} @Overridepublic void Setpulllabel (Charsequence pulllabel) {Mpulllabel = Pulllabel;} @Overridepublic void Setrefreshinglabel (Charsequence refreshinglabel) {Mrefreshinglabel = Refreshinglabel;} @Overridepublic void Setreleaselabel (Charsequence releaselabel) {Mreleaselabel = Releaselabel;} @Overridepublic void Settexttypeface (Typeface tf) {mheadertext.settypeface (TF);} Public final void Showinvisibleviews () {//if (view.invisible = = Mheadertext.getvisibility ()) {// Mheadertext.setvisibility (view.visible);//}//if (view.invisible = = Mheaderprogress.getvisibility ()) {// Mheaderprogress.setvisibility (view.visible);//}if (view.invisible = = Mheaderimage.getvisibility ()) { Mheaderimage.setvisibility (view.visible);} if (view.invisible = = Msubheadertext.getvisibility ()) {//msubheadertext.setvisibility (view.visible);//}}/** * Callbacks for derivative Layouts */protected abstract int getdefaultdrawableresid ();p rotected abstract void Onloadingdrawableset (drawable Imagedrawable);p rotected abstract void Onpullimpl (float scaleoflayout);p rotected abstract void Pulltorefreshimpl ();p rotected abstract void Refreshingimpl ();p rotected abstract void Releasetorefreshimpl ();p rotected abstract void Resetimpl (); private void Setsubheadertext (Charsequence label) {if (null! = Msubheadertext) {if (Textutils.isempty (label)) { Msubheadertext.setvisibility (View.gone);} else {msubheadertext.settext (label);//Only set it to Visible if we ' re GONE, otherwise Visible will//is set soonif (View. GONE = = Msubheadertext.getvisibility ()) {msubheadertext.setvisibility (view.visible);}}} private void setsubtextappearance (int value) {if (null! = Msubheadertext) {msubheadertext.settextappearance (getcontext (), value);}} private void Setsubtextcolor (colorstatelist color) {if (null! = Msubheadertext) {msubheadertext.settextcolor (color);}} private void settextappearance (int value) {if (null! = Mheadertext) {mheadertext.settextappearance (GetContext (), value) ;} if (null! = Msubheadertext) {MsubheaDertext.settextappearance (GetContext (), value);}} private void SetTextColor (colorstatelist color) {if (null! = Mheadertext) {mheadertext.settextcolor (color);} if (null! = Msubheadertext) {msubheadertext.settextcolor (color);}}}
Finally do not let me provide the source code, the author here is just to provide a thought, the refresh of the head layout changes to our custom layout on the line.
The final effect is that when the drop-down refreshes there will be a flashing light, there is no animated GIF, only one bar:
Android Ui-pulltorrefresh Custom drop-down refresh animation