|
在设计应用的时候,我们应该热爱极简主义,简单就是好的,对于很多用户来说,复杂的东西并不受欢迎。
我要实现的是根据不同的情况去显示不同的加载效果,随用随调,效果是借鉴于某一项目的效果,我认为有必要提取出来改善封装一下,供以后使用。情况大致分为:加载中、无网络、无数据、加载失败等,这些仅仅就需要一个View 就可以搞定啦!
预览下效果图:

我们怎么实现这种效果呢
view_loading.xml的布局如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/lin_loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
android:orientation="vertical">
<ImageView
android:id="@+id/img_loading"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/loading_animation" />
<TextView
android:id="@+id/tv_loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/lin_load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:visibility="visible"
>
<ImageView
android:id="@+id/iv_load"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_load"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:textSize="14sp" />
</LinearLayout>
</FrameLayout>
从布局来看,我分了两个部分,一个是加载中,另外一个是带有ImagView、文字和按钮的布局,有人看到这,就会说,哇靠,这不是很简单吗?根据不同的情况去设置Visibility的值就好了啊,没错,原理就是这样。
XHLoadingView.java的代码如下:
package com.woyou.loadingdemo.widget;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.woyou.loadingdemo.LoadingState;
import com.woyou.loadingdemo.R;
/**
* Created by Xiho on 11:21.
*/
public class XHLoadingView extends FrameLayout {
private Context mContext;
// 加载中的布局
private LinearLayout mLinearLoad;
//其他加载的布局
private LinearLayout mLinearLoading;
private TextView mTvLoading;
private TextView mTvLoad;
private ImageView mIvLoading;
private ImageView mIvLoad;
private Button mBtnLoad;
private LoadingState mState;
private AnimationDrawable animation;
public XHLoadingView(Context context) {
super(context);
mContext = context;
}
public XHLoadingView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
}
public XHLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
}
public XHLoadingView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
mContext = context;
}
public void build(){
LayoutInflater.from(mContext).inflate(R.layout.view_loading, this, true);
mLinearLoading = (LinearLayout) findViewById(R.id.lin_loading);
mLinearLoad = (LinearLayout) findViewById(R.id.lin_load);
mIvLoading = (ImageView) findViewById(R.id.img_loading);
mIvLoad = (ImageView) findViewById(R.id.iv_load);
mTvLoading = (TextView) findViewById(R.id.tv_loading);
mTvLoad = (TextView) findViewById(R.id.tv_load);
mBtnLoad = (Button) findViewById(R.id.btn_load);
mBtnLoad.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setState(LoadingState.STATE_LOADING);
mOnRetryListener.onRetry();
}
});
}
@Override
public void setVisibility(int visibility) {
super.setVisibility(visibility);
if(View.GONE==visibility && mState==LoadingState.STATE_LOADING && animation!=null&&animation.isRunning()){
animation.stop();
}
}
/+BXYX]J[HY[[]JH\X]JY[[]JN[Y]^[X]]W\^JNS[Y]H
[Y][Y]ОRY
Y[S[Y][\U^
i{i9eiy.g"HHK[\RX]K\[W]JK[\Q[XJ[CB\X]KX]\JK\^
L2
NILL
L
LNLLL$yknHK\^
!cHHHCB]^
/h9(y/iN
N
/h:-lK]X]KX]\JK]^
o!9ioy.aCB[]K[[[X][K[^
b:/oy.+KK]S\[\][Y]]S\[\
H\YCBXX]J
HX\][S[Y]m*9bb
NCBJKZ[
NCBBBBb:/oy.+CBS[Y]\[]JY]TJNS[Y]]J[]KUWSBl9kBS[Y]\[]JY]TJNS[Y]]J[]KUWTJCBdyBS[Y]\[]JY]TJNS[Y]]J[]KUWU
NBb:/oze&z+S[Y]\[]JY]TJNS[Y]]J[]KUWNBBBBCBBOB]B."gh$y+ley.#yd#9b9;`9.y.a*ahkfay.+y.#y+-#9b'yi%/#g*:g :)yg,9z, 9.+y#9l,y.#yay`f/&)d)Bk9m9. {HYHXK]^Z[[Y][H\H[[Y]B/g: !{Z[BT{L
L
NLB/k:/oz+#a!]LNMN
\XK]Z[LM
MLB."l,y+j:`yk{n#9&)i.h[:/k%"y`9n+bx |