自定义ViewGroup实现进度动态更新效果

自定义ViewGroup实现进度动态更新效果,效果图如下:


package com.example.think.myapplication;


import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.animation.AccelerateInterpolator;
import android.widget.LinearLayout;


/**
 * 投票进度显示容器
 * @author hiphonezhu@gmail.com
 */
public class VoteGroup extends LinearLayout {
    private float cProgress; // 当前进度
    private Paint mPaint; // 背景色画笔
    private long mDuration = 1500; // 动画持续时间


    private final int MAX_ALPHA = 155; // 最大透明阀值
    private final int MAX_PROGRESS = 100; // 最大进度值


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


    public VoteGroup(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }


    public VoteGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
    }


    /**
     * 设置动画时间
     * @param duration 毫秒
     */
    public void setDuration(int duration)
    {
        mDuration = duration;
    }


    /**
     * 非动画显示进度
     * @param progress 进度
     * @param color 颜色值
     */
    public void setProgress(float progress, int color)
    {
        setProgress(progress, color, false);
    }


    /**
     * 是否使用动画方式显示进度
     * @param progress 进度
     * @param color 颜色值
     * @param animated 是否使用动画
     */
    public void setProgress(float progress, int color, boolean animated)
    {
        mPaint.setColor(color);
        if (animated)
        {
            ValueAnimator animator = ValueAnimator.ofFloat(0f, progress);
            animator.setDuration(mDuration);
            animator.setInterpolator(new AccelerateInterpolator());
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    invalidate((float)animation.getAnimatedValue());
                }
            });
            animator.start();;
        }
        else
        {
            invalidate(progress);
        }
    }


    /**
     * 刷新视图
     * @param progress 进度
     */
    private void invalidate(float progress)
    {
        if (progress > MAX_PROGRESS)
        {
            progress = MAX_PROGRESS;
        }
        cProgress = progress;
        /**
         * 设置透明度
         * @see #MAX_ALPHA
         */
        int alpha = (int)((cProgress / MAX_PROGRESS) * MAX_ALPHA);
        mPaint.setAlpha(alpha);
        invalidate();
    }


    /**
     * 绘制完children之后根据进度绘制自身
     * @param canvas
     */
    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        canvas.drawRect(0, 0, getMeasuredWidth() * cProgress / MAX_PROGRESS, getMeasuredHeight(), mPaint);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android自定义ViewGroup是指在Android开发中,通过继承ViewGroup类来创建自定义的布局容器。自定义ViewGroup可以用于实现一些特殊的布局效果,比如侧滑菜单、滑动卡片等等。通过自定义ViewGroup,我们可以更灵活地控制子视图的布局和交互行为,以满足特定的需求。自定义ViewGroup实现主要包括重写onMeasure()方法和onLayout()方法,来测量和布局子视图。同时,我们还可以通过重写onInterceptTouchEvent()方法和onTouchEvent()方法来处理触摸事件,实现自定义的交互效果。如果你对自定义ViewGroup还不是很了解,或者正想学习如何自定义,可以参考相关的教程和文档,如引用\[1\]和引用\[2\]所提到的博客和官方文档。 #### 引用[.reference_title] - *1* [Android 手把手教您自定义ViewGroup(一)](https://blog.csdn.net/iteye_563/article/details/82601716)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [使用LayoutParams自定义安卓ViewGroup](https://blog.csdn.net/lfq88/article/details/127268493)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Android自定义ViewGroup](https://blog.csdn.net/farsight2009/article/details/62046643)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值