如何在Android中使用ProgressBar填充图像 [英] how to fill images using progressbar in android

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

问题描述

我正在一个项目上,它需要填充图像.表示我想将图像形状用作进度条.我不知道如何使用自定义进度栏.这是一个图像及其图像按钮.

所以这是进度为100%的时间:

而进度为0%

解决方案

您需要了解可绘制资源.

可绘制资源| Android开发人员

您可以使用图层列表"和剪贴画"来完成此操作.

首先,让我们谈谈水平.每个可绘制对象的级别都在0到10000之间.该级别可用于修改可绘制对象的外观.

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

/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"/>

现在,您的显示区域正在上升,您希望将灰色的心形显示为背景,以便看起来灰色的心形变为红色.您可以使用图层列表覆盖两个可绘制对象.

/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>

现在,您将此可绘制对象分配给视图的背景.也可以是ImageView的src.

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

现在您可以像这样设置可绘制对象的级别:

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

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

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.

So this is when progress is 100%:

and this for 0% progress:

解决方案

You need to learn about drawable resources.

Drawable Resources | Android Developers

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

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.

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>

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"
    ...

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)

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