Android自定义实现淘宝下拉刷新效果
程序员文章站
2022-07-01 16:20:17
概述
目前下拉刷新的样式是多饰多样,今天我们一起来自定义淘宝下拉刷新,其实淘宝下拉刷新比较的简单就是一个圆环和一个小箭头的显示和隐藏,那么先看看我们的实现的效果。...
概述
目前下拉刷新的样式是多饰多样,今天我们一起来自定义淘宝下拉刷新,其实淘宝下拉刷新比较的简单就是一个圆环和一个小箭头的显示和隐藏,那么先看看我们的实现的效果。
是不是和淘宝有点像呢?那么现在我们来看看如何实现这个效果。我们这里为了省事,提供了2张照片 第一是“随时随地,想淘就淘”的照片,第二种就是小箭头照片,这里就自己画了,主要就是实现那个圆弧的绘制和旋转动画了。首先说这里的下拉刷新我用的是比较有名的 https://github.com/chrisbanes/android-pulltorefresh 这个大家肯定很熟悉了,这里我修改了这个库可以自定义头部和底部。
步骤:
1、自定义一个view。(包含圆弧的绘制和箭头的显示和隐藏)
2、自定义头部。
1)、创建attrs文件
<declare-styleable name="taobaoview"> <attr name="ringprogresscolor" format="color" /> <attr name="ringwidth" format="dimension" /> <attr name="ringimage" format="reference" /> <attr name="ringmax" format="integer" /> </declare-styleable>
2、创建一个类 taobaoview.class
public class taobaoview extends view{ //圆环进度颜色 private int ringprogresscolor; //圆环的宽度 private float ringwidth; //最大值 private int ringmax; //中间的icon private bitmap ringimage; //中间x坐标 private int centerx; //中间y坐标 private int centery; //画笔 private paint mpaint; //view宽度 private int width; //view高度 private int height; //进度 private int progress; //半径 private int radius; //是否显示图标 private boolean isshowicon=true; public taobaoview(context context) { this(context,null); } public taobaoview(context context, attributeset attrs) { this(context, attrs,0); } public taobaoview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); typedarray typedarray=context.obtainstyledattributes(attrs, r.styleable.taobaoview); ringprogresscolor=typedarray.getcolor(r.styleable.taobaoview_ringprogresscolor, color.gray); ringwidth=typedarray.getdimension(r.styleable.taobaoview_ringwidth, 5); ringmax=typedarray.getinteger(r.styleable.taobaoview_ringmax, 50); ringimage= bitmapfactory.decoderesource(getresources(), typedarray.getresourceid(r.styleable.taobaoview_ringimage,r.mipmap.jiantou)); init(); } private void init() { mpaint=new paint(); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { super.onmeasure(widthmeasurespec, heightmeasurespec); int widthmode=measurespec.getmode(widthmeasurespec); int widthsize=measurespec.getsize(widthmeasurespec); int heightmode=measurespec.getmode(heightmeasurespec); int heightsize=measurespec.getsize(heightmeasurespec); if(widthmode==measurespec.at_most)width=dp2px(30); else width=widthsize; if(heightmode==measurespec.at_most)height=dp2px(30); else height=heightsize; setmeasureddimension(width,height); } @override protected void onlayout(boolean changed, int left, int top, int right, int bottom) { super.onlayout(changed, left, top, right, bottom); //获取中心点的位置 centerx=getwidth()/2; centery=getheight()/2; radius=(int) (centerx - ringwidth / 2); } @override protected void onsizechanged(int w, int h, int oldw, int oldh) { super.onsizechanged(w, h, oldw, oldh); //确定view的宽高 width = w; height = h; } @override public void draw(canvas canvas) { super.draw(canvas); drawprogress(canvas); drawimage(canvas); } /** * 绘制图片 * @param canvas */ private void drawimage(canvas canvas) { if(isshowicon) canvas.drawbitmap(ringimage,centerx-ringimage.getwidth()/2,centery-ringimage.getheight()/2,mpaint); } /** * 绘制进度条 * @param canvas */ private void drawprogress(canvas canvas) { mpaint.setantialias(true); mpaint.setcolor(ringprogresscolor); mpaint.setstrokewidth(ringwidth); //设置画笔样式 mpaint.setstyle(paint.style.stroke); rectf rectf=new rectf(centerx-radius,centery-radius,centerx+radius,centery+radius); //绘制圆弧 canvas.drawarc(rectf,-110,-360*progress/ringmax,false,mpaint); } /** * dp转px * @param dp * @return */ public int dp2px(int dp){ float density = getcontext().getresources().getdisplaymetrics().density; return (int) (dp * density + 0.5f); } /** * 设置进度 * @param progress */ public synchronized void setprogress(int progress){ if(progress<0){ progress=0; } if(progress>=ringmax){ progress=ringmax; } this.progress=progress; postinvalidate(); } /** * 设置是否显示图标 * @param isshow */ public synchronized void setisshowicon(boolean isshow){ this.isshowicon=isshow; } }
3、创建一个headerlayout 下拉刷新头部
public class headerlayout extends loadinglayoutbase { private context mcontext; private rotateanimation refreshinganimation; private textview ring_refresh_status; private taobaoview mtaobaoview; private linearlayout header_base; private linearlayout header_layout; public headerlayout(context context) { this(context, pulltorefreshbase.mode.pull_from_start); } public headerlayout(context context, pulltorefreshbase.mode mode) { super(context); init(context,mode); } private void init(context mcontext,pulltorefreshbase.mode mode) { this.mcontext=mcontext; layoutinflater.from(mcontext).inflate(r.layout.taobao_view, this); header_base=(linearlayout)findviewbyid(r.id.header_base); header_layout=(linearlayout)findviewbyid(r.id.refresh_header_content); mtaobaoview=(taobaoview)findviewbyid(r.id.taobao_view); ring_refresh_status=(textview)findviewbyid(r.id.taobao_tv); refreshinganimation = (rotateanimation) animationutils.loadanimation(mcontext, r.anim.rotating); linearinterpolator lir = new linearinterpolator(); refreshinganimation.setinterpolator(lir); mtaobaoview.setprogress(90); layoutparams lp = (layoutparams) header_base.getlayoutparams(); lp.gravity = mode == pulltorefreshbase.mode.pull_from_end ? gravity.top : gravity.bottom; reset(); } @override public int getcontentsize() { return header_layout.getheight(); } /** * 下拉可以刷新 */ @override public void pulltorefresh() { ring_refresh_status.settext("下拉刷新"); mtaobaoview.setisshowicon(true); } /** * 松开后刷新 */ @override public void releasetorefresh() { ring_refresh_status.settext("松开刷新"); mtaobaoview.setisshowicon(false); } /** * 下拉中 * @param scaleoflayout scaleoflayout */ @override public void onpull(float scaleoflayout) { scaleoflayout = scaleoflayout > 1.0f ? 1.0f : scaleoflayout; int progress=(int) ((scaleoflayout)*100); mtaobaoview.setprogress(progress>90?90:progress); } /** * 正在刷新 */ @override public void refreshing() { mtaobaoview.setisshowicon(false); ring_refresh_status.settext("正在刷新"); mtaobaoview.startanimation(refreshinganimation); } @override public void reset() { mtaobaoview.clearanimation(); } @override public void setpulllabel(charsequence pulllabel) { } @override public void setrefreshinglabel(charsequence refreshinglabel) { } @override public void setreleaselabel(charsequence releaselabel) { } }
4、在mainactivity中:
mpulltorefreshlistview=(pulltorefreshlistview)findviewbyid(r.id.list); mpulltorefreshlistview.setheaderlayout(new headerlayout(this));
附上github:https://github.com/dalong982242260/taobaorefresh2
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: 酒鬼进到动物园 听到河马惨叫