当前位置: 移动技术网 > IT编程>移动开发>Android > Android自定义view实现阻尼效果的加载动画

Android自定义view实现阻尼效果的加载动画

2019年07月24日  | 移动技术网IT编程  | 我要评论

安倍表彰中国留学生,涂料机械,小米贷王哲

效果:

这里写图片描述

需要知识:

1. 二次贝塞尔曲线

2. 动画知识

3. 基础自定义view知识

先来解释下什么叫阻尼运动

阻尼振动是指,由于振动系统受到摩擦和介质阻力或其他能耗而使振幅随时间逐渐衰减的振动,又称减幅振动、衰减振动。[1] 不论是弹簧振子还是单摆由于外界的摩擦和介质阻力总是存在,在振动过程中要不断克服外界阻力做功,消耗能量,振幅就会逐渐减小,经过一段时间,振动就会完全停下来。这种振幅随时间减小的振动称为阻尼振动.因为振幅与振动的能量有关,阻尼振动也就是能量不断减少的振动.阻尼振动是非简谐运动.阻尼振动系统属于耗散系统。这里的阻尼是指任何振动系统在振动中,由于外界作用或系统本身固有的原因引起的振动幅度逐渐下降的特性,以及此一特性的量化表征。

这里写图片描述

本例中文字部分凹陷就是这种效果,当然这篇文章知识带你简单的使用.

跳动的水果效果实现

剖析:从上面的效果图中很面就是从顶端向下掉落然后再向上 期间旋转即可.

那么我们首先自定义一个view继承framelayout

public class my extends framelayout {
public my(context context) {
super(context);
}
public my(context context, attributeset attrs) {
super(context, attrs);
}
}

需要素材如下三张图片:

这里写图片描述
这里写图片描述
这里写图片描述

也许有人会问我看到你效果图到顶部或者底部就变成向上或者向下了.你三张够吗?

答:到顶部或者底部旋转180度即可

我们现在自定义中定义几个变量

//用于记录当前图片使用数组中的哪张
int indeximgflag = 0;
//下沉图片 前面三个图片的id
int allimgdown [] = {r.mipmap.p2,r.mipmap.p4,r.mipmap.p6,r.mipmap.p8};
//动画效果一次下沉或上弹的时间 animationduration*2=一次完整动画时间
int animationduration = 1000;
//弹起来的图片
imageview iv;
//图片下沉高度(即从最高点到最低点的距离)
int downheight = 2;
//掉下去的动画
private animation translatedown;
//弹起动画
private animation translateup;
//旋转动画
private objectanimator rotation;

我们再来看看初始化动画的方法(此方法使用了递归思想,实现无限播放动画,大家可以看看哪里不理解)

//初始化弹跳动画
public void myanmation(){
//下沉效果动画
translatedown = new translateanimation(
animation.relative_to_self,0,animation.relative_to_self,0,
animation.relative_to_self,0,animation.relative_to_self,downheight);
translatedown.setduration(animationduration);
//设置一个插值器 动画将会播放越来越快 模拟重力
translatedown.setinterpolator(new accelerateinterpolator());
//上弹动画
translateup = new translateanimation(
animation.relative_to_self,0,animation.relative_to_self,0,
animation.relative_to_self,downheight,animation.relative_to_self,0
);
translateup.setduration(animationduration);
////设置一个插值器 动画将会播放越来越慢 模拟反重力
translateup.setinterpolator(new decelerateinterpolator());
//当下沉动画完成时播放启动上弹
translatedown.setanimationlistener(new animation.animationlistener() {
@override
public void onanimationstart(animation animation) {
iv.setimageresource(allimgdown[indeximgflag]);
rotation = objectanimator.offloat(iv, "rotation", 180f, 360f);
rotation.setduration(1000);
rotation.start();
}
@override
public void onanimationend(animation animation) {
iv.startanimation(translateup);
}
@override
public void onanimationrepeat(animation animation) {
}
});
//当上移动画完成时 播放下移动画
translateup.setanimationlistener(new animation.animationlistener() {
@override
public void onanimationstart(animation animation) {
indeximgflag = 1+indeximgflag>=allimgdown.length?0:1+indeximgflag;
iv.setimageresource(allimgdown[indeximgflag]);
rotation = objectanimator.offloat(iv, "rotation", 0.0f, 180f);
rotation.setduration(1000);
rotation.start();
}
@override
public void onanimationend(animation animation) {
//递归
iv.startanimation(translatedown);
}
@override
public void onanimationrepeat(animation animation) {
}
});
}

