How to display load progress animation for some im

2020-05-23 16:50发布

问题:

I would like to show progress bar (in form of rotating circle or something similar) when loading image from remote location.

I don't want standard ProgressBar that gets shown in the middle of the screen. I would like to have it in the middle of the imageView (or Layout that holds ImageView). Is there easy way to do this? For example, do I have option to attach progressBar to some View?

I was reading about FrameAnimation. I think I will do that, but first I want to make sure that I am not reinventing the wheel.

Thanks.

回答1:

Do you mean like a spinner ?

Yes you can do that:

heres a sample code:

<!--Grey Spinner-->
<ProgressBar
    android:id="@android:id/progress"
    style="?android:attr/progressBarStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>
<!--Black Spinner-->
<ProgressBar
    android:id="@android:id/progress"
    style="?android:attr/progressBarStyleSmallInverse"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>

Here are some other style attributes you can use, just replace above style attributes with one of these, e.g. style=?android:attr/<one of the attribute from below list>

    progressBarStyleSmallTitle
    progressBarStyleLarge
    progressBarStyleLargeInverse
    progressBarStyleHorizontal
    progressBarStyleSmallTitle

You must also note that if you are downloading image from internet do not use UIThread. And you can also add progress bar in the title bar.

Add spinner to title bar with following code(call request.. in onCreate()):

requestWindowFeature(Window.FEATURE_PROGRESS);
//calling setContentView() after requesting
setContentView(R.layout.main);
setProgressBarVisibility(true);
//call setProgressBarVisibility(false); to turn it off

Hope this helps. Cheers!



回答2:

you can attach a ProgressBar into ImageView layout holder,

RelativeLayout -> ImageView ->ProgressBar

then you can set layout align params to the progress bar to show it where you want.

you can do this in a xml layout and inflating later programatically, so, when you start/stop the image download process you can set the ProgressBar visibility on/off