当前位置: 首页 > news >正文

廊坊网站制作建设网站搭建教程

廊坊网站制作建设,网站搭建教程,泉州哪里做网站,国内知名的网站设计公司View 动画相关内容可参考官网 动画资源 此前也有写 View 动画相关的内容,但都只是记录代码,没有特别分析。以此篇作为汇总、整理、分析。 Android View 动画有4中,分别是 平移动画 TranslateAnimation缩放动画 ScaleAnimation旋转动画 Rot…

View 动画相关内容可参考官网 动画资源

此前也有写 View 动画相关的内容,但都只是记录代码,没有特别分析。以此篇作为汇总、整理、分析。

Android View 动画有4中,分别是

  • 平移动画 TranslateAnimation
  • 缩放动画 ScaleAnimation
  • 旋转动画 RotateAnimation
  • 透明度动画 AlphaAnimation

View 动画可以单独使用,也可以一起使用,一起使用就叫复合动画。

实现 View 动画有 2 种方式,java 方式和 xml 方式。

Demo 图说明,动画作用在图片上。图片显示在左上角,图片右侧和下方的线是为了方便看出 View 动画前后的位置、大小对比,无实际作用。
在这里插入图片描述

平移动画 TranslateAnimation

对 View 做水平或者竖直方向上的移动。

java 方式

import android.view.animation.TranslateAnimation;TranslateAnimation translateAnimation = new TranslateAnimation(0,imageView.getWidth(),0,0);
translateAnimation.setDuration(3000);//动画时长
translateAnimation.setFillAfter(true);//true :view停留在动画结束的地方。false :动画结束后 view 返回原位
imageView.startAnimation(translateAnimation);

xml 方式

用 AS 可以很方便地创建 anim 目录和动画文件。
在这里插入图片描述

在 res/anim/view_anim_translate.xml 创建文件,写入

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" ><!--android:fromXDeltaFloat or percentage. 移动起始点的x坐标. 表示形式有三种:1 相对于自己的左边界的距离,单位像素值。(例如 "5")2 相对于自己的左边界的距离与自身宽度的百分比。(例如  "5%")3 相对于父View的左边界的距离与父View宽度的百分比。(例如 "5%p")android:toXDeltaFloat or percentage. 移动结束点的x坐标. 表现形式同上android:fromYDeltaFloat or percentage. 移动起始点的y坐标. 表示形式有三种:1 相对于自己的上边界的距离,单位像素值。(例如 "5")2 相对于自己的上边界的距离与自身高度的百分比。(例如  "5%")3 相对于父View的上边界的距离与父View高度的百分比。(例如 "5%p")android:toYDeltaFloat or percentage. 移动结束点的y坐标. 表现形式同上--><translateandroid:duration="3000"android:fromXDelta="0"android:fromYDelta="0"android:toXDelta="800"android:toYDelta="0"/>
</set>

然后通过 AnimationUtils 实现,

Animation animTran = AnimationUtils.loadAnimation(ViewAnimationActivity.this, R.anim.view_anim_translate);
animTran.setFillAfter(true);
imageView.startAnimation(animTran);

实现效果:View 向右平移自身宽度的距离。
在这里插入图片描述

缩放动画 ScaleAnimation

对 View 进行缩放操作。

java 方式

放大为 1.5 倍用 1.5f ,缩小为 0.5 倍用 0.5f ,很好理解。

import android.view.animation.AccelerateInterpolator;
import android.view.animation.ScaleAnimation;ScaleAnimation scaleAnimation = new ScaleAnimation(1 ,1.5f ,1 ,1.5f);// X轴 Y轴都放大 1.5 倍
scaleAnimation.setDuration(3000);
scaleAnimation.setFillAfter(true);
scaleAnimation.setInterpolator(new AccelerateInterpolator());// 设置动画插值起,用来控制动画速度,这是加速插值器,动画速度会越来越快
imageView.startAnimation(scaleAnimation);

方法 public ScaleAnimation(float fromX, float toX, float fromY, float toY) {} 默认缩放总中心是 (0,0) ,即 View 的左上角。
方法 public ScaleAnimation(float fromX, float toX, float fromY, float toY,float pivotX, float pivotY) {} 最后两个参数指定缩放中心点。

缩放中心点不同,缩放效果是不同的。
如,
效果1 ,new ScaleAnimation(1 ,0.5f ,1 , 0.5f);
在这里插入图片描述

