Android:进度条作为占位符
我希望弄清楚如何在获取图像时使用进度条旋转器作为占位符。我放入了一个动画 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您真正的问题是“如何将一个视图放置在另一个视图之上”。在你的例子中,你似乎想要一个 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.
我会考虑使用 ViewAnimator 在 ImageView 和 ProgressBar 之间切换小部件。
将 ViewAnimator 添加到您的 XML 布局中,然后在您的 Activity 中执行类似以下操作:
其中 addView() 的第一个字段是您要添加的子视图,第二个字段是它在 Animator 中的索引,第三个字段是 LayoutParams 对象您已经定义了(我没有显示)。
我还没有测试代码,但应该会显示 Android ProgressBar 小部件。
然后,无论何时加载图像,请调用:
此外,此处的示例 如果您想查看的话,似乎与您需要的类似。
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:
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:
Also, the example here seems to be similar to what you need if you want to check it out.