在线咨询
微信咨询
服务热线
服务热线:15639912513
TOP
当前位置:
首页 > 新闻中心> 安卓课堂>Android自定义控件loading等待

Android自定义控件loading等待

发布时间:2020-03-16 浏览:4599次

郑州app开发自定义控件loading等待。因为在制作项目中,需要自己动手制作空间。下面是关于loading等待控件的全部代码。

import android.annotation.SuppressLint;

import android.content.Context;

import android.graphics.Canvas;

import android.util.AttributeSet;

import android.widget.ImageView;


import androidx.annotation.Nullable;


import cn.xhhkj.himalaya.R;


@SuppressLint("AppCompatCustomView")

public class LoadingView extends ImageView {

    //旋转角度

    private int rotateDegree=0;

    private boolean mNeedRotate=false;

    public LoadingView(Context context) {

        this(context,null);

    }


    public LoadingView(Context context, @Nullable AttributeSet attrs) {

        this(context, attrs,0);

    }


    public LoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);

        //设置图标

        setImageResource(R.mipmap.loading);

    }


    @Override

    protected void onAttachedToWindow() {

        super.onAttachedToWindow();

        mNeedRotate=true;

        //绑定到window的时候

        post(new Runnable() {

            @Override

            public void run() {

                rotateDegree+=30;

                rotateDegree=rotateDegree<=360?rotateDegree:0;

                invalidate();

                //是否继续旋转

                if (mNeedRotate){

                    postDelayed(this,100);

                }


            }

        });

    }


    @Override

    protected void onDetachedFromWindow() {

        super.onDetachedFromWindow();

        //从window中解绑了

        mNeedRotate=false;

    }


    @Override

    protected void onDraw(Canvas canvas) {

        /**

         * 第一个参数是旋转的角度

         * 第二个参数是旋转的x坐标

         * 第三个参数是旋转的y坐标

         */

        canvas.rotate(rotateDegree,getWidth()/2,getHeight()/2);

        super.onDraw(canvas);


    }

}


 


TAG
4599
该内容对我有帮助