zoukankan      html  css  js  c++  java
  • 波浪效果的实现

    package com.loaderman.customviewdemo;
    
    import android.animation.ValueAnimator;
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.util.AttributeSet;
    import android.view.View;
    import android.view.animation.LinearInterpolator;
    
    
    public class AnimWaveView extends View {
        private Paint mPaint;
        private Path mPath;
        private int mItemWaveLength = 1200;
        private int dx;
    
        public AnimWaveView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mPath = new Path();
            mPaint = new Paint();
            mPaint.setColor(Color.GREEN);
            mPaint.setStyle(Paint.Style.FILL);
    
            ValueAnimator animator = ValueAnimator.ofInt(0, mItemWaveLength);
            animator.setDuration(3000);
            animator.setRepeatCount(ValueAnimator.INFINITE);
            animator.setInterpolator(new LinearInterpolator());
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                public void onAnimationUpdate(ValueAnimator animation) {
                    dx = (Integer) animation.getAnimatedValue();
                    postInvalidate();
                }
            });
            animator.start();
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawColor(Color.WHITE);
            mPath.reset();
            int originY = 300;
            int halfWaveLen = mItemWaveLength / 2;
            mPath.moveTo(-mItemWaveLength + dx, originY);
            for (int i = -mItemWaveLength; i <= getWidth() + mItemWaveLength; i += mItemWaveLength) {
                mPath.rQuadTo(halfWaveLen / 2, -100, halfWaveLen, 0);
                mPath.rQuadTo(halfWaveLen / 2, 100, halfWaveLen, 0);
            }
            mPath.lineTo(getWidth(), getHeight());
            mPath.lineTo(0, getHeight());
            mPath.close();
    
            canvas.drawPath(mPath, mPaint);
        }
    
    }
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <com.loaderman.customviewdemo.AnimWaveView
            android:layout_width="match_parent"
    
            android:layout_height="300dp" />
    
    
    </LinearLayout>

    效果

  • 相关阅读:
    [CF846E]Chemistry in Berland题解
    [CF846D]Monitor题解
    [CF846B]Math Show题解
    [CF846A]Curriculum Vitae题解
    斜率优化 学习笔记
    【CF115E】Linear Kingdom Races 题解(线段树优化DP)
    【洛谷P3802】小魔女帕琪 题解(概率期望)
    7月13日考试 题解(DFS序+期望+线段树优化建图)
    【BZOJ1426】收集邮票 题解 (期望)
    【HNOI2010】弹飞绵羊 题解(分块)
  • 原文地址:https://www.cnblogs.com/loaderman/p/10212505.html
Copyright © 2011-2022 走看看