美文网首页自定义控件
android 抢购进度条

android 抢购进度条

作者: 一个冬季 | 来源:发表于2019-07-29 13:52 被阅读31次
GIF.gif
需求

需要做抢购,然后需要进度条,如上面的git动态图所示

可以自定义哪些参数

1、可以修改底色
2、可以修改上层颜色
3、可以修改图标

代码展示
     final int total = 1000;
     final int current = 600;
     jinDuProgressBar.post(new Runnable() {
            @Override
            public void run() {
                ValueAnimator valueAnimator =  ValueAnimator.ofFloat(0.0f, 1.0f);
                valueAnimator.setDuration(1000);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float currFloat = (float) animation.getAnimatedValue();
                        float currentValue = (float) current * currFloat;
                        Log.i(TAG,"currFloat:"+currFloat);
                        jinDuProgressBar.setCurrentProgress(currentValue,total);
                    }
                });
                valueAnimator.start();
            }
        });
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.example.gao.myapplication45.JinDuProgressBar
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp"
        app:smlIcon="@drawable/icon_tubiao"
        app:undertoneColor="@color/color_dcdcdcdc"
        app:upperstrataColor="@color/colorAccent"
        android:id="@+id/main_jindu"
        android:layout_width="125dp"
        android:layout_height="10dp"/>

</LinearLayout>
public class JinDuProgressBar extends FrameLayout {
    private String TAG = JinDuProgressBar.class.getSimpleName();
    private Context mContext;
    private int totalWidth, totalHeight;
    private Paint mPaint;
    private int radius;
    private float mCurrentProgress = 0.0f;
    private ImageView imageView;

    private int undertoneColor;//底色
    private int upperstrataColor;//上层色
    private Drawable ivDrawable;


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

    public JinDuProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public JinDuProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs,R.styleable.JinDuProgressBar);
        ivDrawable = typedArray.getDrawable(R.styleable.JinDuProgressBar_smlIcon);
        undertoneColor = typedArray.getColor(R.styleable.JinDuProgressBar_undertoneColor,mContext.getResources().getColor(R.color.color_dcdcdcdc));
        upperstrataColor = typedArray.getColor(R.styleable.JinDuProgressBar_upperstrataColor,mContext.getResources().getColor(R.color.colorAccent));

        if (ivDrawable==null){
            return;
        }

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        radius = dip2px(mContext, 50);
        setWillNotDraw(false);
        imageView = new ImageView(mContext);
        imageView.setImageDrawable(ivDrawable);
        this.addView(imageView);
        FrameLayout.LayoutParams layoutParams = (LayoutParams) imageView.getLayoutParams();
        layoutParams.width = dip2px(mContext,10);
        layoutParams.height = dip2px(mContext,10);
        imageView.setLayoutParams(layoutParams);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        totalWidth = getMeasuredWidth();
        totalHeight = getMeasuredHeight();
    }

    //暴露给外面调用 设置当前值 不断调用ondraw方法
    public synchronized  void setCurrentProgress(float currentNum, float totalSum) {
        if (totalWidth<=0){
            return;
        }
        float value =  currentNum * (float) totalWidth / totalSum;
        this.mCurrentProgress = value;
        float tranx = mCurrentProgress - (float)(imageView.getWidth());
        if (tranx <=0){
            imageView.setTranslationX(0);
        }else {
            imageView.setTranslationX(tranx);
        }
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //绘画底部的底色
        mPaint.setColor(undertoneColor);
        RectF bottomRectF = new RectF(0, 0, totalWidth, totalHeight);
        canvas.drawRoundRect(bottomRectF, radius, radius, mPaint);

        mPaint.setColor(upperstrataColor);
        RectF rectF = new RectF(0, 0, mCurrentProgress, totalHeight);
        canvas.drawRoundRect(rectF, radius, radius, mPaint);

    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    private int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}

github

相关文章

  • android 抢购进度条

    需求 需要做抢购,然后需要进度条,如上面的git动态图所示 可以自定义哪些参数 1、可以修改底色2、可以修改上层颜...

  • 控件 - ProgressBar

    ProgressBar 进度条 进度条 属性 android:id:ID,唯一标识符。 android:layou...

  • ProgressBar进度条

    SeekBar :搜寻进度条RatingBar:评价进度条 style="?android:attr/progre...

  • Android进度条

    Android进度条 不同的进度条显示结果: demo xml代码: 二.模拟进度条加载过程:运行展示图: xml...

  • Android之进度条控件和常用资源分类总结

    1 基本UI(二) 1.1进度条 1.1.1 【 常用属性: style进度条样式 Android:max进度...

  • SeekBar

    Android-SeekBar进度条的使用Android控件与布局——基础控件SeekBar

  • 自定义进度条

    1.自定义进度条UI 2.进度条动效 Animate ProgressBar update in Android

  • 进度条使用

    目录 ProgressBar(进度条) ProgressBar是Android下的进度条,也是为数不多的直接继承于...

  • ProgressBar的美化

    横向进度条 style="?android:attr/progressBarStyleHorizontal"制定进...

  • SeekBar自定义样式最优实现

    1. 布局文件 android:thumb为滑块的样式 android:progressDrawable为进度条样...

网友评论

    本文标题:android 抢购进度条

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