gddhy

_(:з」∠)_ 加载中...
  • 主页
  • 归档
  • 工具
  • 关于
所有文章 友链

gddhy

_(:з」∠)_ 加载中...

  • 主页
  • 归档
  • 工具
  • 关于

仿酷安客户端的主题切换动画效果

2022-09-30
字数统计:1.2k字 阅读时长≈6分

这是一个仿酷安主题切换的动画,之前是在 郭霖的微信公众号 上了解到的,最近想要使用时发现按照Github上的方法添加依赖时已经无法使用,作者在Github上没有留下源码,不过好在在作者Gitee中找到了这份源码,可以直接拿来使用

使用时直接将下面源码添加到程序中就行

APIs

Method Description
create(View onClickView) 创建动画对象(静态方法)
setDuration(long duration) 设置动画时长
setOnAnimationEndListener(Listener listener) 动画播放完毕监听器
start() 开始播放动画

使用示例

1
2
3
4
5
public void onClick(View view) {
RippleAnimation.create(view).setDuration(duration).start();

//在这里切换你的主题
}

作者的Git
https://github.com/wuyr/RippleAnimation
https://gitee.com/dingyi222666/RippleAnimation

相关代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package com.wuyr.rippleanimation;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.os.Build;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

/**
* Created by wuyr on 3/15/18 5:23 PM.
* GitHub: https://github.com/wuyr/RippleAnimation
*/

@SuppressLint("ViewConstructor")
public class RippleAnimation extends View {

private Bitmap mBackground;//屏幕截图
private Paint mPaint;
private int mMaxRadius, mStartRadius, mCurrentRadius;
private boolean isStarted;
private long mDuration;
private float mStartX, mStartY;//扩散的起点
private ViewGroup mRootView;//DecorView
private OnAnimationEndListener mOnAnimationEndListener;
private Animator.AnimatorListener mAnimatorListener;
private ValueAnimator.AnimatorUpdateListener mAnimatorUpdateListener;

public static RippleAnimation create(View onClickView) {
Context context = onClickView.getContext();
int newWidth = onClickView.getWidth() / 2;
int newHeight = onClickView.getHeight() / 2;
//计算起点位置
float startX = getAbsoluteX(onClickView) + newWidth;
float startY = getAbsoluteY(onClickView) + newHeight;
//起始半径
//因为我们要避免遮挡按钮
int radius = Math.max(newWidth, newHeight);
return new RippleAnimation(context, startX, startY, radius);
}

private RippleAnimation(Context context, float startX, float startY, int radius) {
super(context);
//获取activity的根视图,用来添加本View
mRootView = (ViewGroup) getActivityFromContext(context).getWindow().getDecorView();
mStartX = startX;
mStartY = startY;
mStartRadius = radius;
mPaint = new Paint();
mPaint.setAntiAlias(true);
//设置为擦除模式
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
updateMaxRadius();
initListener();
}

/**
* try get host activity from view.
* views hosted on floating window like dialog and toast will sure return null.
*
* @return host activity
*/
private Activity getActivityFromContext(Context context) {
while (context instanceof ContextWrapper) {
if (context instanceof Activity) {
return (Activity) context;
}
context = ((ContextWrapper) context).getBaseContext();
}
throw new RuntimeException("Activity not found!");
}

/**
* 开始播放动画
*/
public void start() {
if (!isStarted) {
isStarted = true;
updateBackground();
attachToRootView();
getAnimator().start();
}
}

/**
* 设置动画时长
*/
public RippleAnimation setDuration(long duration) {
mDuration = duration;
return this;
}

private void initListener() {
mAnimatorListener = new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
if (mOnAnimationEndListener != null) {
mOnAnimationEndListener.onAnimationEnd();
}
isStarted = false;
//动画播放完毕, 移除本View
detachFromRootView();
}
};
mAnimatorUpdateListener = new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
//更新圆的半径
mCurrentRadius = (int) (float) animation.getAnimatedValue() + mStartRadius;
postInvalidate();
}
};
}

