Android Open Source - misound Round Animation 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;
/*from  w  w w. j ava  2 s .co  m*/
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.*;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import com.xiaomi.mitv.soundbarapp.R;

/**
 * Created by chenxuetong on 8/18/14.
 */
public class RoundAnimationView extends View {
    private static final String TAG = "miwidget";
    private int mStepGap;
    private int mBeginTransparent;
    private int mEndTransparent;
    private int mColor;
    private int mSteps;
    private Drawable mImage;
    private boolean mAnimationStopped = true;

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

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

    public RoundAnimationView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.RoundAnimationView);
        mColor = typedArray.getColor(R.styleable.RoundAnimationView_color, Color.WHITE);
        mStepGap = typedArray.getInt(R.styleable.RoundAnimationView_stepGap, 30);
        mBeginTransparent = typedArray.getInt(R.styleable.RoundAnimationView_beginTransparent, 100);
        mEndTransparent = typedArray.getInt(R.styleable.RoundAnimationView_endTransparent, 20);
        mImage = typedArray.getDrawable(R.styleable.RoundAnimationView_img);
//        mImageWidth = (int)typedArray.getDimension(R.styleable.RoundAnimationView_imgWidth, 48);
//        mImageHeight = (int)typedArray.getDimension(R.styleable.RoundAnimationView_imgHeight, 48);
        typedArray.recycle();
        mSteps = -1;
    }

    public void setImage(int rid){
        Drawable d = getContext().getResources().getDrawable(rid);
        if(d != null){
            mImage = d;
        }
        postInvalidate();
    }

    public void startAnimation(){
        mAnimationStopped = false;
    }

    public void stopAnimation(){
        mAnimationStopped = true;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        doAnimation();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int sc = canvas.save();

        if(!mAnimationStopped) {
            int w = getDrawWidth();
            int h = getDrawHeight();

            Paint paint = new Paint();
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(2);
            paint.setAntiAlias(true);
            canvas.translate(getWidth() - w, getHeight() - h);
            for (int step = 0; step <= mSteps; step++) {
                int radian = makeRadianForStep(step);
                paint.setColor(makeColor(step));
                canvas.drawCircle(getCenterX(), getCenterY(), radian, paint);
            }
        }

        //draw center icon
        if(mImage != null){
            int imgW = mImage.getIntrinsicWidth();
            int imgH = mImage.getIntrinsicHeight();
            canvas.translate(getCenterX()-imgW/2, getCenterY()-imgH/2);
            mImage.setBounds(new Rect(0,0,imgW, imgH));
            mImage.draw(canvas);
        }

        canvas.restoreToCount(sc);
    }

    private void doAnimation(){
        int radian = makeRadianForStep(mSteps);
        if(radian>getMaxRadian()/2){
            mSteps = -1;
        }else {
            mSteps++;
        }
        invalidate();
        postDelayed(new Runnable() {
            @Override
            public void run() {
                doAnimation();
            }
        }, 400);
    }

    private int makeColor(int step){
        int steps = getMaxRadian()/4/ mStepGap;
        int alpha = 255 * (Math.max(mBeginTransparent, mEndTransparent) - step * Math.abs(mBeginTransparent-mEndTransparent)/steps)/100;
        if(alpha <= mEndTransparent) alpha=mEndTransparent;
        return Color.argb(alpha, Color.red(mColor), Color.green(mColor), Color.blue(mColor));
    }

    private int getCenterX(){
        return getWidth()/2;
    }
    private int getCenterY(){
        return getHeight()/2;
    }

    private int makeRadianForStep(int step){
        int imgW = mImage.getIntrinsicWidth();
        int imgH = mImage.getIntrinsicHeight();
        int baseRadian = (int)Math.sqrt(Math.pow(imgH/2, 2) + Math.pow(imgW/2, 2))+5;
        return (int)(mStepGap*step*Math.pow(1.1,step)) + baseRadian;
    }

    private int getMaxRadian(){
        return (int)(getDrawWidth()*Math.sqrt(2));
    }

    private int getDrawWidth(){
        return getWidth() - getPaddingLeft() - getPaddingRight();
    }

    private int getDrawHeight(){
        return getHeight() - getPaddingTop() - getPaddingBottom();
    }
}




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