自定义 Dialog方式的加载动画

一 自定义Dialog

/**
 * 自定义    加载动画Dialog
 * Created by Administrator on 2016/9/1.
 */
public class MyLoadingDialog extends AlertDialog {
    private int resBigLoading = R.mipmap.ic_loading;//要被做为  动画的图片
    private RotateAnimation mRotateAnimation;
    private ImageView ivBigLoading;
    private TextView title;


    public MyLoadingDialog(Context context) {
        super(context);
    }

    public MyLoadingDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view=LayoutInflater.from(getContext()).inflate(R.layout.loadingdialog, null, true);
        setContentView(view);
        init();
        initViews();
        setDialogSize(350,350);
    }

    /**
     * 初始化
     */
    private void initViews() {
        ivBigLoading = (ImageView) findViewById(R.id.loadingdialog_ivBigLoading);
        title=(TextView)findViewById(R.id.loadingdialog_tv);
        //设置图片
        ivBigLoading.setImageResource(resBigLoading);
        //将图片动画
        ivBigLoading.startAnimation(mRotateAnimation);
    }

    /**
     * 初始化动画方法
     */
    private void init() {
        mRotateAnimation = new RotateAnimation(0f, 359f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        mRotateAnimation.setDuration(1000L);
        mRotateAnimation.setInterpolator(new LinearInterpolator());
        mRotateAnimation.setRepeatCount(-1);
        mRotateAnimation.setRepeatMode(Animation.RESTART);
    }

    /**
     * 设置   dialog宽和高
     * @param width
     * @param height
     */
    private void setDialogSize(int width, int height) {
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = width;
        params.height = height;
        this.getWindow().setAttributes(params);
    }

    /**
     * 设置动画的标题
     * @param str
     */
    public void setTitle(String str){
        title.setText(str);
    }

}

二  dialog布局layount                loadingdialog.xml

<?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:padding="8dp"
    android:background="@drawable/loadingdialog_bg"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/loadingdialog_ivBigLoading"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="center_horizontal"
        android:src="@mipmap/ic_loading"
        android:padding="8dp" />
    <TextView
        android:id="@+id/loadingdialog_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="10sp"
        android:gravity="center"
        />
</LinearLayout>
三   loadingdialog_bg.xml文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/white"
        ></solid>
    <!--圆角白色背景-->
    <corners android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp">

    </corners>
</shape>

 
</pre>
四   自定义好之后的就可以在activity里面用了

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void jiazaizhong(View view){
        //加载dialog动画
        MyLoadingDialog myLoadingDialog=new MyLoadingDialog(this);
        myLoadingDialog.show();
        //设置标题内容
        myLoadingDialog.setTitle("加载中...");
    }


    public void shangchuanzhong(View view){
        MyLoadingDialog myLoadingDialog=new MyLoadingDialog(this);
        myLoadingDialog.show();
        myLoadingDialog.setTitle("上传中...");
    }
}

这样就简单实现了



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值