Android编程权威指南(第32章 属性动画)

1.建立一个名为Sunset的项目
在这里插入图片描述
2.在res/values下新建colors.xml并输入代码

#3F51B5
#303F9F
#FF4081

<color name="bright_sun">#fcfcb7</color>
<color name="blue_sky">#1e7ac7</color>
<color name="sunset_sky">#ec8100</color>
<color name="night_sky">#05192e</color>
<color name="sea">#224869</color>

在这里插入图片描述

3.在rawable下新建sun.xml并输入代码
shape xmlns:android=“http://schemas.android.com/apk/res/android”
android:shape=“oval”>


在这里插入图片描述
4.在res/layout下新建fragment_sunset.xml并输入代码



<View
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.39"
    android:background="@color/sea"
    />
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201210084753915.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d1bGl4aWFvaHVv,size_16,color_FFFFFF,t_70) 5.新建SunsetFragment并输入代码 public class SunsetFragment extends Fragment {
private View mSceneView;
private View mSunView;
private View mSkyView;

private int mBlueSkyColor;
private int mSunsetSkyColor;
private int mNightSkyColor;

public static SunsetFragment newInstance() {
    return new SunsetFragment();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_sunset, container, false);

    mSceneView = view;
    mSunView = view.findViewById(R.id.sun);
    mSkyView = view.findViewById(R.id.sky);

    Resources resources = getResources();
    mBlueSkyColor = resources.getColor(R.color.blue_sky);
    mSunsetSkyColor = resources.getColor(R.color.sunset_sky);
    mNightSkyColor = resources.getColor(R.color.night_sky);

    mSceneView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startAnimation();
        }
    });

    return view;
}

private void startAnimation() {
    float sunYStart = mSunView.getTop();
    float sunYEnd = mSkyView.getHeight();

    ObjectAnimator heightAnimator = ObjectAnimator
            .ofFloat(mSunView, "y", sunYStart, sunYEnd)
            .setDuration(3000);
    heightAnimator.setInterpolator(new AccelerateInterpolator());

    ObjectAnimator sunsetSkyAnimator = ObjectAnimator
            .ofInt(mSkyView, "backgroundColor", mBlueSkyColor, mSunsetSkyColor)
            .setDuration(3000);
    sunsetSkyAnimator.setEvaluator(new ArgbEvaluator());

    ObjectAnimator nightSkyAnimator = ObjectAnimator
            .ofInt(mSkyView, "backgroundColor", mSunsetSkyColor, mNightSkyColor)
            .setDuration(1500);
    nightSkyAnimator.setEvaluator(new ArgbEvaluator());

    AnimatorSet animatorSet = new AnimatorSet();
    animatorSet
            .play(heightAnimator)
            .with(sunsetSkyAnimator)
            .before(nightSkyAnimator);
    animatorSet.start();
}

}

在这里插入图片描述
6.新建SunsetActivity并输入代码
import android.support.v4.app.Fragment;

public class SunsetActivity extends SingleFragmentActivity {

@Override
protected Fragment createFragment() {
    return SunsetFragment.newInstance();
}

}

在这里插入图片描述
7.保存并运行
结果如图
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值