以上代码知识:

插值器:会让一个动画在播放时在某一时间段加快动画或者减慢

//设置一个插值器 动画将会播放越来越快 模拟重力

1.translatedown.setinterpolator(new accelerateinterpolator());

这个插值器 速率表示图:

这里写图片描述

可以从斜率看到使用此插值器 动画将越来越快.意义在于模仿下落时重力的影响

////设置一个插值器 动画将会播放越来越慢 模拟反重力

2. translateup.setinterpolator(new decelerateinterpolator());

速率图:

这里写图片描述

最后我们初始化下图片控件到我们的自定义view

private void init() {
//初始化弹跳图片 控件
iv = new imageview(getcontext());
viewgroup.layoutparams params = new viewgroup.layoutparams(
viewgroup.layoutparams.wrap_content,
viewgroup.layoutparams.wrap_content);
iv.setlayoutparams(params);
iv.setimageresource(allimgdown[0]);
this.addview(iv);
iv.measure(0,0);
iv.getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() {
@override
public void ongloballayout() {
if (!flagmeure)
{
flagmeure =true;
//由于画文字是由基准线开始
path.moveto(iv.getx()-textwidth/2+iv.getwidth()/2, textheight+iv.getheight()+downheight*iv.getheight());
//计算最大弹力
maxelasticfactor = (float) (textheight / elastic);
//初始化贝塞尔曲线
path.rquadto(textwidth / 2, 0, textwidth, 0);
//初始化上弹和下沉动画
myanmation();
iv.startanimation(translatedown);
}
}
});
}

上面的知识:

1. iv.measure(0,0);主动通知系统去测量此控件 不然iv.getwidth = 0;
//下面这个是同理 等iv测量完时回调 不然iv.getwidth = 0;
2. iv.getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener(){ 
… 
}

原因:textview tv = new textview() 或者 layoutinflat 填充布局都是

异步所以你在new出来或者填充时直接获取自然返回0

到现在为止你只需要在自定义view 的onsizechanged回调方法中调用init()即可看到动画的弹动

@override
protected void onsizechanged(int w, int h, int oldw, int oldh) {
super.onsizechanged(w, h, oldw, oldh);
init();
}

此方法会在onmesure方法执行完成后回调 这样你就可以在此方法获得自定义view的宽高了

效果图:

这里写图片描述

画文字成u形

首先你得知道如下知识

贝塞尔曲线:具体学习

这里我们用到2此贝塞尔曲线

我们看看大概是什么叫2次贝塞尔曲线

这里写图片描述 

我们看看 三个点 p0 p1 p2 我们 把p0 称为 开始点 p1 为控制点 p2 结束点,那么可以用贝塞尔公式画出如图曲线

这里大家没必要深究怎么画出来. 也不需要你懂 这个要数学基础的

那么我们在安卓中怎么画呢?

path path = new path();
//p0的x y坐标
path.moveto(p0.x,y);
path.rquadto(p1.x,p1.y,p2.x,p2.y);

这就是api调用方法是不是很简单?那么你又会问那么怎么画出来呢?
很简单在 dispatchdraw方法 或者ondraw中 调用

@override
protected void dispatchdraw(canvas canvas) {
super.dispatchdraw(canvas);
canvas.drawpath(path,paint);
}

