自定义跑马灯背景

居中的图片:
在这里插入图片描述

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;


public class MinionView extends View {
    private static final String TAG = "MarqueeAdView";

    private Paint mDstPaint;
    private Path mRoundPath;
    private Path mDstPath;
    private PathMeasure mPathMeasure;

    private float mLength;
    private int mViewWidth;
    private int mViewHeight;
    private int mLineWidth;
    private RectF mOvalRectF;

    private float mStart;
    private float mStop;

    private ValueAnimator mAnim;

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

    public MinionView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setVisibility(View.GONE);
        init();
    }

    private void init() {
        mDstPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mDstPaint.setStyle(Paint.Style.FILL);
        mRoundPath = new Path();
        mDstPath = new Path();
        mPathMeasure = new PathMeasure();
        mLineWidth = dp2px(20);
        mDstPaint.setStrokeWidth(dp2px(100));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (getVisibility() != View.VISIBLE) {
            return;
        }

        mDstPath.reset();
        if (mViewWidth == 0) {
            mViewWidth = getWidth();
            mViewHeight = getHeight();
            mOvalRectF = new RectF(mLineWidth / 2, mLineWidth / 2,
                    mViewWidth - mLineWidth / 2, mViewHeight - mLineWidth / 2);
            mRoundPath.addRoundRect(mOvalRectF, 10, 10, Path.Direction.CW);
            mPathMeasure.setPath(mRoundPath, true);
            mLength = mPathMeasure.getLength();
            startAnim();
        } else {
            mDstPaint.setShader(getShader());
            canvas.drawRoundRect(mOvalRectF, 10, 10, mDstPaint);//第二个参数是x半径,第三个参数是y半径
        }
    }


    /**
     * 渐变色
     *
     * @return
     */
    private Shader getShader() {
        Shader shader = new LinearGradient(mStart, mStart, mStop, mStart,
                new int[]{Color.parseColor("#FFA1C8FF"),
                        Color.parseColor("#FFB660FF"),
                        Color.parseColor("#FFFF67A0"),
                        Color.parseColor("#FFFFA768"),
                        Color.parseColor("#FFFFFB51")
                },
                null, Shader.TileMode.MIRROR);
        return shader;
    }

    /**
     * 开始动画
     */
    private void startAnim() {
        mAnim = ValueAnimator.ofFloat(0, mLength * 1f);
        mAnim.setDuration(2500);
        mAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                mStart = value;
                mStop = mStart + mLength *0.5f;
                postInvalidate();
            }
        });
        mAnim.setRepeatCount(ValueAnimator.INFINITE);
        mAnim.setRepeatMode(ValueAnimator.RESTART);
        mAnim.setInterpolator(new LinearInterpolator());
        mAnim.start();

    }

    /**
     * 停止动画
     */
    public void destroy() {
        if (mAnim != null) {
            mAnim.pause();
            setVisibility(View.GONE);
        }
    }

    /**
     * 显示
     */
    public void show() {
        if (mAnim != null && mAnim.isPaused())
            mAnim.start();

        setVisibility(View.VISIBLE);
    }


    public int dp2px(float dip) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
    }


}

使用方法:

MinionView  minionView = new MinionView(MainActivity.this);
container.addView(minionView);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(600 300);
minionView.setLayoutParams(lp);
minionView.show();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_41620230

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值