progress带文字跟随,progress滚动颜色渐变

  话不多说上图

 

没有自定义view思路就是上面的textView跟随下面progressBar的进度条做平移动画

因为是模拟用handler发送消息然后Timer模拟progressbar进度条增长

MainActivity.java

public class MainActivity extends AppCompatActivity {

    TextView tvPrecent;
    ProgressBar pbProgressbar;
    Button btnStart;

    //当前进度
    private int currentStatue;
    /**
     * 当前位置
     */
    private float currentPosition;


    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0x101) {
                //计时设置进度
                timer.schedule(new TimerTask() {
                    private float scrollDistance;
                    @Override
                    public void run() {
                        if(currentStatue>=100){
                            Log.e("计时器","---"+currentStatue);
                            timer.cancel();
                            return;
                        }
                        runOnUiThread(new Runnable() {

                            private float aFloat;
                            private int anInt;

                            @Override
                            public void run() {
                                scrollDistance = (float) ((1.0 / pbProgressbar.getMax()) * width);
                                pbProgressbar.setProgress(currentStatue);
                                // 控制进度条的增长进度
                                pbProgressbar.incrementProgressBy(1);
                                currentStatue++;
                                tvPrecent.setText(currentStatue + "%");
                                // 得到字体的宽度
                                int tvWidth = tvPrecent.getWidth();
                                currentPosition += scrollDistance;
                                //做一个平移动画的效果
                                anInt = width - tvPrecent.getPaddingRight();
                                aFloat = currentPosition;
                                if (aFloat <= anInt) {
                                    tvPrecent.setTranslationX(currentPosition);
                                }
                            }
                        });

                    }
                }, 0, 100);
            }
        }
    };

    private int width;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvPrecent = (TextView) findViewById(R.id.progress_precent);
        pbProgressbar = (ProgressBar) findViewById(R.id.pb_progressbar);
        btnStart = (Button) findViewById(R.id.btn_start);

        // 得到progressBar控件的宽度
        ViewTreeObserver vto2 = pbProgressbar.getViewTreeObserver();
        vto2.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {



            @Override
            public void onGlobalLayout() {
                pbProgressbar.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                //获取pbProgressbar宽度
                width = pbProgressbar.getWidth();
            }
        });

        //初始化监听
        initListener();

    }
    private Timer timer;
    private void initListener() {
        //开始按钮的点击事件
        btnStart.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                currentStatue = 0;
                currentPosition = 0;
                pbProgressbar.setProgress(0);
                timer = new Timer();
                mHandler.sendEmptyMessage(0x101);
            }
        });
    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:id="@+id/progress_precent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dp"
        android:text="0%"
        android:textColor="#ff00"
        android:textSize="12sp"
        custom:layout_constraintBottom_toTopOf="@+id/pb_progressbar"
        custom:layout_constraintEnd_toStartOf="@+id/pb_progressbar"
        custom:layout_constraintStart_toStartOf="@+id/pb_progressbar"
        tools:ignore="MissingConstraints" />

    <ProgressBar
        android:id="@+id/pb_progressbar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="20dp"
        android:max="100"
        android:progress="0"
        android:progressDrawable="@drawable/progress_round"
        custom:layout_constraintBottom_toTopOf="@+id/btn_start"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="0dp" />

    <Button
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="start"
        custom:layout_constraintBottom_toBottomOf="parent"
        custom:layout_constraintEnd_toEndOf="parent"
        custom:layout_constraintStart_toStartOf="parent"
        custom:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

progress_round.xml    设置背景颜色和弧形圆角

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--这里是设置progressbar的背景样式-->
    <item
        android:id="@android:id/background"
        android:drawable="@drawable/progress_bar_background" />

    <!--这里是设置progressbar的进度条样式-->
    <item android:id="@android:id/progress">
        <scale
            android:drawable="@drawable/shape_progressbar_progress"
            android:scaleWidth="100%" />
    </item>
</layer-list>

shape_progressbar_progress.xml     设置颜色渐变

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="20dp" />
    <gradient
        android:endColor="#FFE404"
        android:startColor="#FF7200" />
</shape>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值