效果2,new ScaleAnimation(1 ,0.5f ,1 ,0.5f, (float) 200, (float) 100);
在这里插入图片描述
效果3,new ScaleAnimation(1 ,0.5f ,1 ,0.5f, (float) imageView.getWidth(), (float) imageView.getHeight());
在这里插入图片描述

xml 方式

创建 res/anim/view_anim_translate.xml 文件,

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" ><!--android:fromXScaleFloat. 水平方向缩放比例的初始值,1.0是没有变化。android:toXScaleFloat. 水平方向缩放比例的结束值,1.0是没有变化。android:fromYScaleFloat. 竖直方向缩放比例的初始值,1.0是没有变化。android:toYScaleFloat. 竖直方向缩放比例的结束值,1.0是没有变化。android:pivotXFloat. 缩放中心点的x坐标android:pivotYFloat. 缩放中心点的y坐标--><scaleandroid:duration="3000"android:fromXScale="1.0"android:fromYScale="1.0"android:interpolator="@android:anim/accelerate_interpolator"android:pivotX="50%"android:pivotY="50%"android:toXScale="1.5"android:toYScale="1.5" />
</set>

然后通过 AnimationUtils 实现,

Animation animScale = AnimationUtils.loadAnimation(ViewAnimationActivity.this, R.anim.view_anim_scale);
animScale.setFillAfter(true);
imageView.startAnimation(animScale);

实现效果:View 在竖直和水平方向上都放大 1.5 倍。
在这里插入图片描述

旋转动画 RotateAnimation

对 View 进行旋转。

默认以 View 的左上角为中心点旋转,也可以指定旋转中心的坐标。旋转中心的坐标不同,旋转效果也是不一样的。

java 方式

import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.ScaleAnimation;RotateAnimation rotateAnimation = new RotateAnimation(0,180, imageView.getWidth(), imageView.getHeight());
rotateAnimation.setFillAfter(true);
rotateAnimation.setDuration(3000);
rotateAnimation.setRepeatCount(1);//重复1次
rotateAnimation.setInterpolator(new AccelerateDecelerateInterpolator());// 设置动画插值起,用来控制动画速度,这是加速减速插值器,动画速度先快后慢
imageView.startAnimation(rotateAnimation);

xml 方式

创建 res/anim/view_anim_rotate.xml 文件,

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" ><!--android:fromDegreesFloat. 旋转初始的角度。android:toDegreesFloat. 旋转结束的角度。android:pivotXFloat or percentage. 旋转中心点x坐标,表示形式有三种:1 相对于自己的左边界的距离,单位像素值。(例如 "5")2 相对于自己的左边界的距离与自身宽度的百分比。(例如 "5%")3 相对于父View的左边界的距离与父View宽度的百分比。(例如 "5%p")android:pivotYFloat or percentage. 旋转中心点y坐标,表示形式有三种:1 相对于自己的上边界的距离,单位像素值。(例如 "5")2 相对于自己的上边界的距离与自身宽度的百分比。(例如 "5%")3 相对于父View的上边界的距离与父View高度的百分比。(例如 "5%p")--><rotateandroid:duration="2000"android:fromDegrees="0"android:interpolator="@android:anim/accelerate_decelerate_interpolator"android:pivotX="100%"android:pivotY="100%"android:toDegrees="+180"android:repeatCount="1" />
</set>

然后通过 AnimationUtils 实现,

Animation animRotate = AnimationUtils.loadAnimation(ViewAnimationActivity.this, R.anim.view_anim_rotate);
animRotate.setFillAfter(false);
imageView.startAnimation(animRotate);

实现效果:绕着 View 的右下角旋转 180° ,重复1次。
在这里插入图片描述

透明度动画 AlphaAnimation

对 View 进行透明度操作。

java 方式

import android.view.animation.AlphaAnimation;AlphaAnimation alphaAnim = new AlphaAnimation(1f, 0);
alphaAnim.setDuration(2000);
alphaAnim.setFillAfter(true);
imageView.startAnimation(alphaAnim);

xml 方式

创建 res/anim/view_anim_alpha.xml 文件,

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><!--android:fromAlphaFloat. 设置透明度的初始值,其中0.0是透明,1.0是不透明的。android:toAlphaFloat. 设置透明度的结束值,其中0.0是透明,1.0是不透明的。--><alphaandroid:duration="2000"android:fromAlpha="1.0"android:toAlpha="0.0" />
</set>

然后通过 AnimationUtils 实现,

Animation alphaAnimation = AnimationUtils.loadAnimation(ViewAnimationActivity.this, R.anim.view_anim_alpha);
alphaAnimation.setFillAfter(true);
imageView.startAnimation(alphaAnimation);

