我一直在寻找自定义进度条的教程和95%的我发现是如何自定义使用颜色和渐变与图像(基本上是一个图像的空栏图像或顶部的完整酒吧)。当我尝试使用进度条图像时,尺寸错误(包装内容无法正常工作,并且截断了一半)。自定义进度条与图像或图像查看与剪贴
我能够实现一个成功的酒吧与图像使用图像与剪贴画和水平设置。
SOOO,是ProgressBar与图像用于其背景/进展皱眉,我应该使用imageview呢?
我一直在寻找自定义进度条的教程和95%的我发现是如何自定义使用颜色和渐变与图像(基本上是一个图像的空栏图像或顶部的完整酒吧)。当我尝试使用进度条图像时,尺寸错误(包装内容无法正常工作,并且截断了一半)。自定义进度条与图像或图像查看与剪贴
我能够实现一个成功的酒吧与图像使用图像与剪贴画和水平设置。
SOOO,是ProgressBar与图像用于其背景/进展皱眉,我应该使用imageview呢?
关键是要确保ProgressBar
帐户为您的自定义绘图的尺寸。一种方法是覆盖onMeasure
。这里是你的自定义类的onMeasure
实现的草图(比较这对ProgressBar
的实现 - 你会发现微妙的变化):
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
// the super's onMeasure method ignores the dimensions of the custom progress drawable
// if they are greater than a specified height & width (mMaxHeight & mMaxWidth). It simply uses those
// default dimensions for the drawable, consequently resizing them; which is not suitable for larger drawables.
// So, it is necessary to override this method to allow the ProgressBar to account for the drawable's original
// dimensions and draw the image/drawable accordingly.
Drawable d = getProgressDrawable();
int dw = 0;
int dh = 0;
if (d != null) {
dw = d.getIntrinsicWidth();
dh = d.getIntrinsicHeight();
}
int[] state = getDrawableState();
if(mProgressDrawable != null && mProgressDrawable.isStateful())
mProgressDrawable.setState(state);
dw += getPaddingLeft() + getPaddingRight();
dh += getPaddingTop() + getPaddingBottom();
setMeasuredDimension(resolveSize(dw, widthMeasureSpec), resolveSize(dh, heightMeasureSpec));
}
然后,您可以将您的空栏为背景,自定义ProgressBar
像你通常会做一个视图 - android:background="@drawable/empty_bar"
接下来的部分是设置progressDrawable
,为你将不得不使用一个<layer-list>
,因为我们要密切配合进度条的绘制结构(default drawable)。下面是一个示例:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/background">
<shape>
<solid android:color="#00000000"/>
</shape>
</item>
<item android:id="@android:id/progress">
<clip
android:clipOrientation="vertical"
android:gravity="bottom"
android:drawable="@drawable/full_bar">
</clip>
</item>
</layer-list>
最后的动画,你可以使用一个ObjectAnimator
的进度:
final ObjectAnimator animator = ObjectAnimator
.ofInt(progressBar, "progress", 0, 100)
.setDuration(2000);
animator.start();
我不认为这是令人难以接受的。实际上,'ProgressBar'固有地使用可绘制的剪辑来完成动画效果。所以,我看不出为什么你不应该使用它。但正如你所表明的那样,唯一的麻烦就是视角如何度量自身。所以你将不得不重写'onMeasure'来确保你的drawable没有被切断 – Abhijit
谢谢,有什么联系或者例子来说明怎么实现的? – Snake