上上周写的一个demo,仿照网易一元夺宝的下拉刷新效果。
原效果是(第一部分)一个小太阳拉下来,然后松开回d上去,
(第二部分)再掉下来一个硬币进行中轴旋转。
本文实现的效果的是第一部分的,效果演示图如下:
gif图看起来比较卡顿。。。其实真机演示效果还是很流畅的。
下面分析实现过程:
当时因为时间有限没有写在下拉刷新的组件中,也没有封装成一个单独的组件,只是在主布局后面写了一个VIEw然后实现相应的 *** 作,进行封装并不难,这里就不花时间BB了,下面是布局文件:
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context=".aty.MainActivity"> <location.haIDian.com.wypulltoreflush.vIEw.NGimgVIEw androID:ID="@+ID/ngimg_main" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"/> <ImageVIEw androID:ID="@+ID/img_main_sun" androID:layout_wIDth="50dp" androID:layout_height="50dp" androID:layout_centerHorizontal="true" androID:layout_margintop="5dp" androID:src="@drawable/ic_sun1"/> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="#00123456" androID:orIEntation="vertical"> <relativeLayout androID:layout_wIDth="match_parent" androID:layout_height="55dp" androID:background="#FF4081"> </relativeLayout> <VIEw androID:layout_wIDth="match_parent" androID:layout_height="0.5dp" androID:background="@androID:color/darker_gray"></VIEw> <location.haIDian.com.wypulltoreflush.vIEw.NGReFlashListVIEw androID:ID="@+ID/lv_main" androID:layout_wIDth="fill_parent" androID:layout_height="0dp" androID:layout_weight="1" androID:cachecolorHint="#00000000" /> </linearLayout> </relativeLayout>
布局文件预览:
因为设置了透明,所以这里在没有item的情况下是能看到后面的布局的。
代码实现:
通过下拉刷新类NGReFlashListVIEw进行事件判断,然后通过回调把相应的事件传递给NGImageVIEw中改变视图显示。
下面是两个类的源代码:
NGReFlashListVIEw类:
package location.haIDian.com.wypulltoreflush.vIEw; import androID.content.Context; import androID.util.AttributeSet; import androID.vIEw.LayoutInflater; import androID.vIEw.MotionEvent; import androID.vIEw.VIEw; import androID.vIEw.VIEwGroup; import androID.Widget.AbsListVIEw; import androID.Widget.AbsListVIEw.OnScrollListener; import androID.Widget.ListVIEw; import location.haIDian.com.wypulltoreflush.R; public class NGReFlashListVIEw extends ListVIEw implements OnScrollListener { private VIEw header; private int headerHeight; private int firstVisibleItem; private int scrollState; private final int NONE = 0; private final int PulL = 1; private final int RELESE = 2; private final int REFLASHING = 3; public NGReFlashListVIEw(Context context) { super(context); initVIEw(context); } public NGReFlashListVIEw(Context context,AttributeSet attrs) { super(context,attrs); initVIEw(context); } public NGReFlashListVIEw(Context context,AttributeSet attrs,int defStyle) { super(context,attrs,defStyle); initVIEw(context); } /** * @param context */ private voID initVIEw(Context context) { LayoutInflater inflater = LayoutInflater.from(context); header = inflater.inflate(R.layout.layout_heard,null); measureVIEw(header); headerHeight = header.getMeasuredHeight(); toppadding(-headerHeight); this.addheaderVIEw(header); this.setonScrollListener(this); } /** * @param vIEw */ private voID measureVIEw(VIEw vIEw) { VIEwGroup.LayoutParams p = vIEw.getLayoutParams(); if (p == null) { p = new VIEwGroup.LayoutParams(VIEwGroup.LayoutParams.MATCH_PARENT,VIEwGroup.LayoutParams.WRAP_CONTENT); } int wIDth = VIEwGroup.getChildMeasureSpec(0,p.wIDth); int height; int tempHeight = p.height; if (tempHeight > 0) { height = MeasureSpec.makeMeasureSpec(tempHeight,MeasureSpec.EXACTLY); } else { height = MeasureSpec.makeMeasureSpec(0,MeasureSpec.UnspecIFIED); } vIEw.measure(wIDth,height); } /** * @param toppadding */ private voID toppadding(int toppadding) { header.setpadding(header.getpaddingleft(),toppadding,header.getpaddingRight(),header.getpaddingBottom()); header.invalIDate(); } @OverrIDe public voID onScroll(AbsListVIEw vIEw,int firstVisibleItem,int visibleItemCount,int totalitemCount) { this.firstVisibleItem = firstVisibleItem; } @OverrIDe public voID onScrollStateChanged(AbsListVIEw vIEw,int scrollState) { this.scrollState = scrollState; } boolean isRemark;//初始化标识 int startY; int state; @OverrIDe public boolean ontouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: if (firstVisibleItem == 0) { onRlvScrollListener.onScrollYChangged(0,0); isRemark = true; startY = (int) ev.getY(); } break; case MotionEvent.ACTION_MOVE: onMove(ev); break; case MotionEvent.ACTION_UP: if (state == RELESE) { onRlvScrollListener.onScrollYChangged(0,3); state = REFLASHING; reflashVIEwByState(); //iReflashListener.onReflash(); } else if (state == PulL) { state = NONE; onRlvScrollListener.onScrollYChangged(0,0); isRemark = false; reflashVIEwByState(); } break; } return super.ontouchEvent(ev); } int space; //间距 int toppadding; //headvIEw距离顶端的距离,初始-200 /** * @param ev */ private voID onMove(MotionEvent ev) { if (!isRemark) { return; } int tempY = (int) ev.getY(); space = tempY - startY; if (space >= 230) { space = 230; } toppadding = space - headerHeight; onRlvScrollListener.onScrollYChangged(space,state); switch (state) { case NONE: if (space > 0) { state = PulL; reflashVIEwByState(); } break; case PulL: toppadding(toppadding); if (space == headerHeight + 30 && scrollState == SCRolL_STATE_touch_SCRolL) { state = RELESE; reflashVIEwByState(); } break; case RELESE: if (space < headerHeight + 30) { state = PulL; reflashVIEwByState(); } else if (space <= 0) { state = NONE; isRemark = false; reflashVIEwByState(); } break; } } /** * 控制下拉刷新的图像文字显示 */ private voID reflashVIEwByState() { switch (state) { case NONE: //缓慢滑上去 toppadding(-headerHeight); break; case PulL: break; case RELESE: break; case REFLASHING: break; } } /** * 刷新完成 */ public voID reflashComplete() { state = NONE; onRlvScrollListener.onScrollYChangged(0,state); isRemark = false; reflashVIEwByState(); } //绘制背景的接口 public interface OnRlvScrollListener { voID onScrollYChangged(int Y,int state); } private OnRlvScrollListener onRlvScrollListener; public voID setonRlvScrollListener(OnRlvScrollListener onRlvScrollListener) { this.onRlvScrollListener = onRlvScrollListener; } }
NGimgVIEw.java
实现比较简单,根据传来的回调状态改变进行小太阳和两只球球手以及手臂的绘制就可以了。
package location.haIDian.com.wypulltoreflush.vIEw; import androID.content.Context; import androID.graphics.Bitmap; import androID.graphics.Canvas; import androID.graphics.color; import androID.graphics.Paint; import androID.graphics.RectF; import androID.graphics.drawable.BitmapDrawable; import androID.util.AttributeSet; import androID.util.Log; import androID.Widget.ImageVIEw; import location.haIDian.com.wypulltoreflush.R; /** * Created by nangua on 2016/8/28. */ public class NGimgVIEw extends ImageVIEw implements NGReFlashListVIEw.OnRlvScrollListener { private float scale; private final int NONE = 0; private final int PulL = 1; private final int RELESE = 2; private final int REFLASHING = 3; public int state = 0; private float WITH; //屏幕总宽 private float scrollY; private Bitmap sun0; public float time = 0; public NGimgVIEw(Context context,attrs); initVIEw(); } private voID initVIEw() { sun0 = ((BitmapDrawable) getResources().getDrawable(R.drawable.ic_sun0)).getBitmap(); scale = this.getResources().getdisplayMetrics().density; } @OverrIDe protected voID onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setAntiAlias(true);//抗锯齿 paint.setcolor(color.parsecolor("#C4885A")); Log.d("xiaojingyu","State:" + state); if (state!=3) { time = 0; //重置时间 //画两只球球手 canvas.drawCircle(WITH / 2 - 40 * scale,55 * scale,5 * scale,paint); canvas.drawCircle(WITH / 2 + 40 * scale,paint); //画笑脸 canvas.drawBitmap(sun0,null,new RectF(WITH / 2 - 50,55 * scale + scrollY - (scrollY / 230) * 50,WITH / 2 + 50,55 * scale + scrollY - (scrollY / 230) * 50 + 100),null ); //画手臂 paint.setstrokeWIDth(1); canvas.drawline(WITH / 2 - 40 * scale,WITH / 2 - 50,55 * scale + scrollY,paint ); canvas.drawline(WITH / 2 + 40 * scale,paint ); } else if (state == 3) { //1秒钟拉上去,2秒钟旋转 //第一秒 if (time < 30) { time += 1; //画两只球球手 canvas.drawCircle(WITH / 2 - 40 * scale,55 * scale + 180 - (time / 30) * 230,55 * scale + 280 - (time / 30) * 230),55 * scale + 230 - (time / 30) * 230,paint ); postInvalIDateDelayed(1); } else { if (!isBeginMainAnimation) { isBeginMainAnimation = true; iReflashListener.onReflash(); } } } super.onDraw(canvas); } public static boolean isBeginMainAnimation = false; @OverrIDe protected voID onLayout(boolean changed,int left,int top,int right,int bottom) { super.onLayout(changed,left,top,right,bottom); WITH = this.getWIDth(); } @OverrIDe public voID onScrollYChangged(int Y,int state) { this.state = state; switch (state) { case NONE: break; case PulL: //下拉 scrollY = Y; break; case RELESE: break; case REFLASHING: break; } } private IReflashListener iReflashListener; //回调接口 public voID setInterface(IReflashListener iReflashListener) { this.iReflashListener = iReflashListener; } /** * @author administrator */ public interface IReflashListener { voID onReflash(); } }
以上所述是小编给大家介绍的AndroID仿网易一元夺宝客户端下拉加载动画效果(一),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android仿网易一元夺宝客户端下拉加载动画效果(一)全部内容,希望文章能够帮你解决Android仿网易一元夺宝客户端下拉加载动画效果(一)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)