Search in sources :

Example 1 with TimeAnimator

use of android.animation.TimeAnimator in project Android-Developers-Samples by johnjohndoe.

the class MainActivity method startPlayback.

public void startPlayback() {
    // Construct a URI that points to the video resource that we want to play
    Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.vid_bigbuckbunny);
    try {
        // BEGIN_INCLUDE(initialize_extractor)
        mExtractor.setDataSource(this, videoUri, null);
        int nTracks = mExtractor.getTrackCount();
        // any tracks that we haven't explicitly selected.
        for (int i = 0; i < nTracks; ++i) {
            mExtractor.unselectTrack(i);
        }
        // sample assumes that we just want to play the first one.
        for (int i = 0; i < nTracks; ++i) {
            // Try to create a video codec for this track. This call will return null if the
            // track is not a video track, or not a recognized video format. Once it returns
            // a valid MediaCodecWrapper, we can break out of the loop.
            mCodecWrapper = MediaCodecWrapper.fromVideoFormat(mExtractor.getTrackFormat(i), new Surface(mPlaybackView.getSurfaceTexture()));
            if (mCodecWrapper != null) {
                mExtractor.selectTrack(i);
                break;
            }
        }
        // END_INCLUDE(initialize_extractor)
        // By using a {@link TimeAnimator}, we can sync our media rendering commands with
        // the system display frame rendering. The animator ticks as the {@link Choreographer}
        // recieves VSYNC events.
        mTimeAnimator.setTimeListener(new TimeAnimator.TimeListener() {

            @Override
            public void onTimeUpdate(final TimeAnimator animation, final long totalTime, final long deltaTime) {
                boolean isEos = ((mExtractor.getSampleFlags() & MediaCodec.BUFFER_FLAG_END_OF_STREAM) == MediaCodec.BUFFER_FLAG_END_OF_STREAM);
                // BEGIN_INCLUDE(write_sample)
                if (!isEos) {
                    // Try to submit the sample to the codec and if successful advance the
                    // extractor to the next available sample to read.
                    boolean result = mCodecWrapper.writeSample(mExtractor, false, mExtractor.getSampleTime(), mExtractor.getSampleFlags());
                    if (result) {
                        // Advancing the extractor is a blocking operation and it MUST be
                        // executed outside the main thread in real applications.
                        mExtractor.advance();
                    }
                }
                // END_INCLUDE(write_sample)
                // Examine the sample at the head of the queue to see if its ready to be
                // rendered and is not zero sized End-of-Stream record.
                MediaCodec.BufferInfo out_bufferInfo = new MediaCodec.BufferInfo();
                mCodecWrapper.peekSample(out_bufferInfo);
                // BEGIN_INCLUDE(render_sample)
                if (out_bufferInfo.size <= 0 && isEos) {
                    mTimeAnimator.end();
                    mCodecWrapper.stopAndRelease();
                    mExtractor.release();
                } else if (out_bufferInfo.presentationTimeUs / 1000 < totalTime) {
                    // Pop the sample off the queue and send it to {@link Surface}
                    mCodecWrapper.popSample(true);
                }
            // END_INCLUDE(render_sample)
            }
        });
        // We're all set. Kick off the animator to process buffers and render video frames as
        // they become available
        mTimeAnimator.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : TimeAnimator(android.animation.TimeAnimator) MediaCodec(android.media.MediaCodec) IOException(java.io.IOException) Uri(android.net.Uri) Surface(android.view.Surface)

Example 2 with TimeAnimator

use of android.animation.TimeAnimator in project chromeview by pwnall.

the class SmoothScroller method createJBRunnable.

private Runnable createJBRunnable() {
    // On JB, we rely on TimeAnimator to send events tied with vsync.
    return new Runnable() {

        @Override
        public void run() {
            mTimeAnimator = new TimeAnimator();
            mTimeAnimator.setTimeListener(new TimeListener() {

                @Override
                public void onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime) {
                    if (!sendEvent(mDownTime + totalTime)) {
                        mTimeAnimator.end();
                    }
                }
            });
            mTimeAnimator.start();
        }
    };
}
Also used : TimeListener(android.animation.TimeAnimator.TimeListener) TimeAnimator(android.animation.TimeAnimator)

