Android Open Source - misound Gif View






From Project

Back to project page misound.

License

The source code is released under:

Apache License

If you think the Android project misound listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.xiaomi.mitv.widget;
/*  w  ww. ja va 2  s. c om*/
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import com.xiaomi.mitv.soundbarapp.R;

/**
 * Created by chenxuetong on 9/10/14.
 */
public class GifView extends View {
    private static final int DEFAULT_MOVIE_DURATION = 1000;
    private int mMovieResourceId;
    private Movie mMovie;
    private long mMovieStart;
    private int mCurrentAnimationTime = 0;
    private float mLeft;
    private float mTop;
    private float mScale;
    private int mMeasuredMovieWidth;
    private int mMeasuredMovieHeight;
    private boolean mVisible = true;
    private volatile boolean mPaused = false;

    public GifView(Context context) {
        this(context, null);
    }

    public GifView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GifView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setViewAttributes(context, attrs, defStyle);
    }

    @SuppressLint("NewApi")
    private void setViewAttributes(Context context, AttributeSet attrs,
                                   int defStyle) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
        final TypedArray array = context.obtainStyledAttributes(attrs,
                R.styleable.GifView);
        mMovieResourceId = array.getResourceId(R.styleable.GifView_gif, -1);
        mPaused = false;
        array.recycle();
        if (mMovieResourceId != -1) {
            mMovie = Movie.decodeStream(getResources().openRawResource(
                    mMovieResourceId));
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mMovie != null) {
            int movieWidth = mMovie.width();
            int movieHeight = mMovie.height();
            int maximumWidth = MeasureSpec.getSize(widthMeasureSpec);
            float scaleW = (float) movieWidth / (float) maximumWidth;
            mScale = 1f / scaleW;
            mMeasuredMovieWidth = maximumWidth;
            mMeasuredMovieHeight = (int) (movieHeight * mScale);
            setMeasuredDimension(mMeasuredMovieWidth, mMeasuredMovieHeight);
        } else {
            setMeasuredDimension(getSuggestedMinimumWidth(),
                    getSuggestedMinimumHeight());
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        mLeft = (getWidth() - mMeasuredMovieWidth) / 2f;
        mTop = (getHeight() - mMeasuredMovieHeight) / 2f;
        mVisible = getVisibility() == View.VISIBLE;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mMovie != null) {
            if (!mPaused) {
                updateAnimationTime();
                drawMovieFrame(canvas);
                invalidateView();
            } else {
                drawMovieFrame(canvas);
            }
        }
    }

    @SuppressLint("NewApi")
    private void invalidateView() {
        if (mVisible) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                postInvalidateOnAnimation();
            } else {
                invalidate();
            }
        }
    }

    private void updateAnimationTime() {
        long now = android.os.SystemClock.uptimeMillis();
        if (mMovieStart == 0) {
            mMovieStart = now;
        }
        int dur = mMovie.duration();
        if (dur == 0) {
            dur = DEFAULT_MOVIE_DURATION;
        }
        mCurrentAnimationTime = (int) ((now - mMovieStart) % dur);
    }

    private void drawMovieFrame(Canvas canvas) {
        mMovie.setTime(mCurrentAnimationTime);
        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.scale(mScale, mScale);
        mMovie.draw(canvas, mLeft / mScale, mTop / mScale);
        canvas.restore();
    }

    @SuppressLint("NewApi")
    @Override
    public void onScreenStateChanged(int screenState) {
        super.onScreenStateChanged(screenState);
        mVisible = screenState == SCREEN_STATE_ON;
        invalidateView();
    }

    @SuppressLint("NewApi")
    @Override
    protected void onVisibilityChanged(View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        mVisible = visibility == View.VISIBLE;
        invalidateView();
    }

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
        mVisible = visibility == View.VISIBLE;
        invalidateView();
    }

}




Java Source Code List

com.xiaomi.mitv.soundbar.DefaultMisoundDevice.java
com.xiaomi.mitv.soundbarapp.BarInfoUtils.java
com.xiaomi.mitv.soundbarapp.ConnectingActivity.java
com.xiaomi.mitv.soundbarapp.MainActivity2.java
com.xiaomi.mitv.soundbarapp.SoundBarApplication.java
com.xiaomi.mitv.soundbarapp.TestActivity.java
com.xiaomi.mitv.soundbarapp.TypefaceManager.java
com.xiaomi.mitv.soundbarapp.WrapperActivity.java
com.xiaomi.mitv.soundbarapp.diagnosis.DiagnosisFragment.java
com.xiaomi.mitv.soundbarapp.diagnosis.Engine.java
com.xiaomi.mitv.soundbarapp.diagnosis.ViewWrapper.java
com.xiaomi.mitv.soundbarapp.diagnosis.data.Entry.java
com.xiaomi.mitv.soundbarapp.diagnosis.data.Node.java
com.xiaomi.mitv.soundbarapp.diagnosis.data.OrderedList.java
com.xiaomi.mitv.soundbarapp.diagnosis.data.QAElement.java
com.xiaomi.mitv.soundbarapp.eq.EQManager.java
com.xiaomi.mitv.soundbarapp.eq.EQSettingsFragment.java
com.xiaomi.mitv.soundbarapp.eq.EQStyleResource.java
com.xiaomi.mitv.soundbarapp.eq.EQStyle.java
com.xiaomi.mitv.soundbarapp.eq.UserEQControlFragment.java
com.xiaomi.mitv.soundbarapp.faq.FaqDataStore.java
com.xiaomi.mitv.soundbarapp.faq.FaqFragment.java
com.xiaomi.mitv.soundbarapp.fragment.A2dpDlg.java
com.xiaomi.mitv.soundbarapp.fragment.BaseFragment.java
com.xiaomi.mitv.soundbarapp.fragment.FeedbackFragment.java
com.xiaomi.mitv.soundbarapp.fragment.MainEntryFragment.java
com.xiaomi.mitv.soundbarapp.fragment.PanelFragment.java
com.xiaomi.mitv.soundbarapp.fragment.SettingsFragment.java
com.xiaomi.mitv.soundbarapp.player.MediaButtonIntentReceiver.java
com.xiaomi.mitv.soundbarapp.player.MusicUtils.java
com.xiaomi.mitv.soundbarapp.player.PlayListFragment.java
com.xiaomi.mitv.soundbarapp.player.PlayerFragment.java
com.xiaomi.mitv.soundbarapp.player.PlayerService.java
com.xiaomi.mitv.soundbarapp.player.Player.java
com.xiaomi.mitv.soundbarapp.provider.DataProvider.java
com.xiaomi.mitv.soundbarapp.upgrade.FirmwareManager.java
com.xiaomi.mitv.soundbarapp.upgrade.FirmwareZipFile.java
com.xiaomi.mitv.soundbarapp.upgrade.UpdateActivity.java
com.xiaomi.mitv.soundbarapp.upgrade.UpgradeFragment.java
com.xiaomi.mitv.soundbarapp.upgrade.VersionSelectFragment.java
com.xiaomi.mitv.soundbarapp.util.ConfirmActivityDlg.java
com.xiaomi.mitv.soundbarapp.util.Worker.java
com.xiaomi.mitv.widget.GainView.java
com.xiaomi.mitv.widget.GifView.java
com.xiaomi.mitv.widget.LetterIndexSilderBar.java
com.xiaomi.mitv.widget.MiSwitch.java
com.xiaomi.mitv.widget.RoundAnimationView.java
com.xiaomi.mitv.widget.RoundProgressBar.java
com.xiaomi.mitv.widget.RoundSeekBar.java