ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

app直播源码,实现进度条自增长及渐变样式

2022-06-07 14:35:21  阅读:193  来源: 互联网

标签:layout weight 进度条 app height 源码 android id


app直播源码,实现进度条自增长及渐变样式

上代码

activity_main.xml

 


<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="实现进度条自增长"
            android:textSize="50dp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="wrap_content"
            android:layout_height="72dp"
            android:layout_weight="1"
            android:max="100"
            android:progress="0"
            android:progressDrawable="@drawable/jdt" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="当前进度条值为:"
            android:textSize="50dp" />
    </LinearLayout>
</LinearLayout>

在UI界面上先设立一个水平的进度条和用于显示进度条值的TextView组件

代码LinearLayout中的weight用于显示比例

代码中水平进度条的 progressDrawable 用于设置进度条的渐变

MainActivity.java

 


package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
    ProgressBar progressBar;
    int i=0;//用于显示进度条的增长
    TextView textView;//显示当前进度条的值
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar=findViewById(R.id.progressBar);//带入
        textView=findViewById(R.id.textView2);//带入
        final Timer timer=new Timer();//实例化,设置计时器
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        i++;//自增长
                        textView.setText("当前进度条值为:"+i+"%");//用于显示当前进度条的值
            if(i==100){
                timer.cancel();//当i=100时停止
            }else{
                progressBar.setProgress(i);//否则随着i自增长
            }
                    }
                });
            }
        },100,100);
    }
}

 

以上就是app直播源码,实现进度条自增长及渐变样式, 更多内容欢迎关注之后的文章

 

标签:layout,weight,进度条,app,height,源码,android,id
来源: https://www.cnblogs.com/yunbaomengnan/p/16351615.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有