当前位置:网站首页>Custom view - click bubble effect
Custom view - click bubble effect
2022-07-22 09:02:00 【cjzcjl】
One 、 The goal is :
Achieve click “ like ” The button has an upward flowing bubble effect . As shown in the figure :
Two 、 Core law analysis :
The floating effect is similar to sin The activity law of function , But it's more natural to float , Random variables need to be introduced . So I decided to put w Set up 180, Plus random fluctuations 180; The phase is [0, 90] Random values in the range ; The amplitude is [0, mWidth / 2] The random value (mWidth Is the width of the control ). And create ValueAnimator,duration Set to [2100, 2800] Internal random fluctuation .
Finally, you can get random y Random changes from small to large x The floating law is :
among ,curVal / 100f Used to express the percentage of the current time in the set animation duration .
double x = (Math.sin(Math.toRadians(mW * (1 - curVal / 100f) + mOffset))) * mAmplitude;
3、 ... and 、 Implementation code :
import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import androidx.annotation.Nullable;
import com.tencent.edu.R;
import com.tencent.edu.common.utils.PixelUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class BubbleViewLike extends FrameLayout {
private int mWidth;
private int mHeight;
private List<Integer> mBubbleIcons = new ArrayList<>();
public BubbleViewLike(Context context) {
super(context);
init();
}
public BubbleViewLike(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public BubbleViewLike(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mBubbleIcons.add(R.drawable.bubble_awesome);
mBubbleIcons.add(R.drawable.bubble_champ_cup);
mBubbleIcons.add(R.drawable.bubble_cow);
mBubbleIcons.add(R.drawable.bubble_gift);
mBubbleIcons.add(R.drawable.bubble_hear);
mBubbleIcons.add(R.drawable.bubble_love);
mBubbleIcons.add(R.drawable.bubble_red_pack);
mBubbleIcons.add(R.drawable.bubble_rocket);
}
public void click() {
AnimatorBean b = new AnimatorBean();
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 100f);
valueAnimator.addUpdateListener(b);
valueAnimator.addListener(b);
valueAnimator.setDuration((int) (2100 + Math.random() * 700));
valueAnimator.start();
}
private class AnimatorBean implements ValueAnimator.AnimatorUpdateListener, Animator.AnimatorListener {
private View mBubbleView;
private double mAmplitude;
private double mOffset;
private double mW;
@Override
public void onAnimationStart(Animator animation, boolean isReverse) {
// Amplitude of half space width × Random scaling factor
mAmplitude = Math.random() * mWidth / 2;
// random phase
mOffset = Math.random() * 90;
// Random frequency
mW = 180f + Math.random() * 180f;
// Create small bubbles
mBubbleView = new Button(getContext());
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(PixelUtil.dp2px(30), PixelUtil.dp2px(30));
mBubbleView.setLayoutParams(params);
mBubbleView.setX(mWidth / 2);
mBubbleView.setY(mHeight);
mBubbleView.setBackgroundResource(mBubbleIcons.get(new Random().nextInt(mBubbleIcons.size())));
addView(mBubbleView);
}
@Override
public void onAnimationEnd(Animator animation, boolean isReverse) {
// Vanished , It can be destroyed
removeView(mBubbleView);
}
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationUpdate(ValueAnimator animation) {
if (mBubbleView != null) {
float curVal = (float) animation.getAnimatedValue();
float scale = (50f - Math.abs(50f - curVal)) / 50f;
float y = ((1 - curVal / 100f) * mHeight);
double x = (Math.sin(Math.toRadians(mW * (1 - curVal / 100f) + mOffset))) * mAmplitude; // adopt sin Function combines random parameters and progress values , Produce with y The value increases to produce different waves x value
mBubbleView.setX((float) x + mWidth / 2);
mBubbleView.setY(y);
mBubbleView.setScaleX(scale * 1.6f);
mBubbleView.setScaleY(scale * 1.6f);
mBubbleView.setAlpha(scale);
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int w = MeasureSpec.getSize(widthMeasureSpec);
int h = MeasureSpec.getSize(heightMeasureSpec);
if (w != mWidth || h != mHeight) { // already onMeasuer once , Do not reinitialize unless the interface size changes view
mWidth = w;
mHeight = h;
}
}
}
Final effect :
Use sin function + Random parameters achieve bubble effect
Bubble custom controls into the classroom app Effect in
边栏推荐
- OSPF routing control
- Rk3288 detailed explanation of LVDS signal configuration and 1080p video signal
- 管正雄:基于预训练模型、智能运维的QA生成算法落地
- scrollIntoView
- Depth analysis of pointer "Five"
- IO 模型详解(通俗易懂)
- 海思[Hi3531] Onvif+Gosap自动搜索IP_Discovery和PTZ的实现
- js判断是否为整数
- 海思Hi3531||瑞芯微RK1109用rtsp客户端实现h264拉流
- Hisilicon [hi3531] onvif+gosap auto search IP_ Implementation of discovery and PTZ
猜你喜欢
Quantitative transaction Diary - summary in January 2021
Openlayers uses canvas to draw round avatar icons
Commonly used instructions in RT thread
STM32-f40x FSMC信号配置并点亮LCD屏
openlayers 使用canvas绘制圆形头像图标
自定义View——点击冒泡效果
Must use destructuring props assignmenteslint
【OAuth2】三、OAuth2配置解读
Ffmpeg audio decoding (seconds understand)
一种图片选择自定义控件
随机推荐
C语言——三种方式实现学生信息管理
2022年华泰开户网上办理安全吗?
5G和移动边缘计算服务器如何打造智慧园区
Add snowflake effect to oes texture
Postman 配置全局变量 postman设置全局token
v-7
MySQL社区版下载地址
JS object deep copy
基于[三星6818]芯片超声波测距驱动编写
文件查找命令
Is it safe for Galaxy Securities VIP to open an account with low commission? Is it a hoax?
Vscode configuration code automatic formatting and repair
scrollIntoView
Writing of ultrasonic ranging driver based on [Samsung 6818] chip
leetcode_两数相加_个人解法
文件操作《二》(5000字总结篇)
[Samsung 6818] GPIO analog SPI signal writing access card identification module driver
加密方法简析
v-7
2021-09-23