Android 两种方式实现类似水波扩散效果
两种方式实现类似水波扩散效果,先上图为敬
- 自定义view实现
- 动画实现
自定义view实现
思路分析:通过canvas画圆,每次改变圆半径和透明度,当半径达到一定程度,再次从中心开始绘圆,达到不同层级的效果,通过不断绘制达到view扩散效果
private Paint centerPaint; //中心圆paint
private int radius = 100; //中心圆半径
private Paint spreadPaint; //扩散圆paint
private float centerX;//圆心x
private float centerY;//圆心y
private int distance = 5; //每次圆递增间距
private int maxRadius = 80; //最大圆半径
private int delayMilliseconds = 33;//扩散延迟间隔,越大扩散越慢
private List<Integer> spreadRadius = new ArrayList<>();//扩散圆层级数,元素为扩散的距离
private List<Integer> alphas = new ArrayList<>();//对应每层圆的透明度
style文件里自定义属性
<declare-styleable name="SpreadView">
<!--中心圆颜色-->
<attr name="spread_center_color" format="color" />
<!--中心圆半径-->
<attr name="spread_radius" format="integer" />
<!--扩散圆颜色-->
<attr name="spread_spread_color" format="color" />
<!--扩散间距-->
<attr name="spread_distance" format="integer" />
<!--扩散最大半径-->
<attr name="spread_max_radius" format="integer" />
<!--扩散延迟间隔-->
<attr name="spread_delay_milliseconds" format="integer" />
</declare-styleable>
初始化
public SpreadView(Context context) {
this(context, null, 0);
}
public SpreadView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public SpreadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SpreadView, defStyleAttr, 0);
radius = a.getInt(R.styleable.SpreadView_spread_radius, radius);
maxRadius = a.getInt(R.styleable.SpreadView_spread_max_radius, maxRadius);
int centerColor = a.getColor(R.styleable.SpreadView_spread_center_color, ContextCompat.getColor(context, R.color.colorAccent));
int spreadColor = a.getColor(R.styleable.SpreadView_spread_spread_color, ContextCompat.getColor(context, R.color.colorAccent));
distance = a.getInt(R.styleable.SpreadView_spread_distance, distance);
a.recycle();
centerPaint = new Paint();
centerPaint.setColor(centerColor);
centerPaint.setAntiAlias(true);
//最开始不透明且扩散距离为0
alphas.add(255);
spreadRadius.add(0);
spreadPaint = new Paint();
spreadPaint.setAntiAlias(true);
spreadPaint.setAlpha(255);
spreadPaint.setColor(spreadColor);
}
确定圆心位置
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
//圆心位置
centerX = w / 2;
centerY = h / 2;
}
自定义view的绘制
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < spreadRadius.size(); i++) {
int alpha = alphas.get(i);
spreadPaint.setAlpha(alpha);
int width = spreadRadius.get(i);
//绘制扩散的圆
canvas.drawCircle(centerX, centerY, radius + width, spreadPaint);
//每次扩散圆半径递增,圆透明度递减
if (alpha > 0 && width < 300) {
alpha = alpha - distance > 0 ? alpha - distance : 1;
alphas.set(i, alpha);
spreadRadius.set(i, width + distance);
}
}
//当最外层扩散圆半径达到最大半径时添加新扩散圆
if (spreadRadius.get(spreadRadius.size() - 1) > maxRadius) {
spreadRadius.add(0);
alphas.add(255);
}
//超过8个扩散圆,删除最先绘制的圆,即最外层的圆
if (spreadRadius.size() >= 8) {
alphas.remove(0);
spreadRadius.remove(0);
}
//中间的圆
canvas.drawCircle(centerX, centerY, radius, centerPaint);
//TODO 可以在中间圆绘制文字或者图片
//延迟更新,达到扩散视觉差效果
postInvalidateDelayed(delayMilliseconds);
}
xml样式
<com.airsaid.diffuseview.widget.SpreadView
android:id="@+id/spreadView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:spread_center_color="@color/colorAccent"
app:spread_delay_milliseconds="35"
app:spread_distance="5"
app:spread_max_radius="90"
app:spread_radius="100"
app:spread_spread_color="@color/colorAccent" />
效果图
中心圆处可以自定义写文字,画图片等等...
动画实现
思路分析:通过动画实现,imageView
不停做动画缩放+渐变
最中心的imageView
保持不变
中间一层imageView
从原始放大到1.4倍,同时从不透明变为半透明
最外层的imageView
从1.4倍放大到1.8倍,同时从半透明变为全透明
利用shape画一个圆,作为动画基础视图
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="65dp"/>
<solid android:color="@color/colorAccent"/>
</shape>
布局视图
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--中心imageView-->
<ImageView
android:id="@+id/iv_wave"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_gravity="center"
android:background="@drawable/shape_circle" />
<!--中间的imageView-->
<ImageView
android:id="@+id/iv_wave_1"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_gravity="center"
android:background="@drawable/shape_circle" />
<!--最外层imageView-->
<ImageView
android:id="@+id/iv_wave_2"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_gravity="center"
android:background="@drawable/shape_circle" />
</FrameLayout>
中间imageView
的动画
private void setAnim1() {
AnimationSet as = new AnimationSet(true);
//缩放动画,以中心从原始放大到1.4倍
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 1.4f, 1.0f, 1.4f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
//渐变动画
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.5f);
scaleAnimation.setDuration(800);
scaleAnimation.setRepeatCount(Animation.INFINITE);
alphaAnimation.setRepeatCount(Animation.INFINITE);
as.setDuration(800);
as.addAnimation(scaleAnimation);
as.addAnimation(alphaAnimation);
iv1.startAnimation(as);
}
最外层imageView
的动画
private void setAnim2() {
AnimationSet as = new AnimationSet(true);
//缩放动画,以中心从1.4倍放大到1.8倍
ScaleAnimation scaleAnimation = new ScaleAnimation(1.4f, 1.8f, 1.4f, 1.8f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
//渐变动画
AlphaAnimation alphaAnimation = new AlphaAnimation(0.5f, 0.1f);
scaleAnimation.setDuration(800);
scaleAnimation.setRepeatCount(Animation.INFINITE);
alphaAnimation.setRepeatCount(Animation.INFINITE);
as.setDuration(800);
as.addAnimation(scaleAnimation);
as.addAnimation(alphaAnimation);
iv2.startAnimation(as);
}
效果图
相比较而言,自定义view的效果更好点,动画实现起来更方便点。
两种方式实现的扩散效果介绍完毕,具体项目里还是要按需变动的。
同时欢迎关注微信公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
数据库
●数据库 OrmLite (JDBC和Android的轻量级ORM java包) Sugar (用超级简单的方法处理Android数据库) GreenDAO (一种轻快地将对象映射到SQLite数据库的ORM解决方案,使用的App有:薄荷,京东) ActiveAndroid (以活动记录方式为Android SQLite提供持久化) SQLBrite (SQLiteOpenHelper 和ContentResolver的轻量级包装) android-database-sqlcipher (数据库加密) storio (Beautiful API for SQLiteDatabase and ContentResolver) realm-java (移动数据库,高性能数据库:一个SQLite和ORM的替换品) ObjectBox (相信不少人都用过GreenDao和EventBus,这两库都是greenrobot公司出品。ObjectBox也是该公司出品。GreenDao号称是最快的与SQLite的对象关系映射(ORM)。但是自从2011年以来,公司声称发现了许多的缺陷,有很多问题影响了...
-
下一篇
Android应用启动过程
前言: 最近发现自己好像做了android这么久,竟然还不知道一个应用是如何去启动的,所以决定去一探究竟,结果发现这个过程好像有点难,好像有点繁杂,毕竟我以前从未接触过framework层的内容。其实我也是一面探究一面来写这篇文章的。没办法,毕竟我又不会,而且在开发应用层的时候也没怎么接触过。但是这东西内容太多了,如果我不写点东西的话长时间不接触肯定会忘记,所以决定一面探究这个过程一面写下这篇文章。最后肯定会有写得不好或者我理解错的地方,还希望有大神能指点一二。注意,这里只讲启动是怎么样的一个流程,而不讲具体怎么去实现 先看看一些基础的概念: Linux进程通信做了什么事 (1)数据传输 (2)资源和数据共享 (3)通知 (4)进程控制 Linux进程通信的方式 了解一下就行,至于详哪种方式用于哪种场景,我也不是很清楚。 (1)管道 (2)信号量 (3)消息队列 (4)信号 (5)共享内存 (6)套接字 IPC机制 什么是IPC,好像接触安卓的时候经常能听到IPC但是又不知道是什么,IPC的全称是Inter-Process Communication,就是指进程间的通信,那么IPC机制...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS8编译安装MySQL8.0.19
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- SpringBoot2全家桶,快速入门学习开发网站教程
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- SpringBoot2整合Redis,开启缓存,提高访问速度