Ultra-Pull-To-Refresh超簡單實現自定義動畫(二)
阿新 • • 發佈:2019-01-25
前言
接上一篇部落格,Ultra-Pull-To-Refresh超簡單終極實現下拉重新整理、上拉載入 入門實現(一)
上一篇是實現了簡單的自帶的重新整理和載入功能。但是這樣簡單的動畫往往不能滿足我們的需求,所以這一片用一個京東重新整理的例子來實現自定義,就可以舉一反三地幹了,
製作gif太麻煩了,希望不影響各位老師的心情
準備工作
一、圖片素材
下載京東APP,解壓。找到此資料夾複製即可JDMALL-PC2/r/n
二、重新整理的頂部佈局
新建jd_refresh_header.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/refresh_people_0" />
<ImageView
android:id="@+id/iv_goods"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:src="@mipmap/refresh_goods_0"
/>
</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>
三、動畫xml書寫
drawable資料夾下建立running.xml檔案。就是人跑動的動畫
由於京東的圖片名字太長,我截取了最後幾個單詞。
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item
android:drawable="@mipmap/refresh_people_1"
android:duration="70" />
<item
android:drawable="@mipmap/refresh_people_2"
android:duration="70" />
<item
android:drawable="@mipmap/refresh_people_3"
android:duration="70" />
</animation-list>
四、最重要的一步
新建重新整理的檔案,用來控制動畫和文字。繼承FrameLayout,實現框架的PtrUIHandler介面
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 mAnimationDrawable;
public JdRefreshHeader(Context context) {
this(context, null);
}
public JdRefreshHeader(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
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, 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.running);
mAnimationDrawable = (AnimationDrawable) mIvMan.getBackground();
if (!mAnimationDrawable.isRunning()) {
mAnimationDrawable.start();
}
}
@Override
public void onUIRefreshComplete(PtrFrameLayout frame, boolean isHeader) {
mState = STATE_FINISH;
mIvGoods.setVisibility(View.VISIBLE);
//停止動畫
if (mAnimationDrawable.isRunning()) {
mAnimationDrawable.stop();
}
mIvMan.setBackgroundResource(R.mipmap.refresh_people_0);
}
@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());
LayoutParams params = (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());
params.setMargins(0, 0, marginRight, 0);
mIvMan.setLayoutParams(params);
}
if (ptrIndicator.getCurrentPercent() < 1.2) {
mTvRemind.setText("下拉重新整理...");
} else {
mTvRemind.setText("鬆開重新整理...");
}
break;
case STATE_BEGIN:
mTvRemind.setText("更新中...");
break;
case STATE_FINISH:
mTvRemind.setText("載入完成...");
break;
}
}
}
五、Activity引入
接上一篇,
JdRefreshHeader mJdRefreshHeader = new JdRefreshHeader(this);
ptrFrameLayout.setHeaderView(mJdRefreshHeader);
ptrFrameLayout.addPtrUIHandler(mJdRefreshHeader);
這三行程式碼就可以了。
至此,大功告成。程式碼複製可用,有什麼問題,歡迎指出,謝謝。