Android:进度条作为占位符

发布于 2024-11-17 02:36:12 字数 506 浏览 2 评论 0原文

我希望弄清楚如何在获取图像时使用进度条旋转器作为占位符。我放入了一个动画 .gif,但不久之后我发现 .gif 在 Android 上运行不佳。现在是我的代码:

public void displayImage(String url, CarActivity activity, ImageView imageView) {
        if(imageMap.containsKey(url))
            imageView.setImageBitmap(imageMap.get(url));
        else {
            queueImage(url, activity, imageView);
            imageView.setImageResource(R.drawable.loading);

        }
    }

如何使用内置微调器作为占位符,并可能将其覆盖在另一个图像上,而不是将图像设置为我的loading.gif。

谢谢!

I was hoping to figure out how to use a progress bar spinner to be a place holder while my image is being fetched. I put an animated .gif in but soon after I found out that .gif don't play nice with android. Here is my code now:

public void displayImage(String url, CarActivity activity, ImageView imageView) {
        if(imageMap.containsKey(url))
            imageView.setImageBitmap(imageMap.get(url));
        else {
            queueImage(url, activity, imageView);
            imageView.setImageResource(R.drawable.loading);

        }
    }

Instead of setting the image to my loading.gif how can I use the built in spinner as a place holder and possibly have it overlaid on another image.

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云仙小弟 2024-11-24 02:36:12

您真正的问题是“如何将一个视图放置在另一个视图之上”。在你的例子中,你似乎想要一个 ImageView 在后面,一个 ProgressBar 在前面。 (ProgressBar 的名称有点不直观,因为默认情况下它不是一个栏,而是一个小的旋转动画)。

在您当前拥有 ImageView 的布局中,创建一个relativelayout。在RelativeLayout 中,首先放置ImageView,然后放置ProgressBar。设置 ProgressBar 的 android:visibility 属性 = "invisible",当您希望它可见时,在代码中将其设置为可见: view.setVisibility(View.VISIBLE); (或.INVISIBLE)。

将RelativeLayout 的大小设置为wrap_content,并对ImageView 执行相同的操作。对于 ProgressBar,您可能希望硬编码一个小尺寸,例如 40dip 宽度和高度。如果您希望 ProgressBar 位于图像中间居中,请将其布局参数设置为“centerInParent”。

Your real question is "how can I have one View placed above another View". In your case, it seems you want an ImageView in the back and a ProgressBar in the front. (The ProgressBar name is a bit unintuitive because by default it's not a bar, it's a small spinning animation).

In your layout, where you currently have your ImageView, create a RelativeLayout. Within the RelativeLayout, first place your ImageView, and then place your ProgressBar. Set the ProgressBar's android:visibility attribute = "invisible", and when you want it to be seen, set it visible within your code: view.setVisibility(View.VISIBLE); (or .INVISIBLE).

Set the RelativeLayout's size to wrap_content, and do the same for the ImageView. For the ProgressBar, you may wish to hard code a small size, say 40dip width and also height. Set the layout parameters of the ProgressBar to "centerInParent" if you want it centered over the middle of the image.

王权女流氓 2024-11-24 02:36:12

我会考虑使用 ViewAnimator 在 ImageView 和 ProgressBar 之间切换小部件。

将 ViewAnimator 添加到您的 XML 布局中,然后在您的 Activity 中执行类似以下操作:

ViewAnimator switch = (ViewAnimator) findViewById(R.id.your_id);
ImageView image = new ImageView(this);
ProgressBar loading = new ProgressBar(this);

switch.addView(loading, 0, myLayoutParams);
switch.addView(image, 1, myLayoutParams);

其中 addView() 的第一个字段是您要添加的子视图,第二个字段是它在 Animator 中的索引,第三个字段是 LayoutParams 对象您已经定义了(我没有显示)。

我还没有测试代码,但应该会显示 Android ProgressBar 小部件。

然后,无论何时加载图像,请调用:

switch.showNext();

此外,此处的示例 如果您想查看的话,似乎与您需要的类似。

I would look into using a ViewAnimator to switch between your ImageView and a ProgressBar widget.

Add the ViewAnimator to your XML Layout, and then do something like this in your Activity:

ViewAnimator switch = (ViewAnimator) findViewById(R.id.your_id);
ImageView image = new ImageView(this);
ProgressBar loading = new ProgressBar(this);

switch.addView(loading, 0, myLayoutParams);
switch.addView(image, 1, myLayoutParams);

Where the first field of addView() is the Child View you are adding, the second is it's index in the Animator, and the third is a LayoutParams object that you have defined (which I didn't show).

I haven't tested the code, but that should hopefully display the Android ProgressBar widget.

Then, whenever your image has loaded, call:

switch.showNext();

Also, the example here seems to be similar to what you need if you want to check it out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文