欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android仿网易一元夺宝客户端下拉加载动画效果(一)

程序员文章站 2024-03-06 08:57:13
上上周写的一个demo,仿照网易一元夺宝的下拉刷新效果。 原效果是(第一部分)一个小太阳拉下来,然后松开回弹上去, (第二部分)再掉下来一个硬币进行中轴旋转。 本文实...

上上周写的一个demo,仿照网易一元夺宝的下拉刷新效果。

原效果是(第一部分)一个小太阳拉下来,然后松开回弹上去,

(第二部分)再掉下来一个硬币进行中轴旋转。

本文实现的效果的是第一部分的,效果演示图如下:

Android仿网易一元夺宝客户端下拉加载动画效果(一)

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>

布局文件预览:

Android仿网易一元夺宝客户端下拉加载动画效果(一)

因为设置了透明,所以这里在没有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, 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, attributeset attrs) { 
super(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, 55 * scale, 5 * 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, 55 * scale, 
with / 2 - 50, 55 * scale + scrolly, 
paint 
); 
canvas.drawline(with / 2 + 40 * scale, 55 * scale, 
with / 2 + 50, 55 * scale + scrolly, 
paint 
); 
} else if (state == 3) { 
//1秒钟拉上去,2秒钟旋转 
//第一秒 
if (time < 30) { 
time += 1; 
//画两只球球手 
canvas.drawcircle(with / 2 - 40 * scale, 55 * scale, 5 * scale, paint); 
canvas.drawcircle(with / 2 + 40 * scale, 55 * scale, 5 * scale, paint); 
//画笑脸 
canvas.drawbitmap(sun0, 
null, 
new rectf(with / 2 - 50, 
55 * scale + 180 - (time / 30) * 230, 
with / 2 + 50, 
55 * scale + 280 - (time / 30) * 230), 
null 
); 
//画手臂 
paint.setstrokewidth(1); 
canvas.drawline(with / 2 - 40 * scale, 55 * scale, 
with / 2 - 50, 
55 * scale + 230 - (time / 30) * 230, 
paint 
); 
canvas.drawline(with / 2 + 40 * scale, 55 * scale, 
with / 2 + 50, 
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仿网易一元夺宝客户端下拉加载动画效果(一),希望对大家有所帮助