安卓-Loading加载中动画

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Daxue_haha/article/details/48023411

前段时间用到了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);
    }

}

第一次写博客,运用的还不熟练,也许表达的不是很清晰,以后自己慢慢琢磨吧,感觉再回味一遍也是一种乐趣。

猜你喜欢

转载自blog.csdn.net/Daxue_haha/article/details/48023411
今日推荐