https://github.com/liaohuqiu/android-Ultra-Pull-To-Refreshandroid
你们感兴趣的能够去看一下这个下拉刷新框架,很是强大,可扩展性很是强,兼容各类view的下拉刷新事件。git
先放一张效果来看一下,京东的下拉刷新动画:github
通俗的来讲,就是一个快递小哥,跑过来,接住商品,而后刷新的时候,本身拿着商品跑起来。(解释有点牵强啊) 我们来分析一下整一个过程: 1.快递小哥从远处跑过去拿商品,能够拆成两个部分.bash
当拿到商品以后,就跑起来,在咱们程序里,就是动画:框架
(你去解压一下京东的apk,你也能拿到这些图片)ide
其实就是图片之间的切换,以上的准备工做都完成了,看看这个xml应该怎么写:布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/layout_tx">
<ImageView
android:id="@+id/iv_man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/a2a" />
<ImageView
android:id="@+id/iv_goods"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:src="@mipmap/a29" />
</FrameLayout>
<LinearLayout
android:id="@+id/layout_tx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="让购物更便捷"
android:textSize="14sp" />
<TextView
android:id="@+id/tv_remain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="松开刷新"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
复制代码
根据第一我的跟商品,我们先分开两个ImageView,使用FrameLayout布局,而后让这两个ImageView放进去,商品的这个ImageView设置layoutgravity=right.字体
我们再看看android-Ultra-Pull-To-Refresh这个框架给咱们带来什么。动画
public interface PtrUIHandler {
/**
* When the content view has reached top and refresh has been completed, view will be reset.
*
* @param frame
*/
public void onUIReset(PtrFrameLayout frame);
/**
* prepare for loading
*
* @param frame
*/
public void onUIRefreshPrepare(PtrFrameLayout frame);
/**
* perform refreshing UI
*/
public void onUIRefreshBegin(PtrFrameLayout frame);
/**
* perform UI after refresh
*/
public void onUIRefreshComplete(PtrFrameLayout frame);
public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator);
}
复制代码
这是一下下拉刷新事件的接口,只要实现以上接口就能获得相应的,获得以上接口ui
至于各个接口有什么,看接口名你们应该就知道了。 直接贴代码更直观:
/**
* 下拉刷新header
* Created by shenminjie on 2016/12/6.
*/
public class JdRefreshHeader extends FrameLayout implements PtrUIHandler {
/**
* 提醒文本
*/
private TextView mTvRemind;
/**
* 快递员logo
*/
private ImageView mIvMan;
/**
* 商品logo
*/
private ImageView mIvGoods;
/**
* 状态识别
*/
private int mState;
/**
* 重置
* 准备刷新
* 开始刷新
* 结束刷新
*/
public static final int STATE_RESET = -1;
public static final int STATE_PREPARE = 0;
public static final int STATE_BEGIN = 1;
public static final int STATE_FINISH = 2;
public static final int MARGIN_RIGHT = 100;
/**
* 动画
*/
private AnimationDrawable mAnimation;
public JdRefreshHeader(Context context) {
super(context);
initView();
}
public JdRefreshHeader(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public JdRefreshHeader(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
/**
* 初始化view
*/
private void initView() {
View view = LayoutInflater.from(getContext()).inflate(R.layout.jd_refresh_header_view, this, false);
mTvRemind = (TextView) view.findViewById(R.id.tv_remain);
mIvMan = (ImageView) view.findViewById(R.id.iv_man);
mIvGoods = (ImageView) view.findViewById(R.id.iv_goods);
addView(view);
}
@Override
public void onUIReset(PtrFrameLayout frame) {
mState = STATE_RESET;
}
@Override
public void onUIRefreshPrepare(PtrFrameLayout frame) {
mState = STATE_PREPARE;
}
@Override
public void onUIRefreshBegin(PtrFrameLayout frame) {
mState = STATE_BEGIN;
//隐藏商品logo,开启跑步动画
mIvGoods.setVisibility(View.GONE);
mIvMan.setBackgroundResource(R.drawable.runningman);
mAnimation = (AnimationDrawable) mIvMan.getBackground();
if (!mAnimation.isRunning()) {
mAnimation.start();
}
}
@Override
public void onUIRefreshComplete(PtrFrameLayout frame) {
mState = STATE_FINISH;
mIvGoods.setVisibility(View.VISIBLE);
//中止动画
if (mAnimation.isRunning()) {
mAnimation.stop();
}
mIvMan.setBackgroundResource(R.mipmap.a2a);
}
@Override
public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
//处理提醒字体
switch (mState) {
case STATE_PREPARE:
//logo设置
mIvMan.setAlpha(ptrIndicator.getCurrentPercent());
mIvGoods.setAlpha(ptrIndicator.getCurrentPercent());
FrameLayout.LayoutParams mIvManLayoutParams = (LayoutParams) mIvMan.getLayoutParams();
if (ptrIndicator.getCurrentPercent() <= 1) {
mIvMan.setScaleX(ptrIndicator.getCurrentPercent());
mIvMan.setScaleY(ptrIndicator.getCurrentPercent());
mIvGoods.setScaleX(ptrIndicator.getCurrentPercent());
mIvGoods.setScaleY(ptrIndicator.getCurrentPercent());
int marginRight = (int) (MARGIN_RIGHT - MARGIN_RIGHT * ptrIndicator.getCurrentPercent());
mIvManLayoutParams.setMargins(0, 0, marginRight, 0);
mIvMan.setLayoutParams(mIvManLayoutParams);
}
if (ptrIndicator.getCurrentPercent() < 1.2) {
mTvRemind.setText("下拉刷新...");
} else {
mTvRemind.setText("松开刷新...");
}
break;
case STATE_BEGIN:
mTvRemind.setText("更新中...");
break;
case STATE_FINISH:
mTvRemind.setText("加载完成...");
break;
}
}
}
复制代码
建立一个成员变量mState,用于保存下拉刷新的时候,每个状态,而后根据保存好的状态在UiPositionChange的接口中,对UI进行相应的修改,保存每一个状态文本的提示,在下拉的过程当中,经过UiPositionChange的回调,获取PtrIndicator中,能够获取下拉的百分比,根据这个百分比咱们能够作不少东西,例如京东的快递小哥从远处跑过来拿商品,以及快递小哥与商品之间的大小,均可以根据这个PtrIndicator百分比进行设置其大小的比例,跑过来这个过程我使用的方法是利用marginRight进行设置二者之间的距离,当达到下拉刷新的临界点时,快递小哥跟商品之间的margin为0,达到了快递小哥获取商品的效果,而后当刷新的时候,隐藏商品,使用以前所提供的三张图片进行效应的切换,也就是动画:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item
android:drawable="@mipmap/a2b"
android:duration="70" />
<item
android:drawable="@mipmap/a2c"
android:duration="70" />
<item
android:drawable="@mipmap/a2d"
android:duration="70" />
</animation-list>
复制代码
@Override
public void onUIRefreshBegin(PtrFrameLayout frame) {
mState = STATE_BEGIN;
//隐藏商品logo,开启跑步动画
mIvGoods.setVisibility(View.GONE);
mIvMan.setBackgroundResource(R.drawable.runningman);
mAnimation = (AnimationDrawable) mIvMan.getBackground();
if (!mAnimation.isRunning()) {
mAnimation.start();
}
}
复制代码
当刷新结束的时候,咱们把动画中止,并把以前的商品显示出来:
@Override
public void onUIRefreshComplete(PtrFrameLayout frame) {
mState = STATE_FINISH;
mIvGoods.setVisibility(View.VISIBLE);
//中止动画
if (mAnimation.isRunning()) {
mAnimation.stop();
}
mIvMan.setBackgroundResource(R.mipmap.a2a);
}
复制代码
很简单,基本上难点框架都已经帮咱们封装好了,咱们只要继承PtrFrameLayout,添加相应的Header以及实现PtrUIHandler就能够实现。
/**
*仿京东下拉刷新
* Created by shenminjie on 2016/12/6.
*/
public class JdRefreshLayout extends PtrFrameLayout {
/**
* headerView
*/
JdRefreshHeader mHeaderView;
public JdRefreshLayout(Context context) {
super(context);
initView();
}
public JdRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public JdRefreshLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView();
}
/**
* 初始化view
*/
private void initView() {
mHeaderView = new JdRefreshHeader(getContext());
setHeaderView(mHeaderView);
addPtrUIHandler(mHeaderView);
}
}
复制代码
天猫的更简单,毫无动画可言,说白了就是个GIF,你们能够去下载个apk,解压后能获得其gif。 原理跟以前的是同样,但这里我使用的是fresco进行加载gif,方法有不少,你们感兴趣的能够去试试。
/**
* 下拉刷新header
* Created by shenminjie on 2016/12/6.
*/
public class TmallRefreshHeader extends FrameLayout implements PtrUIHandler {
/**
* 提醒文本
*/
private TextView mTvRemind;
/**
* 状态识别
*/
private int mState;
/**
* 重置
* 准备刷新
* 开始刷新
* 结束刷新
*/
public static final int STATE_RESET = -1;
public static final int STATE_PREPARE = 0;
public static final int STATE_BEGIN = 1;
public static final int STATE_FINISH = 2;
public TmallRefreshHeader(Context context) {
super(context);
initView();
}
public TmallRefreshHeader(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public TmallRefreshHeader(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
/**
* 初始化view
*/
private void initView() {
View view = LayoutInflater.from(getContext()).inflate(R.layout.tmall_refresh_header_view, this, false);
mTvRemind = (TextView) view.findViewById(R.id.tv_remind);
SimpleDraweeView sdv = (SimpleDraweeView) view.findViewById(R.id.tm_logo);
DraweeController mDraweeController = Fresco.newDraweeControllerBuilder()
.setAutoPlayAnimations(true)
//设置uri,加载本地的gif资源
.setUri(Uri.parse("res://" + getContext().getPackageName() + "/" + R.drawable.tm_mui_bike))//设置uri
.build();
sdv.setController(mDraweeController);
addView(view);
}
@Override
public void onUIReset(PtrFrameLayout frame) {
mState = STATE_RESET;
}
@Override
public void onUIRefreshPrepare(PtrFrameLayout frame) {
mState = STATE_PREPARE;
}
@Override
public void onUIRefreshBegin(PtrFrameLayout frame) {
mState = STATE_BEGIN;
}
@Override
public void onUIRefreshComplete(PtrFrameLayout frame) {
mState = STATE_FINISH;
}
@Override
public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
//处理提醒字体
switch (mState) {
case STATE_PREPARE:
if (ptrIndicator.getCurrentPercent() < 1) {
mTvRemind.setText("下拉刷新");
} else {
mTvRemind.setText("松开当即刷新");
}
break;
case STATE_BEGIN:
mTvRemind.setText("正在刷新...");
break;
case STATE_FINISH:
mTvRemind.setText("加载完成...");
break;
}
}
}
/**
* 仿天猫下拉刷新view
* Created by shenminjie on 2016/12/6.
*/
public class TmallRefreshLayout extends PtrFrameLayout {
/**
* headerView
*/
TmallRefreshHeader mHeaderView;
public TmallRefreshLayout(Context context) {
super(context);
initView();
}
public TmallRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public TmallRefreshLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView();
}
/**
* 初始化view
*/
private void initView() {
mHeaderView = new TmallRefreshHeader(getContext());
setHeaderView(mHeaderView);
addPtrUIHandler(mHeaderView);
}
}
复制代码