版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_35564145/article/details/90005345

1.首先,让日落可逆。也就是说,点击屏幕,等太阳落下后,再次点击屏幕,让太阳升起来。 动画集不能逆向执行,因此,你需要新建一个AnimatorSet。

2.第二个挑战是添加太阳动画特效,让它有规律地放大、缩小或是加一圈旋转的光线。(这实际是反复执行一段动画特效,可考虑使用ObjectAnimator的setRepeatCount(int)方法。) 另外,海面上要是有太阳的倒影就更真实了。

3.最后,再来看个颇具挑战的练习。在日落过程中实现动画反转。在太阳慢慢下落时点击屏幕, 让太阳无缝回升至原来所在的位置。或者,在太阳落下进入夜晚时点击屏幕,让太阳重新升回天 空,就像日出。

一、日落可逆

这部分比较简单,根据提示新建新的动画和动画集就可以,直接上代码

1
2
3
4
5
6
7
8
 //上升动画
sun_upAnimator = ObjectAnimator.ofFloat(mSunView, "y", sunYFirstEnd,sunYFirstStart).setDuration(3000);
sun_upAnimator.setInterpolator(new AccelerateInterpolator());
//上升动画集
sun_upAnimatorSet = new AnimatorSet();
sun_upAnimatorSet.play(sun_upAnimator)
.with(sunsetSkyAnimator)
.before(nightSkyAnimator);

二、太阳动画特效

这部分主要涉及缩放动画、旋转动画,提示使用ObjectAnimator的setRepeatCount(int)方法,但是我并未使用,我的代码如下

1
2
3
4
5
6
7
8
9
10
11
12
 //变大
bigXAnimator = ObjectAnimator.ofFloat(mSunView,"scaleX",1.0f,2.0f).setDuration(3000);
bigYAnimator = ObjectAnimator.ofFloat(mSunView,"scaleY",1.0f,2.0f).setDuration(3000);
//变小
smallXAnimator = ObjectAnimator.ofFloat(mSunView,"scaleX",2.0f,1.0f).setDuration(3000);
smallYAnimator = ObjectAnimator.ofFloat(mSunView,"scaleY",2.0f,1.0f).setDuration(3000); //旋转特效
//顺时针
clockwiseAnimator = ObjectAnimator.ofFloat(mSunView,"rotation",0f,720f).setDuration(3000);
//逆时针
anti_clockwiseAnimator = ObjectAnimator.ofFloat(mSunView,"rotation",360f,0f).setDuration(3000);

旋转动画特别说明:通过旋转sun来实现旋转的光线的效果,所以修改sun.xml给sun加上边框间断线

1
2
3
4
5
6
7
8
9
10
11
 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android = "http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/bright_sun"/> <stroke
android:width="2dp"
android:color="@color/huawei_red"
android:dashWidth="25dp"
android:dashGap="8dp"/>
</shape>

以上动画直接.with()加到动画集即可。

One more thing:

为了让太阳更真实,添加倒影效果

①在布局文件中,再添加一个FrameLayout,记得android:layout_weight调成0.5,这样sky和sea各一半。

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
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <FrameLayout
android:id="@+id/sky"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:background="@color/blue_sky">
<ImageView
android:id="@+id/sun"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:src="@drawable/sun"
android:contentDescription="@string/todo" />
&l 大专栏  Android编程权威指南第三版 第32章t;/FrameLayout> <FrameLayout
android:id="@+id/sea"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:background="@color/sea">
<ImageView
android:id="@+id/little_sun"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:src="@drawable/sun"
android:contentDescription="@string/todo"
android:alpha="0.7"/>
</FrameLayout> </LinearLayout>