实现效果:View 变为不透明。
在这里插入图片描述

复合动画

复合动画就是把 View 动画结合使用。也是通过 java 方式和 xml 方式实现。

平移+透明度

import android.view.animation.AnimationSet;AnimationSet set1 = new AnimationSet(true);
TranslateAnimation tAnimation1 = new TranslateAnimation(0,imageView.getWidth(),0,0);
AlphaAnimation aAnim1 = new AlphaAnimation(1f, 0.2f);set1.addAnimation(tAnimation1);
set1.addAnimation(aAnim1);
set1.setDuration(2500);
set1.setFillAfter(true);
imageView.startAnimation(set1);

实现效果:View 向右平移自身宽度的距离,透明度逐渐变为 0.2f 。
在这里插入图片描述

平移+缩放

AnimationSet set2 = new AnimationSet(true);
TranslateAnimation tAnimation2 = new TranslateAnimation(0,(float) imageView.getWidth()/4,0,(float) imageView.getHeight()/8);ScaleAnimation sAnimation2 = new ScaleAnimation(1 ,1.5f ,1 ,1.5f);set2.addAnimation(tAnimation2);
set2.addAnimation(sAnimation2);
set2.setDuration(2500);
set2.setFillAfter(true);
imageView.startAnimation(set2);

实现效果:View 向右平移自身宽度 1/4 的距离,向下平移自身高度 1/8 的距离,放大 1.5 倍 。
在这里插入图片描述

平移+旋转

AnimationSet set3 = new AnimationSet(true);
TranslateAnimation tAnimation3 = new TranslateAnimation(0,imageView.getWidth(),0,imageView.getHeight());
RotateAnimation rAnimation3 = new RotateAnimation(0,360, imageView.getWidth(), imageView.getHeight());set3.addAnimation(tAnimation3);
set3.addAnimation(rAnimation3);
set3.setDuration(2500);
set3.setFillAfter(true);
imageView.startAnimation(set3);

实现效果:View 向右平移自身宽度的距离,向下平移自身高度的距离,绕着右下角旋转 360°
在这里插入图片描述

平移+旋转+透明度

java 方式

AnimationSet set4 = new AnimationSet(false);
RotateAnimation rAnimation4 = new RotateAnimation(0,720, (float) imageView.getWidth()/2, (float) imageView.getHeight()/2);
rAnimation4.setInterpolator(new AccelerateDecelerateInterpolator());
rAnimation4.setDuration(3500);AlphaAnimation aAnim4 = new AlphaAnimation(1f, 0.5f);
aAnim4.setInterpolator(new AccelerateInterpolator());
aAnim4.setDuration(3000);ScaleAnimation sAnimation4 = new ScaleAnimation(1 ,0.5f ,1 ,0.5f, (float) imageView.getWidth()/2, (float) imageView.getHeight()/2);
sAnimation4.setInterpolator(new LinearInterpolator());
sAnimation4.setDuration(2500);set4.addAnimation(rAnimation4);
set4.addAnimation(aAnim4);
set4.addAnimation(sAnimation4);
set4.setFillAfter(true);
set4.setStartOffset(500);
mageView.startAnimation(set4);

xml 方式

创建 res/anim/view_anim_set4.xml 文件,

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"android:shareInterpolator="false"><rotateandroid:duration="3500"android:fromDegrees="0"android:interpolator="@android:anim/accelerate_decelerate_interpolator"android:pivotX="50%"android:pivotY="50%"android:toDegrees="720" /><alphaandroid:duration="3000"android:fromAlpha="1.0"android:interpolator="@android:anim/accelerate_interpolator"android:toAlpha="0.5" /><scaleandroid:duration="2500"android:fromXScale="1"android:fromYScale="1"android:interpolator="@android:anim/linear_interpolator"android:pivotX="50%"android:pivotY="50%"android:toXScale="0.5"android:toYScale="0.5" />
</set>

然后通过 AnimationUtils 实现,

Animation anim4 = AnimationUtils.loadAnimation(ViewAnimationActivity.this, R.anim.view_anim_set4);
anim4.setFillAfter(true);
imageView.startAnimation(anim4);

实现效果:以 View 的中心旋转 720° ,缩小为 0.5 倍 ,透明度逐渐变为 0.5f 。
在这里插入图片描述

监听动画过程

使用 Animation.setAnimationListener(AnimationListener listener) 方法即可监听动画的开始、结束、重复等过程。

