android fragment 上的逐帧动画

标签 android animation android-fragments frame

如果我的 View 基于 Activity,我有一个动画效果很好,

但是如果我想基于 fragment ,我不知道该怎么做?

这里是代码[作为 Activity ]:

public class FramesAnimationActivity extends Activity {

    AnimationDrawable animation;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        animation = new AnimationDrawable();
        animation.addFrame(getResources().getDrawable(R.drawable.c_0), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c1), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c2), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c3), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c4), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c5), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c6), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c7), 100);


        animation.setOneShot(false);

        ImageView imageAnim =  (ImageView) findViewById(R.id.img);
        imageAnim.setBackgroundDrawable(animation);

        // run the start() method later on the UI thread
        imageAnim.post(new Starter());

    }

    class Starter implements Runnable {

        public void run() {
            animation.start();        
        }


    }

}

但是作为 Fragment,如何让它发挥作用呢?

public class HomeTab extends Fragment {
    /* (non-Javadoc)
     * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
     */

    AnimationDrawable animation;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            // We have different layouts, and in one of them this
            // fragment's containing frame doesn't exist.  The fragment
            // may still be created from its saved state, but there is
            // no reason to try to create its view hierarchy because it
            // won't be displayed.  Note this is not needed -- we could
            // just run the code below, where we would create and return
            // the view hierarchy; it would just never be used.
            return null;
        }


        animation = new AnimationDrawable();
        animation.addFrame(getResources().getDrawable(R.drawable.c_0), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c1), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c2), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c3), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c4), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c5), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c6), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.c7), 100); 

animation.setOneShot(false);

        ImageView imageAnim =  (ImageView) findViewById(R.id.img);
        imageAnim.setBackgroundDrawable(animation);

        // run the start() method later on the UI thread
        imageAnim.post(new Starter());

        return (LinearLayout)inflater.inflate(R.layout.tab_home_layout, container, false);
    }

    class Starter implements Runnable {

        public void run() {
            animation.start();        
        }


    }

}

所以我在 ImageView imageAnim = (ImageView) findViewById(R.id.img); 中得到一个错误 使用方法 findViewById(id) 未定义...

那么如何调整它以使其适用于我的 fragment ?

谢谢!

最佳答案

首先inflate你的LinearLayout,然后调用创建 View 的findViewById()方法:

    LinearLayout ll = (LinearLayout)inflater.inflate(R.layout.tab_home_layout, container, false);
    ImageView imageAnim =  (ImageView) ll.findViewById(R.id.img);
    imageAnim.setBackgroundDrawable(animation);

    // run the start() method later on the UI thread
    imageAnim.post(new Starter());

    return ll;

关于android fragment 上的逐帧动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10876468/

相关文章:

android - scrollTo 在带有 alpha 的 View 后面时不起作用

AndroidStudio 告诉我我的模拟器与我的项目不兼容,为什么,如果模拟器是 api10 而项目是 api3?

javascript - 如何在按下键时显示元素,然后如果再次按下,隐藏元素?

android - 如何在 android 中给 ListView 下拉刷新功能

android - 在 Android 的按钮上画一条线?

iphone - 有没有一种方法可以为 UITableViewCell 设置动画,以便该行短暂闪烁到选定状态,然后在选择时返回白色?

html - 使用 CSS 动画更改背景图像

android - 无论如何交换或重新排序 fragment BackStack 条目?

java - 显示或隐藏 fragment 菜单

java - 暂停GoogleMap SupportMapFragment/阻止其被控制