如何在android中使用进度条填充图像

如何在android中使用进度条填充图像

本文介绍了如何在android中使用进度条填充图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个项目,它需要填充图像.意味着我想使用图像形状作为进度条.我不知道如何使用自定义进度条.这是一张图片和一个图片按钮.

I am working on a project and it requires to fill the image. means i want to use the image shape as a progress bar. I don't get any idea of how to use custom progress-bar. here is an image and its an image-button.

所以这是进度为 100% 的情况:

So this is when progress is 100%:

这是 0% 的进步:

推荐答案

你需要了解 drawable 资源.

You need to learn about drawable resources.

可绘制资源 |Android 开发者

你可以用一个图层列表和一个可绘制的剪辑来做到这一点.

You can do this with a Layer List and a Clip Drawable.

首先,让我们谈谈级别.每个可绘制对象都有一个从 0 到 10000 的级别.级别可用于修改可绘制对象的外观.

First, let's talk about level. Every drawable has a level that goes from 0 to 10000. The level can be used to modify the appearance of the drawable.

让我们从 Clip Drawable 开始.Clip Drawable 将根据其级别对 drawable 进行剪辑.这非常适合进度条样式.我假设您想在灰色的心上从下往上绘制红色的心.

Let's start with the Clip Drawable. A Clip Drawable will clip the drawable based on its level. This is great for progress-bar style. I'll assume you want to draw the red heart from the bottom up over the grey heart.

/res/drawable/red_heart_clip.xml

<?xml version="1.0" encoding="utf-8"?>
<clip
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/red_heart"
    android:clipOrientation="horizontal"
    android:gravity="bottom"/>

既然在向上的过程中已经有了显露,您想显示灰色的心作为背景,这样看起来灰色的心就会变成红色.您可以使用图层列表覆盖两个可绘制对象.

Now that you have the revealing on the way up, you want to show the grey heart as a background, so that it looks like the grey heart is becoming red. You can overlay two drawables with a Layer List.

/res/drawable/progress_heart.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/grey_heart"/>
    <item android:drawable="@drawable/red_heart_clip"/>
</layer-list>

现在您将这个 drawable 分配给视图的背景.它也可以是 ImageView 的 src.

Now you assign this drawable to the background of a view. It could also be the src of an ImageView.

<View
    android:background="@drawable/progress_heart"
    ...

现在你可以像这样设置drawable的级别:

Now you can set the level of the drawable like this:

    int level = 100 * pct;   // pct goes from 0 to 100
    view.getBackground().setLevel(level)

哦,顺便说一句,您甚至不需要 ProgressBar.

Oh, and BTW you don't even need a ProgressBar.

这篇关于如何在android中使用进度条填充图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 05:15