Android自定义View 实现水波纹动画引导效果
作者:chenzheng8975 时间:2022-04-16 17:47:10
一、实现效果图
二、实现代码
1.自定义view
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>
以上所述是小编给大家介绍的Android自定义View 实现水波纹动画引导效果网站的支持!
来源:http://blog.csdn.net/chenzheng8975/article/details/54580390
标签:android,水波纹,view
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决SpringMVC、tomcat、Intellij idea、ajax中文乱码问题
2023-10-20 10:16:23
![](https://img.aspxhome.com/file/2023/5/72625_0s.jpg)
详解jeefast和Mybatis实现二级联动的问题
2022-11-10 05:10:15
![](https://img.aspxhome.com/file/2023/3/116823_0s.png)
java如何删除以逗号隔开的字符串中某一个值
2023-06-12 11:59:19
![](https://img.aspxhome.com/file/2023/8/113518_0s.png)
使用IDEA创建servlet JavaWeb 应用及使用Tomcat本地部署的实现
2023-09-11 06:04:09
![](https://img.aspxhome.com/file/2023/4/57814_0s.jpg)
Java生成含字母和数字的6位随机字符串
2023-04-02 02:28:58
仿orm自动生成分页SQL分享
2022-11-09 22:48:30
c# 实现语音合成
2021-06-16 00:17:47
![](https://img.aspxhome.com/file/2023/4/91314_0s.png)
Java编程中利用InetAddress类确定特殊IP地址的方法
2021-06-24 23:00:12
JavaFX 监听窗口关闭事件实例详解
2023-07-20 21:53:06
![](https://img.aspxhome.com/file/2023/4/110884_0s.jpg)
读写Android中assets目录下的文件的方法详解
2023-03-03 15:50:35
![](https://img.aspxhome.com/file/2023/7/92617_0s.png)
C# Winform调用百度接口实现人脸识别教程(附源码)
2021-12-27 13:17:09
![](https://img.aspxhome.com/file/2023/2/91252_0s.jpg)
DevExpress中GridControl列转义的实现方法
2022-11-10 05:14:31
![](https://img.aspxhome.com/file/2023/8/68078_0s.png)
Java class文件格式之常量池_动力节点Java学院整理
2023-04-14 07:14:06
![](https://img.aspxhome.com/file/2023/2/66472_0s.jpg)
C#判断字符串中是否包含指定字符串及contains与indexof方法效率问题
2022-07-06 19:04:51
![](https://img.aspxhome.com/file/2023/6/72466_0s.png)
Java实现限定时间CountDownLatch并行场景
2023-06-05 01:47:27
SpringBoot security安全认证登录的实现方法
2021-05-30 08:09:56
![](https://img.aspxhome.com/file/2023/5/63685_0s.png)
Unity3D实现播放gif图功能
2021-11-23 13:17:37
java通过JFrame做一个登录系统的界面完整代码示例
2023-11-06 05:18:32
![](https://img.aspxhome.com/file/2023/0/110860_0s.png)
Java基于Runtime调用外部程序出现阻塞的解决方法
2023-11-09 04:24:23
C# ODP.NET 调用Oracle函数返回值时报错的一个解决方案
2021-10-03 01:54:19
![](https://img.aspxhome.com/file/2023/2/111092_0s.png)