自定义View 波浪

xiaoxiao2021-02-28  10

public class CustomView extends View{ //路径和画笔 private Path mPath; private Paint mPaint; //记录屏幕的宽高 private int mScreenHeight; private int mScreenWidth; private int mOffset; public CustomView(Context context) { super(context); init(context); } public CustomView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(context); } public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } private void init(Context context) { WindowManager win= (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics displayMetrics = new DisplayMetrics(); win.getDefaultDisplay().getMetrics(displayMetrics); mScreenHeight = displayMetrics.heightPixels; mScreenWidth=displayMetrics.widthPixels; mPath=new Path(); mPaint=new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setColor(Color.BLUE); mPaint.setStyle(Paint.Style.FILL); mPaint.setStrokeWidth(8); setViewanimator(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPath.reset(); mPath.moveTo(-mScreenWidth+mOffset,mScreenHeight/2); for (int i=0;i<2;i++){ mPath.quadTo(-mScreenWidth*3/4+(mScreenWidth*i)+mOffset, mScreenHeight / 2 - 100, -mScreenWidth / 2 + (mScreenWidth * i) + mOffset, mScreenHeight / 2); mPath.quadTo(-mScreenWidth / 4 + (mScreenWidth * i) + mOffset, mScreenHeight / 2 + 100, +(mScreenWidth * i) + mOffset, mScreenHeight / 2); } mPath.lineTo(mScreenWidth,mScreenHeight); mPath.lineTo(0,mScreenHeight); canvas.drawPath(mPath,mPaint); } private void setViewanimator() { ValueAnimator valueAnimator = ValueAnimator.ofInt(0, mScreenWidth); valueAnimator.setDuration(1200); valueAnimator.setRepeatCount(ValueAnimator.INFINITE); valueAnimator.setInterpolator(new LinearInterpolator()); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { mOffset= (int) valueAnimator.getAnimatedValue(); invalidate(); } }); valueAnimator.start();
转载请注明原文地址: https://www.6miu.com/read-2050059.html

最新回复(0)