android 垂直方向进度条progressbar


在实际需求会碰到使用垂直方向的是进度条


一 水平方向进度条

1.先看看原生的水平方向进度条



<ProgressBar
        android:id="@+id/progress3"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:indeterminate="false"
        android:indeterminateOnly="false"
        android:max="100"
        android:progress="50"
        />


2.根据style="?android:attr/progressBarStyleHorizontal",我们找到源码中的style.xml


<style name="Widget.ProgressBar.Horizontal">
         <item name="android:indeterminateOnly">false</item>
         <item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
         <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
         <item name="android:minHeight">20dip</item>
         <item name="android:maxHeight">20dip</item>
     </style>


3.看到<item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
木有,继续发掘源码,找到drawable下面的progress_horizontal.xml,这就是我们今天的主角

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is di
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Android 提供了 ProgressBar 控件来显示进度条,但是默认情况下只支持水平和垂直两种进度条样式。如果想要实现半圆形进度条,需要自定义控件来实现。 以下是一个简单的实现半圆形进度条的示例: 1.创建一个继承自 View 的自定义控件 HalfCircleProgressBar。 2.在 HalfCircleProgressBar 类中添加如下代码: ``` private int mProgress = 0; // 进度值 private int mMax = 100; // 最大值 @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 画底部半圆 int radius = getWidth() / 2; int centerX = getWidth() / 2; int centerY = getHeight(); Paint paint = new Paint(); paint.setColor(Color.GRAY); canvas.drawCircle(centerX, centerY, radius, paint); // 画进度条半圆 paint.setColor(Color.BLUE); float sweepAngle = (float) mProgress / mMax * 180; canvas.drawArc(new RectF(0, 0, getWidth(), getHeight()), -90, sweepAngle, true, paint); } public void setProgress(int progress) { if (progress < 0) { progress = 0; } if (progress > mMax) { progress = mMax; } mProgress = progress; invalidate(); // 刷新页面 } public void setMax(int max) { if (max < 0) { max = 0; } mMax = max; } public int getMax() { return mMax; } public int getProgress() { return mProgress; } ``` 3.在布局文件中添加 HalfCircleProgressBar 控件: ``` <com.example.myapplication.HalfCircleProgressBar android:id="@+id/half_circle_progress_bar" android:layout_width="100dp" android:layout_height="100dp" /> ``` 4.在代码中使用 HalfCircleProgressBar 控件: ``` HalfCircleProgressBar progressBar = findViewById(R.id.half_circle_progress_bar); progressBar.setMax(100); progressBar.setProgress(50); ``` 这样就可以实现一个简单的半圆形进度条了。需要注意的是,这只是一个简单的实现,还可以根据需求进行更加复杂的自定义控件开发。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值