Android 사용자 정의 View 물결 유도 애니메이션 구현

1.실현 효과 도

베 세 르 곡선 에 대해

구현 코드
1.사용자 정의 뷰

package com.czhappy.showintroduce.view;

import android.content.Context;
import android.graphics.Bitmap;
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.widget.RelativeLayout;

/**
 * Description:        view
 * User: chenzheng
 * Date: 2017/1/14 0014
 * Time: 18:01
 */
public class RippleIntroView extends RelativeLayout implements Runnable {

 private int mMaxRadius = 70;
 private int mInterval = 20;
 private int count = 0;

 private Bitmap mCacheBitmap;
 private Paint mRipplePaint;
 private Paint mCirclePaint;
 private Path mArcPath;

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

 public RippleIntroView(Context context, AttributeSet attrs) {
  this(context, attrs, 0);
 }

 public RippleIntroView(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  init();
 }

 private void init() {
  mRipplePaint = new Paint();
  mRipplePaint.setAntiAlias(true);
  mRipplePaint.setStyle(Paint.Style.STROKE);
  mRipplePaint.setColor(Color.WHITE);
  mRipplePaint.setStrokeWidth(2.f);

  mCirclePaint = new Paint();
  mCirclePaint.setAntiAlias(true);
  mCirclePaint.setStyle(Paint.Style.FILL);
  mCirclePaint.setColor(Color.WHITE);

  mArcPath = new Path();
 }

 /**
  * view         
  * @param w
  * @param h
  * @param oldw
  * @param oldh
  */
 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  super.onSizeChanged(w, h, oldw, oldh);
  if (mCacheBitmap != null) {
   mCacheBitmap.recycle();
   mCacheBitmap = null;
  }
 }

 @Override
 protected void onDraw(Canvas canvas) {
  //      view
  View mPlusChild = getChildAt(0);
  //      view
  View mRefsChild = getChildAt(1);
  if (mPlusChild == null || mRefsChild == null) return;

  //        
  final int pw = mPlusChild.getWidth();
  final int ph = mPlusChild.getHeight();

  //        
  final int fw = mRefsChild.getWidth();
  final int fh = mRefsChild.getHeight();

  if (pw == 0 || ph == 0) return;

  //         
  final float px = mPlusChild.getX() + pw / 2;
  final float py = mPlusChild.getY() + ph / 2;
  //         
  final float fx = mRefsChild.getX();
  final float fy = mRefsChild.getY();

  final int rw = pw / 2;
  final int rh = ph / 2;

  if (mCacheBitmap == null) {
   mCacheBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
   Canvas cv = new Canvas(mCacheBitmap);
   super.onDraw(cv);

   //         path     
   mArcPath.reset();

   //       x,y   ,           20  
   mArcPath.moveTo(px, py + rh + mInterval);
   //       ,      ,           ,           
   mArcPath.quadTo(px, fy - mInterval, fx + fw * 0.618f, fy - mInterval);
   //0~255,       
   mRipplePaint.setAlpha(255);
   cv.drawPath(mArcPath, mRipplePaint);
   //     6     
   cv.drawCircle(px, py + rh + mInterval, 6, mCirclePaint);
  }

  //      
  canvas.drawBitmap(mCacheBitmap, 0, 0, mCirclePaint);

  //         
  int save = canvas.save();
  for (int step = count; step <= mMaxRadius; step += mInterval) {
   //step         
   mRipplePaint.setAlpha(255 * (mMaxRadius - step) / mMaxRadius);
   canvas.drawCircle(px, py, (float) (rw + step), mRipplePaint);
  }
  //  Canvas   
  canvas.restoreToCount(save);
  //  80       
  postDelayed(this, 80);
 }

 @Override
 public void run() {
  // run            ,  ,       ,  run     
  removeCallbacks(this);
  count += 2;
  count %= mInterval;
  invalidate();//  
 }

 /**
  *   view   ,    
  */
 @Override
 protected void onDetachedFromWindow() {
  super.onDetachedFromWindow();
  if (mCacheBitmap != null) {
   mCacheBitmap.recycle();
   mCacheBitmap = null;
  }
 }
}

2.MainActivity.java

package com.czhappy.showintroduce.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;

import com.czhappy.showintroduce.R;

public class MainActivity extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  View view = findViewById(R.id.layout_ripple);
  view.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    ((ViewGroup) v.getParent()).removeView(v);
   }
  });
 }
}

3.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Hello World!" />

 <com.czhappy.showintroduce.view.RippleIntroView
  android:id="@+id/layout_ripple"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:clickable="true"
  android:fitsSystemWindows="true"
  android:background="#AA000000">

  <ImageView
   android:id="@+id/iv_plus"
   android:layout_marginTop="36dp"
   android:src="@mipmap/ic_add"
   android:layout_alignParentRight="true"
   android:layout_marginRight="6dp"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"/>

  <ImageView
   android:src="@mipmap/tips_subscribe"
   android:id="@+id/tv_title"
   android:layout_below="@id/iv_plus"
   android:layout_marginTop="50dp"
   android:layout_alignParentRight="true"
   android:layout_marginRight="40dp"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"/>

 </com.czhappy.showintroduce.view.RippleIntroView>

</FrameLayout>

3.소스 코드 다운로드
http://xiazai.jb51.net/201701/yuanma/AndroidShowIntroduce(jb51.net).rar
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기