Android studio实现水平进度条

这篇具有很好参考价值的文章主要介绍了Android studio实现水平进度条。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

原文
ProgressBar

用于显示某个耗时操作完成的百分比的组件称为进度条。ProgressBar默认产生圆形进度条。
实现效果图:
Android studio实现水平进度条,android studio,android,ide

MainActivity

import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.widget.Button;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends Activity {

    private RoundedRectProgressBar bar;
    private Button btn;
    private int progress;
    private Timer timer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bar = (RoundedRectProgressBar) findViewById(R.id.bar);
        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reset();
            }
        });

    }

    /**
     * 进度条从头到尾跑一次
     */
    private void reset() {
        progress = 0;
        timer = new Timer();
        //以timer为参数,指定某个时间点执行线程
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                bar.setProgress(progress);//设置当前进度
                progress ++;
                if (progress > 100) {
                    timer.cancel();
                }
            }
        }, 0, 300); 0秒后启动任务,以后每隔0.3秒执行一次线程
    }
}

自定义进度条RoundedRectProgressBar.java:

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class RoundedRectProgressBar extends View {

    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private int barColor;
    private int backColor;
    private int textColor;
    private float radius;

    int progress = 0;

    @SuppressLint("NonConstantResourceId")
    public RoundedRectProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        //获取自定义参数的颜色值
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.RoundedRectProgressBar, defStyle, 0);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            if (attr == R.styleable.RoundedRectProgressBar_backColor) {
                backColor = a.getColor(attr, Color.GRAY);
            } else if (attr == R.styleable.RoundedRectProgressBar_barColor) {
                barColor = a.getColor(attr, Color.GREEN);
            } else if (attr == R.styleable.RoundedRectProgressBar_textColor) {
                textColor = a.getColor(attr, Color.WHITE);
            }
        }
    }


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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        radius = this.getMeasuredHeight() / 5;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //背景
        mPaint.setColor(backColor);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawRoundRect(new RectF(0, 0, this.getMeasuredWidth(), this.getMeasuredHeight()), radius, radius, mPaint);
        //进度条
        mPaint.setColor(barColor);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawRoundRect(new RectF(0, 0, this.getMeasuredWidth() * progress / 100f, this.getMeasuredHeight()), radius, radius, mPaint);
        //进度
        mPaint.setColor(textColor);
        mPaint.setTextSize(this.getMeasuredHeight() / 1.2f);
        String text = "" + progress + "%";
        float x = this.getMeasuredWidth() * progress / 100 - mPaint.measureText(text) - 10;
        float y = this.getMeasuredHeight() / 2f - mPaint.getFontMetrics().ascent / 2f - mPaint.getFontMetrics().descent / 2f;
        canvas.drawText(text, x, y, mPaint);
    }

    /*设置进度条进度, 外部调用*/
    public void setProgress(int progress) {
        if (progress > 100) {
            this.progress = 100;
        } else if (progress < 0) {
            this.progress = 0;
        } else {
            this.progress = progress;
        }
        postInvalidate();
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    tools:context="com.example.progressbarpro.MainActivity">

    <com.example.progressbarpro.RoundedRectProgressBar
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="24dp"
        android:layout_marginTop="100dp"
        app:backColor="#E6E6E6"
        app:barColor="#33CC99"
        app:textColor="#FFFFFF"/>

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="reset"
        android:layout_centerInParent="true"/>
</RelativeLayout>

在values/attrs.xml中添加自定义参数, 使三种颜色可以在布局文件中被配置:

attrs.xml文章来源地址https://www.toymoban.com/news/detail-682141.html

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="RoundedRectProgressBar">
        <attr name="backColor" format="color" />
        <attr name="barColor" format="color" />
        <attr name="textColor" format="color" />
    </declare-styleable>

</resources>

到了这里,关于Android studio实现水平进度条的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Android Studio最新版:TextView字体加粗、水平居中和垂直居中

    Android Studio最新版:TextView字体加粗、水平居中和垂直居中 在Android应用程序的开发过程中,我们经常需要对界面上的文本进行样式设置,其中包括字体加粗以及水平居中和垂直居中显示。本文将介绍如何使用最新版的Android Studio实现这些效果,并提供相应的源代码作为参考。

    2024年01月23日
    浏览(39)
  • Android Studio最新版本中实现TextView字体加粗、水平居中和垂直居中的方法

    Android Studio最新版本中实现TextView字体加粗、水平居中和垂直居中的方法 在Android应用程序开发中,TextView是常用的控件之一,用于在界面上显示文本内容。有时候我们需要对TextView进行样式和布局的设置,比如将字体加粗、水平居中和垂直居中。本文将介绍如何在最新版本的

    2024年02月04日
    浏览(32)
  • Android studio下的线性布局(LinearLayout)与水平布局(ReativeLayout)详细解析+典型例子及其代码

    一:线性布局 线性布局有水平线性布局: android :orientation =\\\"horizontal\\\" ; 和垂直线性布局: android :orientation =\\\"vertical\\\" 两种布局。 当代码表示 android :orientation =\\\"horizontal\\\" 时, 表示这个布局下的所有子元素都要水平方向排列。 当代码表示 android :orientation =\\\"verticall\\\" 时, 表示这个布

    2024年02月09日
    浏览(36)
  • Android 实现环形进度条

    一、项目需求 项目中常常需要用到进度条,很简单,这儿做一个简单的总结和实现 二、实现控件 ProgressBar 三、实现代码 1、水平的进度条 xml布局代码: style属性:确定他是什么类型的进度条 progressDrawable属性:确定进度条的背景,进度条颜色等等 mmmmmm.xml:drawable文件代码

    2024年02月01日
    浏览(68)
  • android实现简单进度条ProgressBar

    记录一下今天学习的进度条ProgressBar 1、在布局文件中添加ProgressBar 其中 **style=“@android:style/Widget.ProgressBar.Horizontal”**设置进度条样式为水平进度条,否则默认原型旋转的进度条; max 设置进度条长度,这里设置为100。 2、java代码: 声明ProgressBar;int型的mprogress表示进度条进度

    2023年04月08日
    浏览(23)
  • android仿QQ列表实现 android studio大作业,android studio课程设计

    1. 效果图 2.功能介绍:登录,注册,好友列表 3.核心代码

    2024年02月11日
    浏览(36)
  • Android 使用Retrofit+协程实现超简单大文件下载并回显进度条

    安卓自带的进度条弹窗过时了,这里简单创建一个进度条弹窗 在 drawable 文件夹创建 progress_dialog_bg_style.xml 一个圆角白色背景样式 创建 alert_dialog_download_progress.xml 布局 创建弹窗工具类,使用刚才创建好的布局 简单封装一个下载工具类 先定义一个下载参数实体 DownloadDTO 编写下

    2024年02月12日
    浏览(43)
  • Android studio实现财务记账系统软件android studio开发课程设计

    Android 开发一个理财记账系统软件(app)的详细实验步骤,内附有源码,小白也能轻松上手自己的软件开发项目学生可当课程设计学习使用。 系统登录/退出 设置/修改密码 主活动页面设计 增加收入记录页面 查看收入记录页面 修改和删除记录页面 1、实验名称 数据库的创建 2.

    2024年02月10日
    浏览(40)
  • android studio大作业,android studio课程设计,记事本实现

    先看效果图 功能点实现: 登录,注册,记事本分类添加,删除,数据分析统计报表,数据库使用SQLlite 部分实现代码

    2024年02月11日
    浏览(38)
  • Android Studio实现飞机大战

    通过自定义View实现Android飞机大战小游戏,游戏玩法很简单,可以锻炼玩家的反应能力。开启背景音乐进行新的游戏,控制飞机移动来消灭敌机获取更多的分数,在移动过程中避免与敌机发生碰撞。主界面可以查看自己的历史战绩和游戏规则,详细规则如下: 我们定义了 Sp

    2024年02月16日
    浏览(22)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包