Android编程权威指南第三版 第32章

挑战练习内容

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

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

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

一、日落可逆

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

//上升动画
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)方法,但是我并未使用,我的代码如下

//变大
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加上边框间断线

<?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各一半。

<?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" />
    </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>

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

//--------------------------------倒影--------------------------------//
//透明度
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());

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

//太阳下降动画集
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,写得很好,在我学动画部分的时候,对我帮助很大。

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Android编程权威指南第4》是一本非常有价值的Android编程指南。该书由美国程序员Bill Phillips、Chris Stewart和Brian Hardy合著,对Android开发的方方面面进行了深入的解析和讲解。 这本书对于想要入门或提升自己在Android开发领域的开发者来说是一本理想的教材。它详细介绍了Android平台的基本原理、架构和开发工具,并通过实际案例和示例代码,讲解了如何使用Java语言和Android SDK进行开发,如何构建用户界面、处理用户输入、管理数据、调试和测试应用程序等等。 《Android编程权威指南第4》的内容涵盖了Android开发的方方面面,包括Activity、Fragment、Intent、Broadcast Receiver、Service、Content Provider等组件的使用,以及Android系统的UI设计、网络通信、数据存储、多媒体处理等相关技术。此外,该书还介绍了如何使用Android Studio进行项目管理和本控制,以及如何发布应用到Google Play商店。 该书语言简练、通俗易懂,结合了理论与实践,帮助读者从零开始深入学习Android开发。无论是初学者还是有一定经验的开发者,都可以通过阅读本书,了解到最新的Android开发技术和最佳实践。 总的来说,《Android编程权威指南第4》是一本对于想要学习和掌握Android开发的人来说必备的书籍,通过深入浅出的讲解和丰富的实例,帮助读者掌握Android开发的核心知识和技巧。无论是个人开发者还是企业开发团队,都可以通过阅读该书,提高自己在Android开发领域的能力和水平。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值