②倒影需要有位移动画、缩放动画、旋转动画、渐变透明动画。代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 //--------------------------------倒影--------------------------------//
//透明度
alphaAnimator = ObjectAnimator.ofFloat(mLittleSunView, "alpha", 0.7f, 0.0f).setDuration(3000);
unalphaAnimator = ObjectAnimator.ofFloat(mLittleSunView,"alpha",0.0f,0.7f).setDuration(3000);
//大小
little_bigXAnimator = ObjectAnimator.ofFloat(mLittleSunView,"scaleX",1.0f,2.0f).setDuration(3000);
little_bigYAnimator = ObjectAnimator.ofFloat(mLittleSunView,"scaleY",1.0f,2.0f).setDuration(3000);
little_smallXAnimator = ObjectAnimator.ofFloat(mLittleSunView,"scaleX",2.0f,1.0f).setDuration(3000);
little_smallYAnimator = ObjectAnimator.ofFloat(mLittleSunView,"scaleY",2.0f,1.0f).setDuration(3000);
//旋转特效
little_clockwiseAnimator = ObjectAnimator.ofFloat(mLittleSunView,"rotation",0f,720f).setDuration(3000);
little_anti_clockwiseAnimator = ObjectAnimator.ofFloat(mLittleSunView,"rotation",360f,0f).setDuration(3000);
//位移
little_upAnimator = ObjectAnimator.ofFloat(mLittleSunView, "y", littlesunYFirstStart, littlesunYFirstEnd).setDuration(3000);
little_upAnimator.setInterpolator(new AccelerateInterpolator());
little_downAnimator = ObjectAnimator.ofFloat(mLittleSunView, "y", littlesunYFirstEnd, littlesunYFirstStart).setDuration(3000);
little_downAnimator.setInterpolator(new AccelerateInterpolator());

分别定义太阳下降、太阳上升、倒影下降、倒影上升动画集,再合并为上升、下降总动画集

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
 //太阳下降动画集
sun_downAnimatorSet = new AnimatorSet();
sun_downAnimatorSet.play(sun_downAnimator)
.with(sunsetSkyAnimator)
.with(bigXAnimator).with(bigYAnimator)
.with(clockwiseAnimator)
.before(nightSkyAnimator);
//太阳上升动画集
sun_upAnimatorSet = new AnimatorSet();
sun_upAnimatorSet.play(sun_upAnimator)
.with(sunsetSkyAnimator)
.with(smallXAnimator).with(smallYAnimator)
.with(anti_clockwiseAnimator)
.before(nightSkyAnimator);
//倒影上升动画集、
little_upAnimatorSet = new AnimatorSet();
little_upAnimatorSet
.play(little_anti_clockwiseAnimator)
.with(little_bigXAnimator).with(little_bigYAnimator)
.with(alphaAnimator)
.with(little_upAnimator);
//倒影下降动画集
little_downAnimatorSet = new AnimatorSet();
little_downAnimatorSet
.play(little_clockwiseAnimator)
.with(little_smallXAnimator).with(little_smallYAnimator)
.with(little_downAnimator)
.with(unalphaAnimator); //总动画集
downAnimatorSet = new AnimatorSet();
downAnimatorSet.play(sun_downAnimatorSet).with(little_upAnimatorSet);
upAnimatorSet = new AnimatorSet();
upAnimatorSet.play(sun_upAnimatorSet).with(little_downAnimatorSet);

三、日落动画反转

目前暂时还没做。

推荐文章

关于Android动画用法,推荐这篇https://blog.csdn.net/qq_40881680/article/details/82378391,写得很好,在我学动画部分的时候,对我帮助很大。

