居中的图片:
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();