Android自定义view实现仿抖音点赞效果
作者:wish-xy 发布时间:2021-11-04 11:58:19
标签:Android,抖音,点赞
前言
学习自定义view,想找点东西耍一下,刚好看到抖音的点赞效果不错,尝试一下。
抖音效果:
话不多说,先上代码:
public class Love extends RelativeLayout {
private Context mContext;
float[] num = {-30, -20, 0, 20, 30};//随机心形图片角度
public Love(Context context) {
super(context);
initView(context);
}
public Love(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public Love(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
private void initView(Context context) {
mContext = context;
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
ImageView imageView = new ImageView(mContext);
LayoutParams params = new LayoutParams(100, 100);
params.leftMargin = getWidth() - 200;
params.topMargin = getHeight() / 2 - 300;
imageView.setImageDrawable(getResources().getDrawable(R.drawable.heart_red));
imageView.setLayoutParams(params);
addView(imageView);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, "这里是点击爱心的动画,待展示", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
final ImageView imageView = new ImageView(mContext);
LayoutParams params = new LayoutParams(300, 300);
params.leftMargin = (int) event.getX() - 150;
params.topMargin = (int) event.getY() - 300;
imageView.setImageDrawable(getResources().getDrawable(R.drawable.heart_red));
imageView.setLayoutParams(params);
addView(imageView);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(scale(imageView, "scaleX", 2f, 0.9f, 100, 0))
.with(scale(imageView, "scaleY", 2f, 0.9f, 100, 0))
.with(rotation(imageView, 0, 0, num[new Random().nextInt(4)]))
.with(alpha(imageView, 0, 1, 100, 0))
.with(scale(imageView, "scaleX", 0.9f, 1, 50, 150))
.with(scale(imageView, "scaleY", 0.9f, 1, 50, 150))
.with(translationY(imageView, 0, -600, 800, 400))
.with(alpha(imageView, 1, 0, 300, 400))
.with(scale(imageView, "scaleX", 1, 3f, 700, 400))
.with(scale(imageView, "scaleY", 1, 3f, 700, 400));
animatorSet.start();
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
removeViewInLayout(imageView);
}
});
return super.onTouchEvent(event);
}
public static ObjectAnimator scale(View view, String propertyName, float from, float to, long time, long delayTime) {
ObjectAnimator translation = ObjectAnimator.ofFloat(view
, propertyName
, from, to);
translation.setInterpolator(new LinearInterpolator());
translation.setStartDelay(delayTime);
translation.setDuration(time);
return translation;
}
public static ObjectAnimator translationX(View view, float from, float to, long time, long delayTime) {
ObjectAnimator translation = ObjectAnimator.ofFloat(view
, "translationX"
, from, to);
translation.setInterpolator(new LinearInterpolator());
translation.setStartDelay(delayTime);
translation.setDuration(time);
return translation;
}
public static ObjectAnimator translationY(View view, float from, float to, long time, long delayTime) {
ObjectAnimator translation = ObjectAnimator.ofFloat(view
, "translationY"
, from, to);
translation.setInterpolator(new LinearInterpolator());
translation.setStartDelay(delayTime);
translation.setDuration(time);
return translation;
}
public static ObjectAnimator alpha(View view, float from, float to, long time, long delayTime) {
ObjectAnimator translation = ObjectAnimator.ofFloat(view
, "alpha"
, from, to);
translation.setInterpolator(new LinearInterpolator());
translation.setStartDelay(delayTime);
translation.setDuration(time);
return translation;
}
public static ObjectAnimator rotation(View view, long time, long delayTime, float... values) {
ObjectAnimator rotation = ObjectAnimator.ofFloat(view, "rotation", values);
rotation.setDuration(time);
rotation.setStartDelay(delayTime);
rotation.setInterpolator(new TimeInterpolator() {
@Override
public float getInterpolation(float input) {
return input;
}
});
return rotation;
}
}
实现思路
在点击时触发将心形的图片add到整个view中,然后在执行动画。主要的处理逻辑都在onTouchEvent()事件中,下面我们来详细讲解一下思路和代码:
@Override
public boolean onTouchEvent(MotionEvent event) {
final ImageView imageView = new ImageView(mContext);
LayoutParams params = new LayoutParams(300, 300);
params.leftMargin = (int) event.getX() - 150;
params.topMargin = (int) event.getY() - 300;
imageView.setImageDrawable(getResources().getDrawable(R.drawable.heart_red));
imageView.setLayoutParams(params);
addView(imageView);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(scale(imageView, "scaleX", 2f, 0.9f, 100, 0))
.with(scale(imageView, "scaleY", 2f, 0.9f, 100, 0))
.with(rotation(imageView, 0, 0, num[new Random().nextInt(4)]))
.with(alpha(imageView, 0, 1, 100, 0))
.with(scale(imageView, "scaleX", 0.9f, 1, 50, 150))
.with(scale(imageView, "scaleY", 0.9f, 1, 50, 150))
.with(translationY(imageView, 0, -600, 800, 400))
.with(alpha(imageView, 1, 0, 300, 400))
.with(scale(imageView, "scaleX", 1, 3f, 700, 400))
.with(scale(imageView, "scaleY", 1, 3f, 700, 400));
animatorSet.start();
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
removeViewInLayout(imageView);
}
});
return super.onTouchEvent(event);
}
•首先,我们需要在触摸事件中做监听,当有触摸时,创建一个展示心形图片的ImageView。
final ImageView imageView = new ImageView(mContext);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.heart_red));//设置红色心形图片
•设置图片展示的位置,是需要在手指触摸的位置上方,即触摸点是心形的下方角的位置。所以我们需要将ImageView设置到手指的位置
LayoutParams params = new LayoutParams(300, 300);
params.leftMargin = (int) event.getX() - 150;
params.topMargin = (int) event.getY() - 300;
imageView.setLayoutParams(params);
•给imageView add到父view中。
addView(imageView);
•设置imageView动画
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(scale(imageView, "scaleX", 2f, 0.9f, 100, 0))//缩放动画,X轴2倍缩小至0.9倍
.with(scale(imageView, "scaleY", 2f, 0.9f, 100, 0))//缩放动画,Y轴2倍缩小至0.9倍
.with(rotation(imageView, 0, 0, num[new Random().nextInt(4)]))//旋转动画,随机旋转角度num={-30.-20,0,20,30}
.with(alpha(imageView, 0, 1, 100, 0))//渐变透明度动画,透明度从0-1.
.with(scale(imageView, "scaleX", 0.9f, 1, 50, 150))//缩放动画,X轴0.9倍缩小至1倍
.with(scale(imageView, "scaleY", 0.9f, 1, 50, 150))//缩放动画,Y轴0.9倍缩小至1倍
.with(translationY(imageView, 0, -600, 800, 400))//平移动画,Y轴从0向上移动600单位
.with(alpha(imageView, 1, 0, 300, 400))//透明度动画,从1-0
.with(scale(imageView, "scaleX", 1, 3f, 700, 400))//缩放动画,X轴1倍放大至3倍
.with(scale(imageView, "scaleY", 1, 3f, 700, 400));//缩放动画,Y轴1倍放大至3倍
animatorSet.start();
•当然,我们不可能无限制的增加view,在view消失之后,需要手动的移除改ImageView。
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
removeViewInLayout(imageView);
}
});
效果如下:
总结
以上所述是小编给大家介绍的Android自定义view实现仿抖音点赞效果网站的支持!
来源:https://blog.csdn.net/ibelieveyouwxy/article/details/80417979


