Android 自定义带动画的dialog

1是通过直接加载gif实现,使用可以加载gif的ImageView 或者 用Glide 这种能够加载gif的框架
2用桢动画做
这里弄个桢动画的:
首先drawable 建立一个xml文件,名字是animation1


<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
                android:oneshot="false"
    >
    <item
        android:drawable="@mipmap/load1"
        android:duration="150">item>
    <item
        android:drawable="@mipmap/load2"
        android:duration="150">item>

    <item
        android:drawable="@mipmap/load3"
        android:duration="150">item>

    <item
        android:drawable="@mipmap/load4"
        android:duration="150">item>

    <item
        android:drawable="@mipmap/load5"
        android:duration="150">item>

    <item
        android:drawable="@mipmap/load6"
        android:duration="150">item>

    <item
        android:drawable="@mipmap/load7"
        android:duration="150">item>
    <item
        android:drawable="@mipmap/load8"
        android:duration="150">item>

    <item
        android:drawable="@mipmap/load9"
        android:duration="150">item>
    <item
        android:drawable="@mipmap/load10"
        android:duration="150">item>
    <item
        android:drawable="@mipmap/load11"
        android:duration="150">item>

    <item
        android:drawable="@mipmap/load12"
        android:duration="150">item>

    <item
        android:drawable="@mipmap/load13"
        android:duration="150">item>
    <item
        android:drawable="@mipmap/load14"
        android:duration="150">item>
    <item
        android:drawable="@mipmap/load15"
        android:duration="150">item>
    <item
        android:drawable="@mipmap/load16"
        android:duration="150">item>
    <item
        android:drawable="@mipmap/load17"
        android:duration="150">item>
    <item
        android:drawable="@mipmap/load18"
        android:duration="150">item>
    <item
        android:drawable="@mipmap/load19"
        android:duration="150">item>
    <item
        android:drawable="@mipmap/load20"
        android:duration="150">item>
    <item
        android:drawable="@mipmap/load21"
        android:duration="150">item>
    <item
        android:drawable="@mipmap/load22"
        android:duration="150">item>
    <item
        android:drawable="@mipmap/load23"
        android:duration="150">item>
    <item
        android:drawable="@mipmap/load24"
        android:duration="150">item>


animation-list>

然后自定义布局文件:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <RelativeLayout
        android:layout_width="230dp"
        android:layout_height="90dp"
        android:layout_gravity="center"
        android:background="@drawable/radio_layout"
        >

        <ImageView
            android:id="@+id/dialog_showview"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_centerVertical="true"
            android:layout_marginStart="10dp"

            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginStart="15dp"
            android:layout_toRightOf="@id/dialog_showview"
            android:text="正在加载"
            android:textColor="@color/gray3"
            android:textSize="14sp"
            />

    RelativeLayout>

LinearLayout>

然后自定义Dialog
在style中设置dialog显示的样式,然后从构造器里面设置这个样式,
然后可以通过window 对象设置宽高和显示的位置,包括底部,顶部,或者是中间

package com.test.posttime2.Activities;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.test.posttime2.R;


/**
 * Created by Administrator on 2017/11/2.
 */

public class MyDialog extends Dialog {

    Context context;
    private AnimationDrawable animationDrawable;

    public MyDialog(Context context) {
        super(context, R.style.MyDialog);
        this.context = context;
    }

    public MyDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected MyDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_item);
//        initView();//通过gif 实现
        initByanimation();
    }

    //通过真动画实现加载
    private void initByanimation() {
        ImageView image = (ImageView) findViewById(R.id.dialog_showview);
        //设置动画的显示
        image.setImageResource(R.drawable.animation1);
        animationDrawable = (AnimationDrawable) image.getDrawable();
        animationDrawable.start();
        windowDeploy();

    }


    private void initView() {
        ImageView image = (ImageView) findViewById(R.id.dialog_showview);
        Glide.with(context).load(R.mipmap.showwaiting).into(image);
        windowDeploy();
    }

    private Window window = null;

    //设置窗口显示
    public void windowDeploy() {
        window = getWindow();
 //           window.setGravity(Gravity.BOTTOM|Gravity.CENTER);
   //     window.setWindowAnimations(R.style.popup_window_anim);
     //   window.setBackgroundDrawableResource(R.drawable.suject_bg);
       // window.setGravity(Gravity.CENTER | Gravity.CENTER);
        window.setBackgroundDrawableResource(R.color.transparent1);
        WindowManager.LayoutParams wl = window.getAttributes();
        //根据x,y坐标设置窗口需要显示的位置
        window.setAttributes(wl);
    }
}

然后使用时就直接new 然后 show就行了
其他的如果要设置dialog的背景图片,需要在windowDeploy() 里面设置background ,然后就可以有各种背景自己调控

你可能感兴趣的:(Android)