Example 3 with TimeAnimator

use of android.animation.TimeAnimator in project android_frameworks_base by DirtyUnicorns.

the class MLand method reset.

public void reset() {
    L("reset");
    final Drawable sky = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, SKIES[mTimeOfDay]);
    sky.setDither(true);
    setBackground(sky);
    mFlipped = frand() > 0.5f;
    setScaleX(mFlipped ? -1 : 1);
    int i = getChildCount();
    while (i-- > 0) {
        final View v = getChildAt(i);
        if (v instanceof GameView) {
            removeViewAt(i);
        }
    }
    mObstaclesInPlay.clear();
    mCurrentPipeId = 0;
    mWidth = getWidth();
    mHeight = getHeight();
    boolean showingSun = (mTimeOfDay == DAY || mTimeOfDay == SUNSET) && frand() > 0.25;
    if (showingSun) {
        final Star sun = new Star(getContext());
        sun.setBackgroundResource(R.drawable.sun);
        final int w = getResources().getDimensionPixelSize(R.dimen.sun_size);
        sun.setTranslationX(frand(w, mWidth - w));
        if (mTimeOfDay == DAY) {
            sun.setTranslationY(frand(w, (mHeight * 0.66f)));
            sun.getBackground().setTint(0);
        } else {
            sun.setTranslationY(frand(mHeight * 0.66f, mHeight - w));
            sun.getBackground().setTintMode(PorterDuff.Mode.SRC_ATOP);
            sun.getBackground().setTint(0xC0FF8000);
        }
        addView(sun, new LayoutParams(w, w));
    }
    if (!showingSun) {
        final boolean dark = mTimeOfDay == NIGHT || mTimeOfDay == TWILIGHT;
        final float ff = frand();
        if ((dark && ff < 0.75f) || ff < 0.5f) {
            final Star moon = new Star(getContext());
            moon.setBackgroundResource(R.drawable.moon);
            moon.getBackground().setAlpha(dark ? 255 : 128);
            moon.setScaleX(frand() > 0.5 ? -1 : 1);
            moon.setRotation(moon.getScaleX() * frand(5, 30));
            final int w = getResources().getDimensionPixelSize(R.dimen.sun_size);
            moon.setTranslationX(frand(w, mWidth - w));
            moon.setTranslationY(frand(w, mHeight - w));
            addView(moon, new LayoutParams(w, w));
        }
    }
    final int mh = mHeight / 6;
    final boolean cloudless = frand() < 0.25;
    final int N = 20;
    for (i = 0; i < N; i++) {
        final float r1 = frand();
        final Scenery s;
        if (HAVE_STARS && r1 < 0.3 && mTimeOfDay != DAY) {
            s = new Star(getContext());
        } else if (r1 < 0.6 && !cloudless) {
            s = new Cloud(getContext());
        } else {
            switch(mScene) {
                case SCENE_ZRH:
                    s = new Mountain(getContext());
                    break;
                case SCENE_TX:
                    s = new Cactus(getContext());
                    break;
                case SCENE_CITY:
                default:
                    s = new Building(getContext());
                    break;
            }
            s.z = (float) i / N;
            // no more shadows for these things
            //s.setTranslationZ(PARAMS.SCENERY_Z * (1+s.z));
            // buildings move proportional to their distance
            s.v = 0.85f * s.z;
            if (mScene == SCENE_CITY) {
                s.setBackgroundColor(Color.GRAY);
                s.h = irand(PARAMS.BUILDING_HEIGHT_MIN, mh);
            }
            final int c = (int) (255f * s.z);
            final Drawable bg = s.getBackground();
            if (bg != null)
                bg.setColorFilter(Color.rgb(c, c, c), PorterDuff.Mode.MULTIPLY);
        }
        final LayoutParams lp = new LayoutParams(s.w, s.h);
        if (s instanceof Building) {
            lp.gravity = Gravity.BOTTOM;
        } else {
            lp.gravity = Gravity.TOP;
            final float r = frand();
            if (s instanceof Star) {
                lp.topMargin = (int) (r * r * mHeight);
            } else {
                lp.topMargin = (int) (1 - r * r * mHeight / 2) + mHeight / 2;
            }
        }
        addView(s, lp);
        s.setTranslationX(frand(-lp.width, mWidth + lp.width));
    }
    for (Player p : mPlayers) {
        // put it back!
        addView(p);
        p.reset();
    }
    realignPlayers();
    if (mAnim != null) {
        mAnim.cancel();
    }
    mAnim = new TimeAnimator();
    mAnim.setTimeListener(new TimeAnimator.TimeListener() {

        @Override
        public void onTimeUpdate(TimeAnimator timeAnimator, long t, long dt) {
            step(t, dt);
        }
    });
}
Also used : TimeAnimator(android.animation.TimeAnimator) Drawable(android.graphics.drawable.Drawable) GradientDrawable(android.graphics.drawable.GradientDrawable) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) GradientDrawable(android.graphics.drawable.GradientDrawable) Paint(android.graphics.Paint)