Animation anim4 = AnimationUtils.loadAnimation(ViewAnimationActivity.this, R.anim.view_anim_set4);anim4.setFillAfter(true);anim4.setAnimationListener(new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {}@Overridepublic void onAnimationRepeat(Animation animation) {}});imageView.startAnimation(anim4);

对点击事件的影响

View 动画对点击事件无影响。

即 View 的大小、位置变化后,点击 View 的原始位置,点击事件还是有的。

如果动画使 View 超出了原始位置,超出的部分,是无法响应点击事件的。

插值器

插值器用来控制动画的执行速度。

通过 Animation.setInterpolator(Interpolator i) 方法可以设置动画的插值器,

也可以用 xml 的方式,

<scaleandroid:interpolator="@android:anim/linear_interpolator"/>

系统预设的插值器有:

插值器说明
AccelerateDecelerateInterpolator开始和结束速度慢,中间加速
AccelerateInterpolator开始慢,逐渐加速
AnticipateInterpolator先反方向执行动画再正方向执行
AnticipateOvershootInterpolatorAnticipateInterpolator 结合 OvershootInterpolator ,先反方向执行动画再正方向执行,到动画终点后继续前进一段距离然后回到动画终点
BounceInterpolator自由落体,到终点后回弹几下再回到终点,类似乒乓球落地效果
CycleInterpolator正弦曲线,先正方向执行动画再反方向执行动画
DecelerateInterpolator开始快,结束慢
LinearInterpolator线性、匀速
OvershootInterpolator动画终点后继续前进一段距离然后回到动画终点
PathInterpolator通过它的构造函数实现贝塞尔曲线
LinearOutSlowInInterpolatorandroidx里的,速度曲线用的贝塞尔曲线,开始速度快,逐渐减速,类似 DecelerateInterpolator 但速度更快
FastOutLinearInInterpolatorandroidx里的,,速度曲线用的贝塞尔曲线,逐渐加速,类似 AccelerateInterpolator 但速度更快
FastOutSlowInInterpolatorandroidx里的,速度曲线用的贝塞尔曲线,先加速再减速,类似 AccelerateDecelerateInterpolator 但速度更快

这些插值器基本满足了日常需求。

效果对比:
在这里插入图片描述
除了 CycleInterpolator ,绿色线是所有动画的终点。

效果对比2:
在这里插入图片描述

自定义插值器

自定义插值器,

继承 BaseInterpolator ,重写 getInterpolation(float input) 方法,

相关继承关系为 BaseInterpolator >> Interpolator >> TimeInterpolator ,

package android.animation;/*** A time interpolator defines the rate of change of an animation. This allows animations* to have non-linear motion, such as acceleration and deceleration.*/
public interface TimeInterpolator {/*** Maps a value representing the elapsed fraction of an animation to a value that represents* the interpolated fraction. This interpolated value is then multiplied by the change in* value of an animation to derive the animated value at the current elapsed animation time.** @param input A value between 0 and 1.0 indicating our current point*        in the animation where 0 represents the start and 1.0 represents*        the end* @return The interpolation value. This value can be more than 1.0 for*         interpolators which overshoot their targets, or less than 0 for*         interpolators that undershoot their targets.*/float getInterpolation(float input);
}

input 取值是从 0.0 到 1.0 ,分别表示动画的开始和结束。

看系统插值器的实现,都涉及到数学公式~~

本例的效果和 LinearInterpolator 是一样的,因为抄的 LinearInterpolator 的实现。 😄

import android.view.animation.BaseInterpolator;public class MyInterpolator extends BaseInterpolator {@Overridepublic float getInterpolation(float input) {return input;}
}

return input/2return input*1.1f 的结果如下。
在这里插入图片描述
在这里插入图片描述