那么你画出来的效果应该和在ps用钢笔画出来的差不多 ps中钢笔工具就是二次贝塞尔曲线

这里写图片描述 

(借用下图片)

如果你的三个点的位置如刚开的图片 p0 p1 p2 (p1在p0右上方并且 p1在p2左上方)一样那么在屏幕中的显示效果如下

这里写图片描述 

这里随扩张下dispatchdraw和ondraw的区别

如果你的自定义view 是继承view 那么 会先调用 ondraw->>dispatchdraw

如果你的自定义view 是继承viewgroup那么会跳过ondraw方法直接调用dispathchdraw这里特别注意!!我们这个案例中继承的是framelayout, 而framelayout又是继承自viewgroup所以….

那么我们回到主题 如何画一个u形文字?简单就是说按照我们画出来的曲线在上面写字 如: 文字是”csdn开源中国” 如何让这几个字贴着我们的曲线写出来?

这里介绍一个api

canvas.drawtextonpath()

这里写图片描述 

第一个参数:文字 类型为字符串

第二个参数:路径 也就是我们前面的二次贝塞尔曲线

第三个参数:沿着路径文字开始的位置 说白了偏移量

第四个参数:贴着路径的高度的偏移量

hoffset:

the distance along the path to add to the text's starting position

voffset:

the distance above(-) or below(+) the path to position the text
//ok我们看看他可以画出什么样子的文字

这里写图片描述

这种看大家对贝塞尔曲线的理解,你理解的越深那么你可以画出的图像越多,当然不一定要用贝塞尔曲线

确定贝塞尔曲线的起点

我们在回过头来看看我们的效果图

这里写图片描述

我们可以看到文字应该是在iv(弹跳的图片中央位置且正好在 iv弹到底部的位置)

这里我们先补充知识在考虑计算

我们来学习一下文字的测量我们来看幅图

这里写图片描述

我们调用画文字的api时

canvas.drawtextonpath或者canvas.drawtext 是从基准线开始画的也就是说途中的baseline开始画.

如:

canvas.drawtext(“fmy”,0,0,paint);

那么你将看不到文字 只能在屏幕看到文字底部如下图:

这里写图片描述

另一个同理api drawtextonpath 也是

再看看几个简单的api

1 . paint.measuretext(“fmy”);返回在此画笔paint下写fmy文字的宽度

下面的api会把文字的距离左边left 上边top 右边right 底部的bottom的值写入此矩形 那么

rect.right-rect.left=文字宽度 
rect.bottom-rect.top=文字高度
//矩形
rect rect = new rect();
//将文字画入矩形目的是为了测量高度
paint.gettextbounds(printtext, 0, printtext.length(), rect);

那么请看:

private void init() {
//初始化弹跳图片 控件
iv = new imageview(getcontext());
viewgroup.layoutparams params = new viewgroup.layoutparams(
viewgroup.layoutparams.wrap_content,
viewgroup.layoutparams.wrap_content);
iv.setlayoutparams(params);
iv.setimageresource(allimgdown[0]);
this.addview(iv);
//画笔的初始化
paint = new paint();
paint.setstrokewidth(1);
paint.setcolor(color.cyan);
paint.setstyle(paint.style.fill);
paint.settextsize(50);
paint.setantialias(true);
//矩形
rect rect = new rect();
//将文字画入矩形目的是为了测量高度
paint.gettextbounds(printtext, 0, printtext.length(), rect);
//文本宽度
textwidth = paint.measuretext(printtext);
//获得文字高度
textheight = rect.bottom - rect.top;
//初始化路径
path = new path();
iv.setx(getwidth()/2);
iv.measure(0,0);
iv.getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() {
@override
public void ongloballayout() {
if (!flagmeure)
{
flagmeure =true;
//由于画文字是由基准线开始
path.moveto(iv.getx()-textwidth/2+iv.getwidth()/2, textheight+iv.getheight()+downheight*iv.getheight());
//计算最大弹力
maxelasticfactor = (float) (textheight / elastic);
//初始化贝塞尔曲线
path.rquadto(textwidth / 2, 0, textwidth, 0);
//初始化上弹和下沉动画
myanmation();
iv.startanimation(translatedown);
}
}
});
}