Example 4 with TimeAnimator

use of android.animation.TimeAnimator in project android_frameworks_base by AOSPA.

the class MLand method reset.

public void reset() {
    L("reset");
    final Drawable sky = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, SKIES[mTimeOfDay]);
    sky.setDither(true);
    setBackground(sky);
    mFlipped = frand() > 0.5f;
    setScaleX(mFlipped ? -1 : 1);
    int i = getChildCount();
    while (i-- > 0) {
        final View v = getChildAt(i);
        if (v instanceof GameView) {
            removeViewAt(i);
        }
    }
    mObstaclesInPlay.clear();
    mCurrentPipeId = 0;
    mWidth = getWidth();
    mHeight = getHeight();
    boolean showingSun = (mTimeOfDay == DAY || mTimeOfDay == SUNSET) && frand() > 0.25;
    if (showingSun) {
        final Star sun = new Star(getContext());
        sun.setBackgroundResource(R.drawable.sun);
        final int w = getResources().getDimensionPixelSize(R.dimen.sun_size);
        sun.setTranslationX(frand(w, mWidth - w));
        if (mTimeOfDay == DAY) {
            sun.setTranslationY(frand(w, (mHeight * 0.66f)));
            sun.getBackground().setTint(0);
        } else {
            sun.setTranslationY(frand(mHeight * 0.66f, mHeight - w));
            sun.getBackground().setTintMode(PorterDuff.Mode.SRC_ATOP);
            sun.getBackground().setTint(0xC0FF8000);
        }
        addView(sun, new LayoutParams(w, w));
    }
    if (!showingSun) {
        final boolean dark = mTimeOfDay == NIGHT || mTimeOfDay == TWILIGHT;
        final float ff = frand();
        if ((dark && ff < 0.75f) || ff < 0.5f) {
            final Star moon = new Star(getContext());
            moon.setBackgroundResource(R.drawable.moon);
            moon.getBackground().setAlpha(dark ? 255 : 128);
            moon.setScaleX(frand() > 0.5 ? -1 : 1);
            moon.setRotation(moon.getScaleX() * frand(5, 30));
            final int w = getResources().getDimensionPixelSize(R.dimen.sun_size);
            moon.setTranslationX(frand(w, mWidth - w));
            moon.setTranslationY(frand(w, mHeight - w));
            addView(moon, new LayoutParams(w, w));
        }
    }
    final int mh = mHeight / 6;
    final boolean cloudless = frand() < 0.25;
    final int N = 20;
    for (i = 0; i < N; i++) {
        final float r1 = frand();
        final Scenery s;
        if (HAVE_STARS && r1 < 0.3 && mTimeOfDay != DAY) {
            s = new Star(getContext());
        } else if (r1 < 0.6 && !cloudless) {
            s = new Cloud(getContext());
        } else {
            switch(mScene) {
                case SCENE_ZRH:
                    s = new Mountain(getContext());
                    break;
                case SCENE_TX:
                    s = new Cactus(getContext());
                    break;
                case SCENE_CITY:
                default:
                    s = new Building(getContext());
                    break;
            }
            s.z = (float) i / N;
            // no more shadows for these things
            //s.setTranslationZ(PARAMS.SCENERY_Z * (1+s.z));
            // buildings move proportional to their distance
            s.v = 0.85f * s.z;
            if (mScene == SCENE_CITY) {
                s.setBackgroundColor(Color.GRAY);
                s.h = irand(PARAMS.BUILDING_HEIGHT_MIN, mh);
            }
            final int c = (int) (255f * s.z);
            final Drawable bg = s.getBackground();
            if (bg != null)
                bg.setColorFilter(Color.rgb(c, c, c), PorterDuff.Mode.MULTIPLY);
        }
        final LayoutParams lp = new LayoutParams(s.w, s.h);
        if (s instanceof Building) {
            lp.gravity = Gravity.BOTTOM;
        } else {
            lp.gravity = Gravity.TOP;
            final float r = frand();
            if (s instanceof Star) {
                lp.topMargin = (int) (r * r * mHeight);
            } else {
                lp.topMargin = (int) (1 - r * r * mHeight / 2) + mHeight / 2;
            }
        }
        addView(s, lp);
        s.setTranslationX(frand(-lp.width, mWidth + lp.width));
    }
    for (Player p : mPlayers) {
        // put it back!
        addView(p);
        p.reset();
    }
    realignPlayers();
    if (mAnim != null) {
        mAnim.cancel();
    }
    mAnim = new TimeAnimator();
    mAnim.setTimeListener(new TimeAnimator.TimeListener() {

        @Override
        public void onTimeUpdate(TimeAnimator timeAnimator, long t, long dt) {
            step(t, dt);
        }
    });
}
Also used : TimeAnimator(android.animation.TimeAnimator) Drawable(android.graphics.drawable.Drawable) GradientDrawable(android.graphics.drawable.GradientDrawable) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) GradientDrawable(android.graphics.drawable.GradientDrawable) Paint(android.graphics.Paint)