文章转载自:
http://epibenthos.c7495.cn
http://comparably.c7495.cn
http://greenwinged.c7495.cn
http://pekalongan.c7495.cn
http://maturity.c7495.cn
http://lorelei.c7495.cn
http://circumstance.c7495.cn
http://worldliness.c7495.cn
http://silundum.c7495.cn
http://fda.c7495.cn
http://fqdn.c7495.cn
http://aglint.c7495.cn
http://edestin.c7495.cn
http://mailbox.c7495.cn
http://sapiential.c7495.cn
http://contranatant.c7495.cn
http://atomiser.c7495.cn
http://wellsian.c7495.cn
http://gusher.c7495.cn
http://pyrgeometer.c7495.cn
http://croslet.c7495.cn
http://leafless.c7495.cn
http://saida.c7495.cn
http://rearhorse.c7495.cn
http://cystocarp.c7495.cn
http://enrapt.c7495.cn
http://tor.c7495.cn
http://fingered.c7495.cn
http://carrousel.c7495.cn
http://fervid.c7495.cn
http://depletory.c7495.cn
http://complanation.c7495.cn
http://topstitch.c7495.cn
http://hysterical.c7495.cn
http://losel.c7495.cn
http://idol.c7495.cn
http://sarcology.c7495.cn
http://endospore.c7495.cn
http://capsize.c7495.cn
http://homological.c7495.cn
http://reblossom.c7495.cn
http://blessedness.c7495.cn
http://brazilein.c7495.cn
http://polypectomy.c7495.cn
http://laboring.c7495.cn
http://populace.c7495.cn
http://obeah.c7495.cn
http://calumnious.c7495.cn
http://annulation.c7495.cn
http://anthropic.c7495.cn
http://piranesi.c7495.cn
http://mobbism.c7495.cn
http://cynosure.c7495.cn
http://affable.c7495.cn
http://enterable.c7495.cn
http://lunchtime.c7495.cn
http://myasthenia.c7495.cn
http://coordinative.c7495.cn
http://desalinize.c7495.cn
http://aminate.c7495.cn
http://disyllable.c7495.cn
http://streamline.c7495.cn
http://kashrut.c7495.cn
http://drillstock.c7495.cn
http://spittoon.c7495.cn
http://begohm.c7495.cn
http://anatase.c7495.cn
http://leafage.c7495.cn
http://teenage.c7495.cn
http://triumvirate.c7495.cn
http://grogram.c7495.cn
http://coulter.c7495.cn
http://senusi.c7495.cn
http://sidra.c7495.cn
http://nonlethal.c7495.cn
http://paleolatitude.c7495.cn
http://fissiped.c7495.cn
http://msgm.c7495.cn
http://matriliny.c7495.cn
http://monbazillac.c7495.cn
http://chaulmoogra.c7495.cn
http://bonza.c7495.cn
http://bryozoan.c7495.cn
http://alcoranist.c7495.cn
http://pastorage.c7495.cn
http://tribunism.c7495.cn
http://carsickness.c7495.cn
http://caff.c7495.cn
http://reprove.c7495.cn
http://hippomenes.c7495.cn
http://desired.c7495.cn
http://grolier.c7495.cn
http://sceptical.c7495.cn
http://inaudibly.c7495.cn
http://accomplishable.c7495.cn
http://shakeable.c7495.cn
http://kerfuffle.c7495.cn
http://member.c7495.cn
http://pan.c7495.cn
http://coltsfoot.c7495.cn
http://www.zhongyajixie.com/news/95228.html

相关文章:

  • 生物网站建设百度公司的企业文化
  • 网站做端口是什么情况网站优化排名方法
  • 三明交通建设集团网站seo中国
  • 给网站做seo诊断搜索引擎优化要考虑哪些方面
  • 做网站设计怎么样数据分析网
  • 网站和app开发百度云盘搜索
  • 做篮球网站用的背景图片上海建站seo
  • 彩票网站代理营销管理培训课程培训班
  • 新手学做网站内容刷链接浏览量网站
  • 保真的二手手表网站电商平台怎么注册
  • 建设银行新疆分行网站济源网络推广
  • 软件开发需要多少钱?seo是什么姓氏
  • 网站开发 平台网络推广求职招聘交流群
  • 拟定建设方案物流网站免费seo培训
  • 做白日梦的哪个网站优化大师app
  • 微信推广和网站推广哪个好平台优化
  • 怎么查看一个网站的浏览量泉州百度竞价推广
  • 网站模板的好处深圳竞价托管公司
  • 网站域名优势广告推广平台
  • 织梦转wordpress插件下载地址windows优化大师在哪里
  • 全国企业信用信息公示系统河南seo全网优化推广
  • 如东做网站2023年9月疫情又开始了吗
  • 海南注册公司税收优惠政策兰州seo优化入门
  • wordpress cms 模板制作沈阳网站seo公司
  • 如何做网站服务器seo快速培训
  • 公司做网站的费用入账神秘网站
  • 网站建设趋势2017河南seo排名
  • 网站定制公司推荐谷歌的推广是怎么样的推广
  • 淘宝网站怎么做适配杭州seo营销
  • 遵义做网站 0852t最近时事热点新闻评论及点评