Android编程权威指南第三版 第32章的更多相关文章

  1. Android编程权威指南(第三版)- 2.8 挑战练习:添加后退按钮

    package com.example.geoquiz; import android.support.v7.app.AppCompatActivity; import android.os.Bund ...

  2. 使用最新AndroidStudio编写Android编程权威指南(第3版)中的代码会遇到的一些问题

    Android编程权威指南(第3版)这本书是基于Android7.0的,到如今已经过于古老,最新的Android版本已经到10,而这本书的第四版目前还没有正式发售,在最近阅读这本书时,我发现这本书的部 ...

  3. Android编程权威指南(第2版)--第16章 使用intent拍照 挑战练习

    16.7挑战练习:优化照片显示 新建dialog_photo.xml 1234567891011121314 <?xml version="1.0" encoding=&qu ...

  4. 读《Android编程权威指南》

    因为去年双十二购买了一折的<Android 编程权威指南(第一版)>,在第二版出来后图灵社区给我推送了第二版的优惠码,激动之余就立马下单购买电子书,不得不说Big Nerd Ranch G ...

  5. 《Android编程权威指南》

    <Android编程权威指南> 基本信息 原书名:Android programming: the big nerd ranch guide 原出版社: Big Nerd Ranch Gu ...

  6. 《Android编程权威指南》CriminalIntent项目梳理

    相信很多新手或者初级开发人员都已经买了第2版的<Android编程权威指南>, 这本书基于Android Studio开发,对入门人员来说是很好的选择,但是很可惜的是, 在完成一个项目后, ...

  7. 《Android编程权威指南》PhotoGallery应用梳理

    PhotoGalley是<Android编程权威指南>书中另外一个重要的应用.       

  8. Swift编程权威指南第2版 读后收获

    自从参加工作一直在用OC做iOS开发.在2015年的时候苹果刚推出swift1.0不久,当时毕竟是新推出的语言,大家也都很有激情的学习.不过在学完后发现很难在实际项目中使用,再加上当时公司项目都是基于 ...

  9. Android编程权威指南笔记2:解决R文件爆红问题和SDK概念

    在android studio中会遇到R文件的丢失,所以遇见这问题怎么解决呢? 重新检查资源文件中xml文件 最近一次编译时如果未生成R.java文件,项目中资源引用的地方都会出错.通常,这是某个xm ...

随机推荐

  1. 谷歌为何要研发新系统在5年内取代Android?

    现在的Android系统已经越做越好,体验也愈来愈佳,是唯一能和iOS掰腕子的移动操作系统.而且对于很多智能手机厂商来说,开源的Android为它们节约了太多成本,是不可或缺的基石之一.因此,想必很多 ...

  2. Cannot read property 'XXXX' of null/undifined

    这个问题可能的原因有很多 1.如果你的js直接写在自执行函数或者head标签内的script里面,那么可以检查一下你的代码有没有用到页面里的节点,因为这样写的代码在页面加载完成之前就会开始执行,如果有 ...

  3. 7.windows-oracle实战第七课 --约束、索引

    数据的完整性 数据的完整性用于确保数据库数据遵从一定的商业和逻辑规则.数据的完整性使用约束.触发器.函数的方法来实现.在这三个方法中,约束易于维护,具备最好的性能,所以作为首选.  约束:not nu ...

  4. docker安装(centos-7)

    centos7安装docker:Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker .通过 uname -r 命令 ...

  5. British postal system to launch parcel postboxes

    1 单词 parcel n. 包裹 pilot n. 试行计划 2 句子 1400 of the new boxes will be installed at 30 locations across ...

  6. tensorflow(四)

    tensorflow数据处理方法, 1.输入数据集 小数据集,可一次性加载到内存处理. 大数据集,一般由大量数据文件组成,因为数据集的规模太大,无法一次性加载到内存,只能每一步训练时加载数据,可以采用 ...

  7. 广义线性模型|logistics|Odds ratio|最大似然函数|LR|AIC|

    广义线性模型 y是分类变量 Link function:将分类变量和数值变量放在一起 使用得到结果0 or 1的概率值来评估选0 or1 函数关系: 正比例函数: logistics函数S型曲线: O ...

  8. BZOJ2733 [HNOI2012]永无乡(并查集+线段树合并)

    题目大意: 在$n$个带权点上维护两个操作: 1)在点$u,v$间连一条边: 2)询问点$u$所在联通块中权值第$k$小的点的编号,若该联通块中的点的数目小于$k$,则输出$-1$: 传送门 上周的模 ...

  9. Rikka with Prefix Sum

    Rikka with Prefix Sum 题目 https://www.nowcoder.com/acm/contest/148/D 题目有三个操作 l到r都添加一个数 取一次前缀和 查询区间和 这 ...

  10. 十三、linux-mysql的mysql的核心优化思想

    一.数据库运维管理思想核心 1.未雨绸缪,不要停留在制度上,而是要实际做出来 2.亡羊补牢,举一反三,不要好了伤疤忘了疼 3.完善的框架设计及备份.恢复策略 4.定期思考,并实战模拟以上策略演练 二. ...