Android Custom Control faux Autohome drop-down refresh

Source: Internet
Author: User


About the implementation of the drop-down refresh I have explained in detail in the previous article The imitation of the Android custom control drop-down refresh, this article mainly introduces the principle of the animation of the dial

The Autohome drop-down refresh is divided into three states:
The first state is the drop-down refresh state (pull to refresh), in which case the dial dynamically changes the angle of the pointer as the drop-down distance
The second state is the release refresh state (release to refresh), which is an animation of the pointer angle change in this state

Implementation of the first State:


This effect is implemented using a custom view, and we get the two images from the Autohome apk for the drop-down refresh:


We draw the first picture on the canvas as the background, then we rotate the canvas dynamically based on the current progress value, and then we draw the second picture on the canvas, and we see that the rotation of the needle is actually the canvas.

  @Override  protected  void  onDraw             (canvas canvas) {super . OnDraw (canvas); //now draws the first picture on the canvas  canvas.drawbitmap (finalbackgroundbitmap,0 , 0 , null ); //rotate canvas  canvas.rotate (mcurrentprogress*2.7  f,x            /2 , Y/2 ); //draw the second picture on the rotated canvas  canvas.drawbitmap (finalpointerbitmap, 0 , 0 , null ); }

The complete code for customizing the view is as follows:

/** * Created by Zhangqi on 15/10/17. * * Public  class autohome extends View{    PrivateBitmap Backgroundbitmap; PublicBitmap Pointerbitmap;Private intXPrivate intYPrivateBitmap Finalbackgroundbitmap;PrivateBitmap Finalpointerbitmap;Private floatmcurrentprogress; Public Autohome(Context context) {Super(context);    Init (context); } Public Autohome(context context, AttributeSet attrs) {Super(context, attrs);    Init (context); } Public Autohome(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr);    Init (context); }Private void Init(Context context) {Backgroundbitmap = Bitmap.createbitmap (Bitmapfactory.decoderesource (Context.getresources (), R.drawable.load_icon        _DIAL2X)); Pointerbitmap = Bitmap.createbitmap (Bitmapfactory.decoderesource (Context.getresources (), R.drawable.load_icon_    POINTER2X)); }@Override    protected void onmeasure(intWidthmeasurespec,intHeightmeasurespec) {setmeasureddimension (Measurewidth (Widthmeasurespec), Measurewidth (HeightMeasureSpec));        x = Getmeasuredwidth ();        y = Getmeasuredheight (); Finalbackgroundbitmap = Bitmap.createscaledbitmap (Backgroundbitmap, x, Y,false); Finalpointerbitmap = Bitmap.createscaledbitmap (Pointerbitmap, x, Y,false); }Private int Measurewidth(intWIDMEASURESPEC) {intresult =0;intSize = Measurespec.getsize (WIDMEASURESPEC);intmode = Measurespec.getmode (WIDMEASURESPEC);if(mode = = measurespec.exactly)        {result = size; }Else{result = Backgroundbitmap.getwidth ();if(mode = = Measurespec.at_most)            {result = Math.min (result,size); }        }returnResult }@Override    protected void OnDraw(Canvas canvas) {Super. OnDraw (canvas); Canvas.drawbitmap (Finalbackgroundbitmap,0,0,NULL); Canvas.rotate (mcurrentprogress*2.7f,x/2, y/2); Canvas.drawbitmap (Finalpointerbitmap,0,0,NULL); } Public void setcurrentprogress(floatProgress) {mcurrentprogress = progress* -; }}

We then use the Seekbar to simulate a progress value in the activity and pass it on to our custom view

 Public  class mainactivity extends appcompatactivity {    PrivateSeekBar Mseekbar;PrivateAutohome Mautohome;Private floatmcurrentprogress;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        Mseekbar = (SeekBar) Findviewbyid (R.id.seekbar);        Mautohome = (Autohome) Findviewbyid (r.id.autohome); Mseekbar.setonseekbarchangelistener (NewSeekbar.onseekbarchangelistener () {@Override             Public void onprogresschanged(SeekBar SeekBar,intIBooleanb) {mcurrentprogress = (float) seekbar.getprogress ()/(float) Seekbar.getmax ();                Mautohome.setcurrentprogress (mcurrentprogress);            Mautohome.invalidate (); }@Override             Public void Onstarttrackingtouch(SeekBar SeekBar) {            }@Override             Public void Onstoptrackingtouch(SeekBar SeekBar)    {            }        }); }
Implementation of a second state:

The second state is that the needle performs a rotating animation, and we can write the needle to a custom view, then the dial as a background image, then the needle view to perform the rotate animation.

/** * Created by Zhangqi on 15/10/27. * * Public  class pointerview extends View {    Private intXPrivate intYPrivateBitmap Finalpointerbitmap;PrivateBitmap Pointerbitmap; Public Pointerview(Context context) {Super(context);    Init (); } Public Pointerview(context context, AttributeSet attrs) {Super(context, attrs);    Init (); } Public Pointerview(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr);    Init (); }Private void Init() {Pointerbitmap = Bitmap.createbitmap (Bitmapfactory.decoderesource (Getresources (), R.drawable.load_icon_pointer2    x)); }@Override    protected void onmeasure(intWidthmeasurespec,intHeightmeasurespec) {setmeasureddimension (Measurewidth (Widthmeasurespec), Measurewidth (HeightMeasureSpec));        x = Getmeasuredwidth ();        y = Getmeasuredheight (); Finalpointerbitmap = Bitmap.createscaledbitmap (Pointerbitmap, x, Y,false); }Private int Measurewidth(intWIDMEASURESPEC) {intresult =0;intSize = Measurespec.getsize (WIDMEASURESPEC);intmode = Measurespec.getmode (WIDMEASURESPEC);if(mode = = measurespec.exactly)        {result = size; }Else{result = Pointerbitmap.getwidth ();if(mode = = Measurespec.at_most)            {result = Math.min (result,size); }        }returnResult }@Override    protected void OnDraw(Canvas canvas) {Super. OnDraw (canvas);//The aim is to have the hands of the hands 270 degrees in the initial position! Canvas.rotate ( the, x/2, y/2); Canvas.drawbitmap (Finalpointerbitmap,0,0,NULL); }}

Then we write this in the XML file:

 <framelayout  android:id  = "@+id/anim_container"  android:layout_width  = "45DP"  android:layout_height  = "45DP"  android:layout_margin  =     "15DP"  android:visibility  = "Gone" ;     <com.zhangqi.autohomerefreshlistview.pointerview  Span class= "Hljs-attribute" >android:id  = "@+id/anim_pointer"  android:layout_width  =" wrap_content " android:layout_height  =" wrap_content " android:layout_gravity  =" center "/>         <ImageViewandroid:layout_width="Wrap_content"android:layout_height= "Wrap_content" android:src="@drawable/load_icon_dial2x" />                                        </framelayout>

This gives the dial a background, and we can manipulate the hands to perform rotate animations.

<?xml version= "1.0" encoding= "Utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android"Android : fromdegrees="0"android:todegrees= "-150"android:pivoty="50%" android:pivotx="50%"android:duration="Android": RepeatCount="Infinite"android:repeatmode="reverse" >                            </rotate>
mAutoHomeAnim = (PointerView) headerView.findViewById(R.id.anim_pointer);animation = AnimationUtils.loadAnimation(context, R.anim.pointer_rotate);//执行动画mAutoHomeAnim.startAnimation(animation);
In the ListView

Because the drop-down refreshes the core code and the American group drop-down refresh is the same, here I only intercept the different parts

Private void changeheaderbystate(intState) {Switch(state) { CaseDONE:headerView.setPadding (0,-headerviewheight,0,0);//The view of the first state is displayedMautohome.setvisibility (view.visible);//Stop the animation of the second stage view firstMautohomeanim.clearanimation ();//Hide the second stage viewManimcontainer.setvisibility (View.gone); Break; CaseRELEASE_TO_REFRESH:tv_pull_to_refresh.setText ("Release Refresh"); Break; CasePULL_TO_REFRESH:tv_pull_to_refresh.setText ("Drop-down refresh");//First State view displayMautohome.setvisibility (view.visible);//Stop second stage animationMautohomeanim.clearanimation ();//To hide the second stage viewManimcontainer.setvisibility (View.gone); Break; CaseREFRESHING:tv_pull_to_refresh.setText ("Refreshing");//To hide the first stage viewMautohome.setvisibility (View.gone);//Show the second stage viewManimcontainer.setvisibility (view.visible);//Stop the second stage animation firstMautohomeanim.clearanimation ();//Start the second stage animationMautohomeanim.startanimation (animation); Break;default: Break; }    }
Full code:

Everyone can download it on my github.

Copyright NOTICE: Welcome reprint, Reprint please indicate the source http://blog.csdn.net/nugongahou110

Android Custom Control faux Autohome drop-down refresh

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.