我们现在写一个类当iv图片(弹跳图)碰到文字顶部时设置一个监听器 时间正好是弹图向上到顶部的时间 期间不断让文字凹陷在恢复正常

//用于播放文字下沉和上浮动画传入的数值必须是 图片下沉和上升的一次时间
public void initanimation(int duration){
//这里为什maxelasticfactor/4 好看...另一个同理 这个数值大家自行调整
valueanimator animator = valueanimator.offloat(maxelasticfactor/4, (float) (maxelasticfactor / 1.5),0);
animator.setduration(duration/2);
animator.addupdatelistener(new valueanimator.animatorupdatelistener() {
@override
public void onanimationupdate(valueanimator animation) {
calc();//重新画路径
nowelasticfactor= (float) animation.getanimatedvalue();
postinvalidate();
}
});
animator.start();
}

再来一个重新绘画路径计算的方法

public void calc(){
//重置路径
path.reset();
//由于画文字是由基准线开始
path.moveto(iv.getx()-textwidth/2+iv.getwidth()/2, textheight+iv.getheight()+downheight*iv.getheight());
//画二次贝塞尔曲线
path.rquadto(textwidth / 2, nowelasticfactor, textwidth, 0);
}

好了到这里我们看看完整源码吧:

package com.example.administrator.myapplication;
import android.animation.objectanimator;
import android.animation.valueanimator;
import android.content.context;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.paint;
import android.graphics.path;
import android.graphics.rect;
import android.util.attributeset;
import android.view.viewgroup;
import android.view.viewtreeobserver;
import android.view.animation.accelerateinterpolator;
import android.view.animation.animation;
import android.view.animation.decelerateinterpolator;
import android.view.animation.translateanimation;
import android.widget.framelayout;
import android.widget.imageview;
public class my extends framelayout {
//画笔
private paint paint;
//路径
private path path;
//要输入的文本
private string printtext = "正在加载";
//文本宽
private float textwidth;
//文本高
private float textheight;
//测量文字宽高的时候使用的矩形
private rect rect;
//最大弹力系数
private float elastic = 1.5f;
//最大弹力
private float maxelasticfactor;
//当前弹力
private float nowelasticfactor;
//用于记录当前图片使用数组中的哪张
int indeximgflag = 0;
//下沉图片
int allimgdown [] = {r.mipmap.p2,r.mipmap.p4,r.mipmap.p6,r.mipmap.p8};
//动画效果一次下沉或上弹的时间 animationduration*2=一次完整动画时间
int animationduration = 1000;
//弹起来的图片
imageview iv;
//图片下沉高度(即从最高点到最低点的距离)
int downheight = 2;
private animation translatedown;
private animation translateup;
private objectanimator rotation;
public my(context context) {
super(context);
}
public my(context context, attributeset attrs) {
super(context, attrs);
}
@override
protected void dispatchdraw(canvas canvas) {
super.dispatchdraw(canvas);
canvas.drawtextonpath(printtext, path, 0, 0, paint);
}
//用于播放文字下沉和上浮动画传入的数值必须是 图片下沉和上升的一次时间
public void initanimation(int duration){
//这里为什maxelasticfactor/4为什么
valueanimator animator = valueanimator.offloat(maxelasticfactor/4, (float) (maxelasticfactor / 1.5),0);
animator.setduration(duration/2);
animator.addupdatelistener(new valueanimator.animatorupdatelistener() {
@override
public void onanimationupdate(valueanimator animation) {
calc();
nowelasticfactor= (float) animation.getanimatedvalue();
postinvalidate();
}
});
animator.start();
}
public void calc(){
//重置路径
path.reset();
//由于画文字是由基准线开始
path.moveto(iv.getx()-textwidth/2+iv.getwidth()/2, textheight+iv.getheight()+downheight*iv.getheight());
//画二次贝塞尔曲线
path.rquadto(textwidth / 2, nowelasticfactor, textwidth, 0);
}
//初始化弹跳动画
public void myanmation(){
//下沉效果动画
translatedown = new translateanimation(
animation.relative_to_self,0,animation.relative_to_self,0,
animation.relative_to_self,0,animation.relative_to_self,downheight);
translatedown.setduration(animationduration);
//设置一个插值器 动画将会播放越来越快 模拟重力
translatedown.setinterpolator(new accelerateinterpolator());
//上弹动画
translateup = new translateanimation(
animation.relative_to_self,0,animation.relative_to_self,0,
animation.relative_to_self,downheight,animation.relative_to_self,0
);
translateup.setduration(animationduration);
////设置一个插值器 动画将会播放越来越慢 模拟反重力
translateup.setinterpolator(new decelerateinterpolator());
//当下沉动画完成时播放启动上弹
translatedown.setanimationlistener(new animation.animationlistener() {
@override
public void onanimationstart(animation animation) {
iv.setimageresource(allimgdown[indeximgflag]);
rotation = objectanimator.offloat(iv, "rotation", 180f, 360f);
rotation.setduration(1000);
rotation.start();
}
@override
public void onanimationend(animation animation) {
iv.startanimation(translateup);
initanimation(animationduration);
}
@override
public void onanimationrepeat(animation animation) {
}
});
//当上移动画完成时 播放下移动画
translateup.setanimationlistener(new animation.animationlistener() {
@override
public void onanimationstart(animation animation) {
indeximgflag = 1+indeximgflag>=allimgdown.length?0:1+indeximgflag;
iv.setimageresource(allimgdown[indeximgflag]);
rotation = objectanimator.offloat(iv, "rotation", 0.0f, 180f);
rotation.setduration(1000);
rotation.start();
}
@override
public void onanimationend(animation animation) {
//递归
iv.startanimation(translatedown);
}
@override
public void onanimationrepeat(animation animation) {
}
});
}
boolean flagmeure;
@override
protected void onsizechanged(int w, int h, int oldw, int oldh) {
super.onsizechanged(w, h, oldw, oldh);
init();
}
private void init() {
//初始化弹跳图片 控件
iv = new imageview(getcontext());
viewgroup.layoutparams params = new viewgroup.layoutparams(
viewgroup.layoutparams.wrap_content,
viewgroup.layoutparams.wrap_content);
iv.setlayoutparams(params);
iv.setimageresource(allimgdown[0]);
this.addview(iv);
//画笔的初始化
paint = new paint();
paint.setstrokewidth(1);
paint.setcolor(color.cyan);
paint.setstyle(paint.style.fill);
paint.settextsize(50);
paint.setantialias(true);
//矩形
rect = new rect();
//将文字画入矩形目的是为了测量高度
paint.gettextbounds(printtext, 0, printtext.length(), rect);
//文本宽度
textwidth = paint.measuretext(printtext);
//获得文字高度
textheight = rect.bottom - rect.top;
//初始化路径
path = new path();
iv.setx(getwidth()/2);
iv.measure(0,0);
iv.getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() {
@override
public void ongloballayout() {
if (!flagmeure)
{
flagmeure =true;
//由于画文字是由基准线开始
path.moveto(iv.getx()-textwidth/2+iv.getwidth()/2, textheight+iv.getheight()+downheight*iv.getheight());
//计算最大弹力
maxelasticfactor = (float) (textheight / elastic);
//初始化贝塞尔曲线
path.rquadto(textwidth / 2, 0, textwidth, 0);
//初始化上弹和下沉动画
myanmation();
iv.startanimation(translatedown);
}
}
});
}
}

小人奉上源码一封供大家 参考github源码下载地址

以上所述是小编给大家介绍的android自定义view实现阻尼效果的加载动画,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网