Android学习之使用贝塞尔曲线实现波纹效果
2017-04-19 09:59
597 查看
package com.lzy.host.perfect.view;
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;
/**
* 波纹效果实现View
*/
public class WaveView extends View {
//波浪画笔
private Paint mPaint;
//测试红点画笔
private Paint mCyclePaint;
//波浪Path类
private Path mPath;
//一个波浪长度
private int mWaveLength = 1000;
//波纹个数
private int mWaveCount;
//平移偏移量
private int mOffset;
//波纹的中间轴
private int mCenterY;
//屏幕高度
private int mScreenHeight;
//屏幕宽度
private int mScreenWidth;
private ValueAnimator animator;
public WaveView(Context context) {
super(context);
}
public WaveView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public WaveView(Context context, AttributeSet attrs) {
super(context, attrs);
this.animator = ValueAnimator.ofInt(0, mWaveLength);
mPath = new Path();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setAntiAlias(true);
mPaint.setColor(Color.BLUE);
mPaint.setStrokeWidth(5);
mPaint.setStyle(Paint.Style.STROKE);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mScreenHeight = h;
mScreenWidth = w;
//加1.5:至少保证波纹有2个,至少2个才能实现平移效果
mWaveCount = (int) Math.round(mScreenWidth / mWaveLength + 1.5);
mCenterY = mScreenHeight / 2;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPath.reset();
//移到屏幕外最左边
mPath.moveTo(-mWaveLength + mOffset, mCenterY);
for (int i = 0; i < mWaveCount; i++) {
//正弦曲线
mPath.quadTo((-mWaveLength * 3 / 4) + (i * mWaveLength) + mOffset, mCenterY + 60, (-mWaveLength / 2) + (i * mWaveLength) + mOffset, mCenterY);
mPath.quadTo((-mWaveLength / 4) + (i * mWaveLength) + mOffset, mCenterY - 60, i * mWaveLength + mOffset, mCenterY);
}
//填充矩形
// mPath.lineTo(mScreenWidth, mScreenHeight);
// mPath.lineTo(mScreenWidth + mWaveLength, mCenterY);
// mPath.close();
canvas.drawPath(mPath, mPaint);
}
public void start(long interval) {
animator.setDuration(interval);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setInterpolator(new LinearInterpolator());
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mOffset = (int) animation.getAnimatedValue();
postInvalidate();
}
});
animator.start();
}
public void stop() {
if (this.animator != null) {
this.animator.cancel();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/main_gray"
android:clipToPadding="true"
android:fitsSystemWindows="true"
android:orientation="vertical">
<include layout="@layout/base_top_title" />
<com.lzy.host.perfect.view.WaveView
android:id="@+id/waveView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
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;
/**
* 波纹效果实现View
*/
public class WaveView extends View {
//波浪画笔
private Paint mPaint;
//测试红点画笔
private Paint mCyclePaint;
//波浪Path类
private Path mPath;
//一个波浪长度
private int mWaveLength = 1000;
//波纹个数
private int mWaveCount;
//平移偏移量
private int mOffset;
//波纹的中间轴
private int mCenterY;
//屏幕高度
private int mScreenHeight;
//屏幕宽度
private int mScreenWidth;
private ValueAnimator animator;
public WaveView(Context context) {
super(context);
}
public WaveView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public WaveView(Context context, AttributeSet attrs) {
super(context, attrs);
this.animator = ValueAnimator.ofInt(0, mWaveLength);
mPath = new Path();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setAntiAlias(true);
mPaint.setColor(Color.BLUE);
mPaint.setStrokeWidth(5);
mPaint.setStyle(Paint.Style.STROKE);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mScreenHeight = h;
mScreenWidth = w;
//加1.5:至少保证波纹有2个,至少2个才能实现平移效果
mWaveCount = (int) Math.round(mScreenWidth / mWaveLength + 1.5);
mCenterY = mScreenHeight / 2;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPath.reset();
//移到屏幕外最左边
mPath.moveTo(-mWaveLength + mOffset, mCenterY);
for (int i = 0; i < mWaveCount; i++) {
//正弦曲线
mPath.quadTo((-mWaveLength * 3 / 4) + (i * mWaveLength) + mOffset, mCenterY + 60, (-mWaveLength / 2) + (i * mWaveLength) + mOffset, mCenterY);
mPath.quadTo((-mWaveLength / 4) + (i * mWaveLength) + mOffset, mCenterY - 60, i * mWaveLength + mOffset, mCenterY);
}
//填充矩形
// mPath.lineTo(mScreenWidth, mScreenHeight);
// mPath.lineTo(mScreenWidth + mWaveLength, mCenterY);
// mPath.close();
canvas.drawPath(mPath, mPaint);
}
public void start(long interval) {
animator.setDuration(interval);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setInterpolator(new LinearInterpolator());
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mOffset = (int) animation.getAnimatedValue();
postInvalidate();
}
});
animator.start();
}
public void stop() {
if (this.animator != null) {
this.animator.cancel();
}
}
}
package com.lzy.host.perfect.demo; import android.os.Bundle; import android.widget.TextView; import com.lzy.host.perfect.R; import com.lzy.host.perfect.activity.BaseActivity; import com.lzy.host.perfect.view.WaveView; /** * 波纹效果 * * @author linzhiyong * @time 2017年1月16日17:07:18 * @desc * */ public class WaveViewDemoActivity extends BaseActivity { private TextView topTitleView; private WaveView waveView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_wave_demo); topTitleView = (TextView) findViewById(R.id.base_top_title); topTitleView.setText("波纹效果"); waveView = (WaveView) findViewById(R.id.waveView); waveView.start(1500); } @Override protected void onDestroy() { if (waveView != null) { waveView.stop(); } super.onDestroy(); } }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/main_gray"
android:clipToPadding="true"
android:fitsSystemWindows="true"
android:orientation="vertical">
<include layout="@layout/base_top_title" />
<com.lzy.host.perfect.view.WaveView
android:id="@+id/waveView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
相关文章推荐
- Android开发之使用贝塞尔曲线实现黏性水珠下拉效果
- 【Android开源项目解析】QQ“一键下班”功能实现解析——学习Path及贝塞尔曲线的基本使用
- 2014-11-6Android学习------Android 仿真翻页效果实现--------贝塞尔曲线(二)
- 学习使用Material Design控件(四)Android实现标题栏自动缩放、放大效果
- 2014-11-8Android学习------Android 仿真翻页效果实现原理学习--------贝塞尔曲线(三)
- 安卓学习笔记---Android源码解析--Material Design之水波纹点击效果RippleEffect使用
- android 使用贝塞尔曲线实现“波浪”效果思路解析
- Android学习-使用ViewFlipper实现屏幕切换的动画效果
- Android学习---使用异步内部类实现进度条加载效果
- Android自定义View——贝塞尔曲线实现水波纹效果
- Android Material Design控件学习(三)——使用TextInputLayout实现酷市场登录效果
- Android使用PowerImageView实现播放强大的ImageView动画效果
- Android中MaterialDesign的使用,以及实现仿网易新闻客户端的UI效果(二)
- android使用xml实现虚线效果
- Android之UI学习篇十:使用TabHost实现微博客户端界面
- android学习笔记---59_各种图形的使用介绍,android炫酷效果的实现
- android中使用LinearGradient实现渲染效果
- Android开发之RecyclerView的基本使用(实现常用的4种效果)
- Android 使用NineOldAndroids实现绚丽的ListView左右滑动删除Item效果
- 【移动开发】Android波纹动画效果实现 推荐