1. 程式人生 > >安卓-Loading載入中動畫

安卓-Loading載入中動畫

前段時間用到了loading載入動畫,這裡記錄兩種方法。一種是多個影象變化組成的動畫,一種圖片本身的動作,以轉圈為例下面是程式碼。

這是效果圖

原始碼下載地址
Loading載入中動畫專案原始碼下載
第一種多個圖片變化
在res的anim資料夾中新建一個loadingfish.xml檔案,anim資料夾需要自己建

<?xml version="1.0" encoding="UTF-8"?>
<animation-list android:oneshot="false"
    xmlns:android="http://schemas.android.com/apk/res/android"
>
<item android:duration="200" android:drawable="@drawable/img1" /> <item android:duration="200" android:drawable="@drawable/img2" /> <item android:duration="200" android:drawable="@drawable/img3" /> <item android:duration="200" android:drawable="@drawable/img4" /> <item
android:duration="200" android:drawable="@drawable/img5" />
<item android:duration="200" android:drawable="@drawable/img6" /> </animation-list>

然後定義loading的佈局檔案alertone.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="match_parent" android:background="@drawable/probg" android:gravity="center" >
<ProgressBar android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:indeterminateDrawable="@anim/loadingfish" /> </RelativeLayout>

第二種本身動畫,同樣在anim中定義loadingtwo.xml

<?xml version="1.0" encoding="utf-8"?>

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:drawable="@drawable/grey4"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fromDegrees="0"
        android:toDegrees="360" />

第二種loading佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@drawable/probg"
              android:gravity="center"
        >
    <ProgressBar android:id="@+id/loading_process_dialog_progressBar"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:indeterminate="false"
                 android:background="@drawable/img1"
                 android:indeterminateDrawable="@drawable/loadingtwo"/>
</LinearLayout>

第二種loading這裡我定義了一個繼承AlertDialog的自定義控制元件FishDialog.java,方便在通過建立物件的show()和dismiss()方法更方便的使用。

 */
public class FishDialog extends AlertDialog {

    Context context;

    public FishDialog(Context context){
        super(context);
        this.context=context;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.view_dialog);
        setCanceledOnTouchOutside(false);

        WindowManager.LayoutParams lp=getWindow().getAttributes();
        final float scale = context.getResources().getDisplayMetrics().density;
        lp.width=(int)(100*scale + 0.5f);
        lp.height=(int)(100*scale + 0.5f);
        getWindow().setAttributes(lp);
}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#fff"
        android:gravity="center">
    <Button android:id="@+id/alertone"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:text="loading one..."/>
    <Button android:id="@+id/alerttwo"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:text="loading two..."/>



</LinearLayout>

MyActivity裡統一測試

package com.example.TestDemo;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.*;
import android.widget.Button;

public class MyActivity extends Activity{

   Button btnblue,btngrey;
   private FishDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
    }
    private void init() {
        dialog=new FishDialog(this);
        btnblue= (Button) findViewById(R.id.alertone);
        btngrey= (Button) findViewById(R.id.alerttwo);
        btnblue.setOnClickListener(new    View.OnClickListener() {
            @Override
            public void onClick(View view) {
            alert();

            }

        });

        btngrey.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            dialog.show();
            }
        });
    }
    public void alert(){
        AlertDialog alert = new AlertDialog.Builder(this).create();
        alert.show();
        alert.getWindow().setLayout(150, 150);           alert.getWindow().setContentView(R.layout.alertone);
    }

}

第一次寫部落格,運用的還不熟練,也許表達的不是很清晰,以後自己慢慢琢磨吧,感覺再回味一遍也是一種樂趣。