Android动画背景图自动播放的实现

我们在开发android应用的时候,经常会遇到类似从网络加载大图,在加载的过程中,在图片要显示的ImageView位置,先显示一个转圈的loading动画图,给用户的体验会更好一些,要实现这个动画图很简单,使用在/res/anim中定义xml的方式,通常使用…. 来实现。

不过大多数朋友都会遇到的问题是,动画是做好了,但是界面在加载的时候,动画并不会自动播放,还得通过屏幕点击等事件来触发,这就失去了意义了,实际上,android的动画AnimationDrawable 组件里面有个start()方法用于启动动画播放,但是这个方法不能直接写在onClick,onStart,onResume里面,写进去也是无效的,无法启动动画,只能写在比如事件监听当中,于是我们可以使用点小技巧来实现自动播放

目前我知道的有三种方法:

ImageView imageView = (ImageView)findViewById(R.id.xxx);

方法一:使用Runnalbe()来加载

imageView.setBackgroundResource(R.anim.xxxxx);
final AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();
imageView.post(new Runnable() {
    @
Override
        
public void run()  {
            
animationDrawable.start();
        
}
});

方法二:使用AsyncTask异步加载启动

imageView.setBackgroundResource(R.anim.xxxxx);
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
RunAnim runAnim=new RunAnim();
runAnim.execute("");
 
class RunAnim extends AsyncTask<String, String, String> {
        @
Override
        
protected String doInBackground(String... params) {
            
if (!animationDrawable.isRunning()) {
                
animationDrawable.stop();
                
animationDrawable.start();
            
}
            
return "";
        
}
}

方法三:通过添加addOnPreDrawListener来自动加载

imageView.setBackgroundResource(R.anim.xxxxx);
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
imageView.getViewTreeObserver().addOnPreDrawListener(preDrawListener);
 
OnPreDrawListener preDrawListener = new OnPreDrawListener(){
    @
Override
    
public boolean onPreDraw() {
        
animationDrawable.start();
        
return true; //必须要有这个true返回
    
}
};

以上三种方法经过测试没有问题,另外网上有一些说使用重写Activity的onWindowFocusChanged()方法来实现,但是还是有不足,得改变焦点才能触发,虽然理论可以自动实现改变焦点,感觉还是不甚可取。

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
No Responses
  1. minecraft says:

    Remarkable things here. I am very glad to see your
    article. Thank you so much and I am taking a look forward
    to contact you. Will you please drop me a e-mail?

  2. minecraft says:

    You ought to be a part of a contest for one of the finest sites online.
    I’m going to highly recommend this web site!

  3. Magnificent beat ! I wish to apprentice at the same time as you amend your
    website, how could i subscribe for a weblog website?
    The account helped me a applicable deal. I have been a little bit acquainted of this your broadcast offered shiny transparent concept

  4. I blog often and I really thank you for your content. The article
    has really peaked my interest. I will take
    a note of your blog and keep checking for
    new information about once a week. I opted in for your Feed as
    well.

  5. We’re a group of volunteers and starting a brand new
    scheme in our community. Your web site provided us with valuable
    info to work on. You’ve performed an impressive process and our whole neighborhood shall be grateful to you.

  6. I do trust all the ideas you’ve introduced in your post.
    They are very convincing and can certainly work. Nonetheless, the posts are very
    brief for newbies. May you please lengthen them a
    bit from subsequent time? Thank you for the post.

  7. Greetings from Colorado! I’m bored to tears at work so I decided to check out your blog on my iphone
    during lunch break. I really like the knowledge you present here and
    can’t wait to take a look when I get home. I’m amazed at how fast your blog loaded on my phone ..
    I’m not even using WIFI, just 3G .. Anyways, great site!

  8. What’s Taking place i’m new to this, I stumbled upon this I have found It positively useful and it has
    aided me out loads. I’m hoping to give a contribution & assist different users like its helped me.
    Good job.

  9. Please let me know if you’re looking for a article author for your blog.
    You have some really good posts and I think I would be a good asset.
    If you ever want to take some of the load off, I’d really like to write some material for your blog in exchange for a
    link back to mine. Please shoot me an email if interested.
    Thanks!

  10. Hello! I just wanted to ask if you ever have any trouble with
    hackers? My last blog (wordpress) was hacked and I ended up losing a few months of
    hard work due to no data backup. Do you have any solutions
    to stop hackers?

  11. I like what you guys are up too. This kind of clever work and
    reporting! Keep up the excellent works guys I’ve included you guys to my
    own blogroll.

  12. Quest Bars says:

    My spouse and I absolutely love your blog and find
    most of your post’s to be just what I’m looking for.
    Does one offer guest writers to write content available for
    you? I wouldn’t mind producing a post or elaborating on a lot of the
    subjects you write related to here. Again, awesome web log!

  13. Hello just wanted to give you a quick heads up and let you know a few of the pictures aren’t loading properly.
    I’m not sure why but I think its a linking issue. I’ve tried it in two different browsers and both show the same outcome.

  14. Pingback: 2019
  15. Sling TV says:

    Wow that was unusual. I just wrote an incredibly long comment but after I
    clicked submit my comment didn’t appear. Grrrr…
    well I’m not writing all that over again. Anyway, just wanted to say excellent
    blog!

  16. I every time used to study article in news papers but now as I am a user of net thus
    from now I am using net for articles or reviews, thanks to web.

  17. Hi there mates, how is the whole thing, and what you wish for to say regarding this paragraph, in my view
    its in fact awesome in support of me.

  18. Pingback: a2019-2020
  19. Pingback: facebook
  20. Pingback: facebook1
  21. Pingback: javsearch.mobi
  22. minecraft says:

    At this time I am going away to do my breakfast,
    later than having my breakfast coming again to
    read further news.

  23. tinyurl.com says:

    Howdy! Someone in my Facebook group shared this website with us so I came to take a look.
    I’m definitely loving the information. I’m book-marking and will be tweeting this to my
    followers! Exceptional blog and wonderful design.

  24. tinyurl.com says:

    My partner and I stumbled over here from a different web page and thought I might as well check things
    out. I like what I see so now i’m following you. Look forward
    to finding out about your web page repeatedly.

  25. minecraft says:

    I am sure this post has touched all the internet visitors, its really really pleasant
    article on building up new blog.

  26. minecraft says:

    Does your site have a contact page? I’m having a tough
    time locating it but, I’d like to send you an email.
    I’ve got some creative ideas for your blog you might be interested in hearing.
    Either way, great site and I look forward to seeing it
    develop over time.

  27. minecraft says:

    Does your blog have a contact page? I’m having problems locating it but, I’d like to shoot you an e-mail.
    I’ve got some suggestions for your blog you might be interested in hearing.
    Either way, great website and I look forward to seeing it grow over time.

  28. minecraft says:

    Fine way of describing, and pleasant post to obtain data regarding
    my presentation topic, which i am going to deliver in academy.

  29. tinyurl.com says:

    I’m gone to say to my little brother, that he should also visit this web site on regular basis
    to get updated from most recent gossip.

  30. tinyurl.com says:

    I every time spent my half an hour to read this webpage’s content every day along with a mug of coffee.

  31. minecraft says:

    As the admin of this web site is working, no uncertainty very
    soon it will be famous, due to its feature contents.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image