需求:
需求很簡單,就是中間顯示一個logo,外部是一個轉圈的動畫,這樣看着就是一個loading。
這裏通過dialog來實現,比較好集中代碼。
圖片:
全圖 loading1 loading2
代碼:
調用:
LoadingDialog loadingDialog = new LoadingDialog(activity);//new loading彈框
loadingDialog.show();//顯示彈框
loadingDialog.dismiss();//隱藏彈框
LoadingDialog.java彈框
package com.baofu.sdkwebpayment;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
/**
* Loading彈框
* Created by zst on 2018/5/8.
*/
public class LoadingDialog extends Dialog {
private ImageView iv_ing;
private AnimationSet animationSet;
public LoadingDialog(@NonNull Context context) {
super(context);
}
public LoadingDialog(@NonNull Context context, @StyleRes int themeResId) {
super(context, themeResId);
}
protected LoadingDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//背景透明處理
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getWindow().setDimAmount(0f);
this.setContentView(R.layout.dialog_loading);
//設置dialog屬性
setCancelable(true);
setCanceledOnTouchOutside(false);
iv_ing = findViewById(R.id.iv_ing);
//加載動畫
loadIng();
}
@Override
protected void onStart() {
super.onStart();
iv_ing.startAnimation(animationSet);//開始播放
}
@Override
protected void onStop() {
super.onStop();
}
//加載動畫
private void loadIng() {
animationSet = new AnimationSet(true);
RotateAnimation animation_rotate = new RotateAnimation(0, +359,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
//第一個參數fromDegrees爲動畫起始時的旋轉角度 //第二個參數toDegrees爲動畫旋轉到的角度
//第三個參數pivotXType爲動畫在X軸相對於物件位置類型 //第四個參數pivotXValue爲動畫相對於物件的X座標的開始位置
//第五個參數pivotXType爲動畫在Y軸相對於物件位置類型 //第六個參數pivotYValue爲動畫相對於物件的Y座標的開始位置
animation_rotate.setRepeatCount(-1);
animation_rotate.setStartOffset(0);
animation_rotate.setDuration(1000);
LinearInterpolator lir = new LinearInterpolator();
animationSet.setInterpolator(lir);
animationSet.addAnimation(animation_rotate);
}
}
dialog_loading.xml佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:orientation="horizontal"
android:padding="10dp">
<RelativeLayout
android:layout_width="80dp"
android:layout_height="80dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_loading_bac" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:src="@drawable/ic_loading_logo" />
<ImageView
android:id="@+id/iv_ing"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:src="@drawable/ic_loading_ing" />
</RelativeLayout>
</LinearLayout>
圖標最上面有的ic_loading_logo是logo,ic_loading_ing是loading1或2
結尾
因爲loading是比較常用的,並且跟隨dialog的生命週期,所以只用空時dialog就可以控制動畫了,不必擔心內存問題。