/**
* 根据起始点将屏幕分成4个小矩形,mMaxRadius就是取它们中最大的矩形的对角线长度
* 这样的话, 无论起始点在屏幕中的哪一个位置上, 我们绘制的圆形总是能覆盖屏幕
*/
private void updateMaxRadius() {
//将屏幕分成4个小矩形
RectF leftTop = new RectF(0, 0, mStartX + mStartRadius, mStartY + mStartRadius);
RectF rightTop = new RectF(leftTop.right, 0, mRootView.getRight(), leftTop.bottom);
RectF leftBottom = new RectF(0, leftTop.bottom, leftTop.right, mRootView.getBottom());
RectF rightBottom = new RectF(leftBottom.right, leftTop.bottom, mRootView.getRight(), leftBottom.bottom);
//分别获取对角线长度
double leftTopHypotenuse = Math.sqrt(Math.pow(leftTop.width(), 2) + Math.pow(leftTop.height(), 2));
double rightTopHypotenuse = Math.sqrt(Math.pow(rightTop.width(), 2) + Math.pow(rightTop.height(), 2));
double leftBottomHypotenuse = Math.sqrt(Math.pow(leftBottom.width(), 2) + Math.pow(leftBottom.height(), 2));
double rightBottomHypotenuse = Math.sqrt(Math.pow(rightBottom.width(), 2) + Math.pow(rightBottom.height(), 2));
//取最大值
mMaxRadius = (int) Math.max(
Math.max(leftTopHypotenuse, rightTopHypotenuse),
Math.max(leftBottomHypotenuse, rightBottomHypotenuse));
}

/**
* 添加到根视图
*/
private void attachToRootView() {
setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
mRootView.addView(this);
}

/**
* 从根视图中移除并释放资源
*/
private void detachFromRootView() {
if (mRootView != null) {
mRootView.removeView(this);
mRootView = null;
}
if (mBackground != null) {
if (!mBackground.isRecycled()) {
mBackground.recycle();
}
mBackground = null;
}
if (mPaint != null) {
mPaint = null;
}
}

/**
* 更新屏幕截图
*/
private void updateBackground() {
if (mBackground != null && !mBackground.isRecycled()) {
mBackground.recycle();
}
mBackground = getBitmapFromView(mRootView);
}

/**
* 由canvas更新背景截图(drawingCache已废弃)
*/
private static Bitmap getBitmapFromView(View view) {
view.measure(MeasureSpec.makeMeasureSpec(view.getLayoutParams().width, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(view.getLayoutParams().height, MeasureSpec.EXACTLY));
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}

/**
* 获取view在屏幕中的绝对x坐标
*/
private static float getAbsoluteX(View view) {
float x = view.getX();
ViewParent parent = view.getParent();
if (parent instanceof View) {
x += getAbsoluteX((View) parent);
}
return x;
}

/**
* 获取view在屏幕中的绝对y坐标
*/
private static float getAbsoluteY(View view) {
float y = view.getY();
ViewParent parent = view.getParent();
if (parent instanceof View) {
y += getAbsoluteY((View) parent);
}
return y;
}

@Override
protected void onDraw(Canvas canvas) {
//在新的图层上面绘制
int layer;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
layer = canvas.saveLayer(0, 0, getWidth(), getHeight(), null);
} else {
layer = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
}
canvas.drawBitmap(mBackground, 0, 0, null);
canvas.drawCircle(mStartX, mStartY, mCurrentRadius, mPaint);
canvas.restoreToCount(layer);
}

private ValueAnimator getAnimator() {
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, mMaxRadius).setDuration(mDuration);
valueAnimator.addUpdateListener(mAnimatorUpdateListener);
valueAnimator.addListener(mAnimatorListener);
return valueAnimator;
}

/**
* 设置动画结束监听器
*/
public RippleAnimation setOnAnimationEndListener(OnAnimationEndListener listener) {
mOnAnimationEndListener = listener;
return this;
}

@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
return true;
}

public interface OnAnimationEndListener {
void onAnimationEnd();
}
}
赏

谢谢你请我吃糖果

微信

扫一扫,分享到微信

微信分享二维码
企业微信通知
Context转Activity
  1. 1. APIs
  2. 2. 使用示例
  3. 3. 相关代码
留言已关闭
:gddhy
© gddhy
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链

tag:

  • Android
  • 软件分享
  • game
  • Hexo
  • JavaScript
  • 旧机博物馆
  • MIUI
  • Java
  • git
  • Termux
  • mtk
  • 原神
  • Win
  • Html
  • 安卓学习笔记

    缺失模块

  • Luminous' Home
  • 影子博客
  • 四次元领域
  • 初之音
  • Mr.Pumpkin
  • ZhaoQuinn 's Blog