美文网首页
自定义View_一个带悬浮窗的ProgressBar(下)

自定义View_一个带悬浮窗的ProgressBar(下)

作者: BraveJoy | 来源:发表于2024-02-20 22:42 被阅读0次

经过了前两篇文章的介绍,我们已经分别完成了progress和悬浮窗的效果。文章见:
【自定义View_一个带悬浮窗的ProgressBar(上)】
【自定义View_一个带悬浮窗的ProgressBar(中)】
现在我们把两个效果合并起来看一下是怎样的呢。如下图:

View合并→ViewGroup

现在,我们来把两个独立的View合并成一个ViewGroup来使用。由于组合效果是上下进行叠加,那么我们就直接继承自LinearLayout。

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

public class ProgressLayout extends LinearLayout {
    // 悬浮窗的高度,单位dp
    private static final float INFO_HEIGHT = 50;
    // 进度条的高度,单位dp
    private static final float PROGRESS_HEIGHT = 15;
    // 进度条距离悬浮窗的间隔距离,单位dp
    private static final float TOP_DISTANCE = 10;
    // 悬浮窗控件
    private InfoView infoView;
    // 进度条控件
    private ProgressView progressView;

    public ProgressLayout(Context context) {
        this(context, null);
    }

    public ProgressLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(LinearLayout.VERTICAL);
        setClipChildren(false);
        setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        initView(context);
    }

    private void initView(Context context) {
        // 悬浮窗
        infoView = new InfoView(context);
        infoView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Utils.dp2px(context, INFO_HEIGHT)));
        addView(infoView);

        // 进度条
        progressView = new ProgressView(context);
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Utils.dp2px(context, PROGRESS_HEIGHT));
        layoutParams.setMargins(0, Utils.dp2px(context, TOP_DISTANCE), 0, 0);
        progressView.setLayoutParams(layoutParams);
        addView(progressView);
    }


    /**
     * 设置进度,带动画
     *
     * @param progress 当前进度
     */
    public void setProgressWithAnim(float progress) {
        infoView.setProgressWithAnim(progress);
        progressView.setProgressWithAnim(progress);
    }
}

到这里,其实已经完成了初步的效果,距离实际效果还需要进行升级改造,下一篇文章我们将在此效果的基础上进行拓展实现,敬请期待。

相关文章

网友评论

      本文标题:自定义View_一个带悬浮窗的ProgressBar(下)

      本文链接:https://www.haomeiwen.com/subject/kgpomdtx.html