Example 5 with TimeAnimator

use of android.animation.TimeAnimator in project android_frameworks_base by ResurrectionRemix.

the class MLand method reset.

public void reset() {
    L("reset");
    final Drawable sky = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, SKIES[mTimeOfDay]);
    sky.setDither(true);
    setBackground(sky);
    mFlipped = frand() > 0.5f;
    setScaleX(mFlipped ? -1 : 1);
    int i = getChildCount();
    while (i-- > 0) {
        final View v = getChildAt(i);
        if (v instanceof GameView) {
            removeViewAt(i);
        }
    }
    mObstaclesInPlay.clear();
    mCurrentPipeId = 0;
    mWidth = getWidth();
    mHeight = getHeight();
    boolean showingSun = (mTimeOfDay == DAY || mTimeOfDay == SUNSET) && frand() > 0.25;
    if (showingSun) {
        final Star sun = new Star(getContext());
        sun.setBackgroundResource(R.drawable.sun);
        final int w = getResources().getDimensionPixelSize(R.dimen.sun_size);
        sun.setTranslationX(frand(w, mWidth - w));
        if (mTimeOfDay == DAY) {
            sun.setTranslationY(frand(w, (mHeight * 0.66f)));
            sun.getBackground().setTint(0);
        } else {
            sun.setTranslationY(frand(mHeight * 0.66f, mHeight - w));
            sun.getBackground().setTintMode(PorterDuff.Mode.SRC_ATOP);
            sun.getBackground().setTint(0xC0FF8000);
        }
        addView(sun, new LayoutParams(w, w));
    }
    if (!showingSun) {
        final boolean dark = mTimeOfDay == NIGHT || mTimeOfDay == TWILIGHT;
        final float ff = frand();
        if ((dark && ff < 0.75f) || ff < 0.5f) {
            final Star moon = new Star(getContext());
            moon.setBackgroundResource(R.drawable.moon);
            moon.getBackground().setAlpha(dark ? 255 : 128);
            moon.setScaleX(frand() > 0.5 ? -1 : 1);
            moon.setRotation(moon.getScaleX() * frand(5, 30));
            final int w = getResources().getDimensionPixelSize(R.dimen.sun_size);
            moon.setTranslationX(frand(w, mWidth - w));
            moon.setTranslationY(frand(w, mHeight - w));
            addView(moon, new LayoutParams(w, w));
        }
    }
    final int mh = mHeight / 6;
    final boolean cloudless = frand() < 0.25;
    final int N = 20;
    for (i = 0; i < N; i++) {
        final float r1 = frand();
        final Scenery s;
        if (HAVE_STARS && r1 < 0.3 && mTimeOfDay != DAY) {
            s = new Star(getContext());
        } else if (r1 < 0.6 && !cloudless) {
            s = new Cloud(getContext());
        } else {
            switch(mScene) {
                case SCENE_ZRH:
                    s = new Mountain(getContext());
                    break;
                case SCENE_TX:
                    s = new Cactus(getContext());
                    break;
                case SCENE_CITY:
                default:
                    s = new Building(getContext());
                    break;
            }
            s.z = (float) i / N;
            // no more shadows for these things
            //s.setTranslationZ(PARAMS.SCENERY_Z * (1+s.z));
            // buildings move proportional to their distance
            s.v = 0.85f * s.z;
            if (mScene == SCENE_CITY) {
                s.setBackgroundColor(Color.GRAY);
                s.h = irand(PARAMS.BUILDING_HEIGHT_MIN, mh);
            }
            final int c = (int) (255f * s.z);
            final Drawable bg = s.getBackground();
            if (bg != null)
                bg.setColorFilter(Color.rgb(c, c, c), PorterDuff.Mode.MULTIPLY);
        }
        final LayoutParams lp = new LayoutParams(s.w, s.h);
        if (s instanceof Building) {
            lp.gravity = Gravity.BOTTOM;
        } else {
            lp.gravity = Gravity.TOP;
            final float r = frand();
            if (s instanceof Star) {
                lp.topMargin = (int) (r * r * mHeight);
            } else {
                lp.topMargin = (int) (1 - r * r * mHeight / 2) + mHeight / 2;
            }
        }
        addView(s, lp);
        s.setTranslationX(frand(-lp.width, mWidth + lp.width));
    }
    for (Player p : mPlayers) {
        // put it back!
        addView(p);
        p.reset();
    }
    realignPlayers();
    if (mAnim != null) {
        mAnim.cancel();
    }
    mAnim = new TimeAnimator();
    mAnim.setTimeListener(new TimeAnimator.TimeListener() {

        @Override
        public void onTimeUpdate(TimeAnimator timeAnimator, long t, long dt) {
            step(t, dt);
        }
    });
}
Also used : TimeAnimator(android.animation.TimeAnimator) Drawable(android.graphics.drawable.Drawable) GradientDrawable(android.graphics.drawable.GradientDrawable) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) GradientDrawable(android.graphics.drawable.GradientDrawable) Paint(android.graphics.Paint)

Aggregations

TimeAnimator (android.animation.TimeAnimator)8 Paint (android.graphics.Paint)5 Drawable (android.graphics.drawable.Drawable)5 GradientDrawable (android.graphics.drawable.GradientDrawable)5 View (android.view.View)5 ImageView (android.widget.ImageView)5 TextView (android.widget.TextView)5 TimeListener (android.animation.TimeAnimator.TimeListener)1 MediaCodec (android.media.MediaCodec)1 Uri (android.net.Uri)1 Surface (android.view.Surface)1 IOException (java.io.IOException)1