猜你喜欢
- 本文以实例形式讲述了基于Java的图的广度优先遍历算法实现方法,具体方法如下:用邻接矩阵存储图方法:1.确定图的顶点个数和边的个数2.输入顶
- Class.forName(xxx.xx.xx) 返回的是一个类一.首先你要明白在java里面任何class都要装载在虚拟机上才能运行。1.
- 问题描述:在用fabric集成后编译出现如下错误,Error:Cause: hostname in certificate didn'
- eclipse导入Spring配置文件约束 Windows-Preference-XML-XMLCatalog点 Add 选Fil
- 本文实例为大家分享了Java实现简易计算器的具体代码,供大家参考,具体内容如下程序的运行环境为Windows10 ,编译环境为IDEA。计算
- using System; using System.Drawing; using System.Collec
- 桥接模式桥接模式是将抽象部分与它的实现部分分离,使他们都可以独立地变化。它是一种对象结构型模式,又称为柄体(Handle and Body)
- 1、Alt+*(按钮快捷键)按钮快捷键也为最常用快捷键,其设置也故为简单。在大家给button、label、menuStrip等其他控件的T
- Java常用类String类概述String类:代表字符串。Java程序中的所有字符串字面值(如:”abc“)都作为子类的实例实现Strin
- 本节向你展示如何在任务中发送数据给UI线程里的对象,这个特性允许你在后台线程工作,完了在UI线程展示结果。在UI线程定义一个HandlerH
- 一、写在前面的在需求上遇到背景设置透明度还是比较常见的,设置透明度有几种方式,但是不同的场景应用下,不同的方式可能会出现一些问题。针对开发过
- ava最明显的一个优势就是它的内存管理机制。你只需简单创建对象,java的垃圾回收机制负责分配和释放内存。然而情况并不像想像的那么简单,因为
- 上一篇文章我们了解了Java背包问题求解实例代码,接下来我们看看Java中模仿用户登录的相关代码,下面是具体内容。基于用户从控制台输入模拟的
- mybatis多层级collection嵌套json结构第一步查询第一层查询,将第一层的id传递到第二层当条件查询
- 虽然闭包主要是函数式编程的玩意儿,而C#的最主要特征是面向对象,但是利用委托或lambda表达式,C#也可以写出具有函数式编程风味的代码。同
- 为了提高用户体验,我们肯定希望该Dialog能更加炫酷,让用户看着更舒服。那如何做呢,当然是我们自己定义一个ProgressDialog了。
- springboot的最强大的就是那些xxxAutoconfiguration,但是这些xxxAutoConfiguration又依赖那些s
- 概述JDK的bin目录下提供了很多命令工具,比如java.exe,javap.exe,javac.exe。。。。。。这些命令由jdk/lib
- 什么是JDBCJDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java AP
- idea如何设置系统环境变量背景最近在接入阿里云的短信服务,在使用阿里云短信服务的SDK过程中想看看SDK中HttpUtil 中public