1. 程式人生 > >Android載入動畫 旋轉動畫 、幀動畫

Android載入動畫 旋轉動畫 、幀動畫

android中的載入動畫   上程式碼:

這是使用方式:

public class LoadingActivity extends AppCompatActivity {
    //載入動畫activity
private Dialog loading;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading);

    }

   //顯示菊花轉
public void showFlower(View view) { loading = MyProgress.createLoadingDialog(this, R.mipmap.loading_flower); loading.show(); } // 顯示美團進度對話方塊 public void showMeiTuan(View v) { loading = MyProgress.createProgressLoading(this, R.drawable.anim_loading); loading.show(); } }
這是載入動畫那個公共類:
public class MyProgress {
//自定義載入動畫
//旋轉
public static Dialog createLoadingDialog(Context context,int resId) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.loading_layout, null);// 得到載入view
RelativeLayout layout = (RelativeLayout) v.findViewById(R.id.dialog_view);// 載入佈局
//xml中的ImageView ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img); spaceshipImage.setBackgroundResource(resId); // 載入動畫 Animation loadAnimation = AnimationUtils.loadAnimation(context, R.anim.my_progress); // 使用ImageView顯示動畫 spaceshipImage.startAnimation(loadAnimation); Dialog loadingDialog = new Dialog(context, R.style.nocolor_progress);// 建立自定義樣式dialog loadingDialog.setCanceledOnTouchOutside(true); loadingDialog.setCancelable(true);// 可以用“返回鍵”取消 loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));// 設定佈局 return loadingDialog; } //幀動畫 public static Dialog createProgressLoading(Context context,int resId) { LayoutInflater inflater = LayoutInflater.from(context); View v = inflater.inflate(R.layout.loading_layout, null);// 得到載入view RelativeLayout layout = (RelativeLayout) v.findViewById(R.id.dialog_view);// 載入佈局 //xml中的ImageView ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img); spaceshipImage.setBackgroundResource(resId); // 載入動畫 // 通過ImageView物件拿到背景顯示的AnimationDrawable final AnimationDrawable mAnimation = (AnimationDrawable) spaceshipImage.getBackground(); // 為了防止在onCreate方法中只顯示第一幀的解決方案之一 spaceshipImage.post(new Runnable() { @Override public void run() { mAnimation.start(); } }); Dialog loadingDialog = new Dialog(context, R.style.nocolor_progress);// 建立自定義樣式dialog loadingDialog.setCanceledOnTouchOutside(true); loadingDialog.setCancelable(true);// 可以用“返回鍵”取消 loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));// 設定佈局 return loadingDialog; } }

這是loading_layout那個頁面佈局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_view"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/no">
    <ImageView
android:layout_centerInParent="true"
android:id="@+id/img"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@mipmap/ic_progress_normal"
/>

    <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在載入... "
android:id="@+id/textView"
android:layout_below="@+id/img"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp" />
</RelativeLayout>
這是my_progress那個anim:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
    <rotate
android:duration="1000"
android:fromDegrees="0"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="-1"
android:repeatMode="restart"
android:startOffset="-1"
android:toDegrees="+360" />
</set>
這是dialog的樣式:
<!--菊花轉樣式-->
<style name="nocolor_progress" parent="android:style/Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowNoTitle">true</item>
<!--@color/no   #00000000-->
<item name="android:windowBackground">@color/no</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item></style>

這是anim_loading:

<?xml version="1.0" encoding="utf-8"?>
<animation-list
android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android"
>
    <item android:drawable="@mipmap/progress_loading_image_01" android:duration="150"/>
    <item android:drawable="@mipmap/progress_loading_image_02" android:duration="150"/>
</animation-list>