如何在确定模式下使用 Android ProgressBar?

发布于 2024-09-04 02:23:02 字数 166 浏览 5 评论 0原文

我正在编写一个媒体播放器,我希望有一个进度条显示歌曲的进度。我找到了 ProgressBar 类,但我在屏幕上只能看到一个圆形旋转图标。我正在寻找的是一个真正的酒吧。

如何将 ProgressBar 的样式更改为条形(而不是圆形)以及如何将其与 MediaPlayer 一起使用?

谢谢

I am writing a media player and i would like to have a progress bar showing the progress of the song. I found the ProgressBar class, but all i can get on the screen is a circular spinning icon. what im looking for is an actual bar.

How do i change the style of the ProgressBar to be a bar (not a circle) and how would i use it with MediaPlayer?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尬尬 2024-09-11 02:23:02

使用样式 ?android:attr/progressBarStyleHorizo​​ntal

例如:

  <ProgressBar android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"

这是 MediaPlayer 的示例:

package com.playerpgbar;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Player extends Activity implements Runnable, OnClickListener {

    private TextView status;
    private ProgressBar progressBar;
    private Button startMedia;
    private Button stop;
    private MediaPlayer mp;      

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        status = (TextView) findViewById(R.id.status);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        startMedia = (Button) findViewById(R.id.startMedia);
        stop = (Button) findViewById(R.id.stop);

        startMedia.setOnClickListener(this);
        stop.setOnClickListener(this);                
    }        

    @Override
    public void onClick(View v) {
        if (v.equals(startMedia)) {
            if (mp != null && mp.isPlaying()) return;
            mp = MediaPlayer.create(Player.this, R.raw.exodus_piranha);
            mp.start();               
            status.setText(R.string.PlayingMedia);         
            progressBar.setVisibility(ProgressBar.VISIBLE);
            progressBar.setProgress(0);
            progressBar.setMax(mp.getDuration());
            new Thread(this).start();
        }

        if (v.equals(stop) && mp!=null) {
            mp.stop();
            mp = null;            
            status.setText(R.string.Stopped);
            progressBar.setVisibility(ProgressBar.GONE);
        }
    }

    @Override
    public void run() {
        int currentPosition= 0;
        int total = mp.getDuration();
        while (mp!=null && currentPosition<total) {
            try {
                Thread.sleep(1000);
                currentPosition= mp.getCurrentPosition();
            } catch (InterruptedException e) {
                return;
            } catch (Exception e) {
                return;
            }            
            progressBar.setProgress(CurrentPosition);
        }
    }
}

use the style ?android:attr/progressBarStyleHorizontal

for example:

  <ProgressBar android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"

and this is an example with MediaPlayer:

package com.playerpgbar;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Player extends Activity implements Runnable, OnClickListener {

    private TextView status;
    private ProgressBar progressBar;
    private Button startMedia;
    private Button stop;
    private MediaPlayer mp;      

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        status = (TextView) findViewById(R.id.status);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        startMedia = (Button) findViewById(R.id.startMedia);
        stop = (Button) findViewById(R.id.stop);

        startMedia.setOnClickListener(this);
        stop.setOnClickListener(this);                
    }        

    @Override
    public void onClick(View v) {
        if (v.equals(startMedia)) {
            if (mp != null && mp.isPlaying()) return;
            mp = MediaPlayer.create(Player.this, R.raw.exodus_piranha);
            mp.start();               
            status.setText(R.string.PlayingMedia);         
            progressBar.setVisibility(ProgressBar.VISIBLE);
            progressBar.setProgress(0);
            progressBar.setMax(mp.getDuration());
            new Thread(this).start();
        }

        if (v.equals(stop) && mp!=null) {
            mp.stop();
            mp = null;            
            status.setText(R.string.Stopped);
            progressBar.setVisibility(ProgressBar.GONE);
        }
    }

    @Override
    public void run() {
        int currentPosition= 0;
        int total = mp.getDuration();
        while (mp!=null && currentPosition<total) {
            try {
                Thread.sleep(1000);
                currentPosition= mp.getCurrentPosition();
            } catch (InterruptedException e) {
                return;
            } catch (Exception e) {
                return;
            }            
            progressBar.setProgress(CurrentPosition);
        }
    }
}
给我一枪 2024-09-11 02:23:02

以编程方式:

progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleHorizontal);
progressBar.setIndeterminate(false);

Programmatically:

progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleHorizontal);
progressBar.setIndeterminate(false);
执妄 2024-09-11 02:23:02

在较新版本的 材质设计库 中,圆形进度条可能是也确定

<com.google.android.material.progressindicator.CircularProgressIndicator
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

此外,如果您的进度条最初是不确定的,线性和循环进度指示器都可以平滑地从不确定模式切换到确定模式:

int progress = getLoadingProgress()
indicator.setProgressCompat(progress, true)

来源

In newer versions of material design library, the circular progress bar could be determinate too:

<com.google.android.material.progressindicator.CircularProgressIndicator
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Also, if your progress bar is initially indeterminate, both linear and circular progress indicators can smoothly switch from indeterminate mode to determinate mode:

int progress = getLoadingProgress()
indicator.setProgressCompat(progress, true)

Source

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文