溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

如何使用Android實(shí)現(xiàn)簡(jiǎn)單水波紋效果

發(fā)布時(shí)間:2021-04-16 14:39:55 來(lái)源:億速云 閱讀:244 作者:小新 欄目:移動(dòng)開(kāi)發(fā)

這篇文章主要介紹了如何使用Android實(shí)現(xiàn)簡(jiǎn)單水波紋效果,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

具體內(nèi)容如下

一、效果

如何使用Android實(shí)現(xiàn)簡(jiǎn)單水波紋效果

二、實(shí)現(xiàn)原理

自定義view,使用Path和貝塞爾曲線繪制,然后不斷刷新,并且改變X、Y的值

主要知識(shí)點(diǎn)rQuadTo的使用   

三、實(shí)現(xiàn)

WaveView.java

public class WaveView extends View {
  private Paint mPaint;
  private final Path mPath;
  //波長(zhǎng)
  private int wavelength = 500;
  private int originY=800;
  private int dx,dy;
 
  public WaveView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    mPaint = new Paint();
    mPath = new Path();
    mPaint.setColor(Color.GREEN);
    mPaint.setStrokeWidth(5);
    mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
//    startanimation();
  }
 
 
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //重置path
    mPath.reset();
//    改變y的起始坐標(biāo)
    if(dy<originY+150){
      dy+=10;
    }
 
    int halfWaveLength = wavelength / 2;
    mPath.moveTo(-wavelength + dx, originY-dy);
    //屏幕多寬,畫(huà)多少
    for (int i = -wavelength; i <= getWidth() + wavelength; i += wavelength) {
      /**
       * 相對(duì)繪制二階貝塞爾曲線(相對(duì)于自己的起始點(diǎn)--也即是上一個(gè)曲線的終點(diǎn) )
       * float dx1 相對(duì)于上一個(gè)曲線的終點(diǎn) 的距離
       * float dy1
       * float dx2
       * float dy2
       */
      mPath.rQuadTo(halfWaveLength / 2, -150, halfWaveLength, 0);
      mPath.rQuadTo(halfWaveLength / 2, 150, halfWaveLength, 0);
 
    }
    //顏色填充
    //畫(huà)一個(gè)封閉的空間
    mPath.lineTo(getWidth(), getHeight());
    mPath.lineTo(0, getHeight());
    mPath.close();
    canvas.drawPath(mPath, mPaint);
//    //設(shè)置起始點(diǎn)坐標(biāo)
//    path.moveTo(100,400);
//    //二階貝塞爾曲線1
//    path.quadTo(250,200,400,400);
//    //二階貝塞爾曲線2
//    path.quadTo(550,600,700,400);
//    //關(guān)閉路徑(將起點(diǎn)和終點(diǎn)閉合)
//    path.close();
//    path.moveTo(100,700);
//    path.cubicTo(50,500,550,500,700,700);
 
  }
 
  public void startanimation() {
    ValueAnimator animator = ValueAnimator.ofInt(0, wavelength);
    animator.setDuration(1000);
    animator.setInterpolator(new LinearInterpolator());
    //無(wú)限循環(huán)
    animator.setRepeatCount(ValueAnimator.INFINITE);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
        dx = (int) animation.getAnimatedValue();
        postInvalidate();
      }
    });
    animator.start();
  }
}

最后把這個(gè)當(dāng)成一個(gè)控件使用就可以。

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“如何使用Android實(shí)現(xiàn)簡(jiǎn)單水波紋效果”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI