视频直播app源码,实现进度条的自增长和颜色渐变

视频直播app源码,实现进度条的自增长和颜色渐变的相关代码

一、进度条自增长

activity_main.xml

 

1
csharp<br><?xml version="1.0" encoding="utf-8"?><br><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br>    xmlns:app="http://schemas.android.com/apk/res-auto"<br>    xmlns:tools="http://schemas.android.com/tools"<br>    android:layout_width="match_parent"<br>    android:layout_height="match_parent"<br>    android:orientation="vertical"<br>    tools:context=".MainActivity"><br>    <LinearLayout<br>        android:layout_width="match_parent"<br>        android:layout_height="0dp"<br>        android:layout_weight="1"<br>        android:orientation="horizontal"><br>        <TextView<br>            android:id="@+id/textView"<br>            android:layout_width="wrap_content"<br>            android:layout_height="wrap_content"<br>            android:layout_weight="1"<br>            android:text="实现进度条自增长"<br>            android:textSize="50dp" /><br>    </LinearLayout><br>    <LinearLayout<br>        android:layout_width="match_parent"<br>        android:layout_height="0dp"<br>        android:layout_weight="1"<br>        android:orientation="horizontal"><br>        <ProgressBar<br>            android:id="@+id/progressBar"<br>            style="?android:attr/progressBarStyleHorizontal"<br>            android:layout_width="wrap_content"<br>            android:layout_height="72dp"<br>            android:layout_weight="1"<br>            android:max="100"<br>            android:progress="0"<br>            android:progressDrawable="@drawable/jdt" /><br>    </LinearLayout><br>    <LinearLayout<br>        android:layout_width="match_parent"<br>        android:layout_height="0dp"<br>        android:layout_weight="1"<br>        android:orientation="horizontal"><br>        <TextView<br>            android:id="@+id/textView2"<br>            android:layout_width="wrap_content"<br>            android:layout_height="wrap_content"<br>            android:layout_weight="1"<br>            android:text="当前进度条值为:"<br>            android:textSize="50dp" /><br>    </LinearLayout><br></LinearLayout>

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

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

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

MainActivity.java

 

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

 

二、颜色随着渐变

 

1
csharp<br><?xml version="1.0" encoding="utf-8"?><br><layer-list xmlns:android="http://schemas.android.com/apk/res/android"><br>    <item android:id="@+id/progress"><br>        <clip><br>            <shape><br>                <corners android:radius="5dp"/><br>                <gradient android:angle="0"<br>                    android:startColor="#33A72B"<br>                    android:endColor="#2C4EAD"/><br>            </shape><br>        </clip><br>    </item><br></layer-list>

 

以上就是 视频直播app源码,实现进度条的自增长和颜色渐变的相关代码,更多内容欢迎关注之后的文章

 

posted @   云豹科技-苏凌霄  阅读(263)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示