学习自定义加载框

253 阅读1分钟

先自定义布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/load_llt"
    android:background="#000000"
    android:padding="15dp"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/load_img"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:adjustViewBounds="true"
        android:src="@drawable/load_dialog" />

    <TextView
        android:id="@+id/load_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="正在加载中......"
        android:layout_marginTop="15dp"
        android:gravity="center_horizontal"
        android:textColor="#FFFFFF"
        android:textSize="20sp" />

</LinearLayout>

加载的图片是网上随便找的:
图片加载

设置图片的旋转动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

    <rotate
        android:duration="2000"
        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" >
    </rotate>

</set>

使用

        LayoutInflater layoutInflater = LayoutInflater.from(this);
        View view = layoutInflater.inflate(R.layout.my_load_dialog, null);//获取布局

        LinearLayout layout = (LinearLayout) view.findViewById(R.id.load_llt);
        ImageView imageView = (ImageView) view.findViewById(R.id.load_img);
        TextView tView = (TextView) view.findViewById(R.id.load_tv);

        Animation animation = AnimationUtils.loadAnimation(this, R.anim.load_anim);
        imageView.startAnimation(animation);//加载动画

        Dialog loadDialog = new Dialog(this);
        loadDialog.setCancelable(true);//使返回键不作用与加载框
        loadDialog.setCanceledOnTouchOutside(false);//点击框外框不消失
        loadDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//去除标题栏
        loadDialog.setContentView(layout,new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT));
        loadDialog.show();

加载中

自定义样式和封装

    <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>

如下封装:

/**
     * 显示加载框
     * @param context
     * @return Dialog
     */
    public Dialog showLoadDialog(Context context){

        return showLoadDialog(context, "");
    }

    /**
     * 显示加载框,更改消息显示
     * @param context
     * @param showMessage
     * @return Dialog
     */
    public Dialog showLoadDialog(Context context,String showMessage){
        LayoutInflater layoutInflater = LayoutInflater.from(this);
        View view = layoutInflater.inflate(R.layout.my_load_dialog, null);//获取布局

        LinearLayout layout = (LinearLayout) view.findViewById(R.id.load_llt);
        ImageView imageView = (ImageView) view.findViewById(R.id.load_img);
        TextView tView = (TextView) view.findViewById(R.id.load_tv);

        if (!showMessage.equals("")) {
            tView.setText(showMessage);
        }

        Animation animation = AnimationUtils.loadAnimation(context, R.anim.load_anim);
        imageView.startAnimation(animation);//加载动画

        Dialog loadDialog = new Dialog(context,R.style.loading_dialog);
        loadDialog.setCancelable(true);//使返回键不作用与加载框
        loadDialog.setCanceledOnTouchOutside(false);//点击框外框不消失
        loadDialog.setContentView(layout,new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT));
        loadDialog.show();
        return loadDialog;
    }

这里写图片描述