属性动画的两种实现方法——代码实现、xml实现

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textColor="#000"
        android:textSize="48sp" />

    <Button
        android:id="@+id/btn_showAnimator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示动画" />
</LinearLayout>

**

MainActivity

**

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_showAnimator)
    Button btnShowAnimator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

    }

    @SuppressLint("ObjectAnimatorBinding")
    @OnClick(R.id.btn_showAnimator)
    public void onViewClicked() {
    	//第一种  xml文件实现动画效果
        //给控件设置属性动画
        /*Animator animator = AnimatorInflater.loadAnimator(this, R.animator.textview_animator);
        animator.setTarget(tvTitle);
        animator.start();*/
        //这种方法不太推荐
        //tvTitle.animate().translationX(300f);
        //第二种  代码实现动画效果
        //推荐以下方式
        ObjectAnimator animator = ObjectAnimator.ofFloat(tvTitle, "rotationX", 0, 200);
        animator.setInterpolator(new AccelerateInterpolator());
        animator.setDuration(2000);
        animator.start();

    }
}

**

xml文件、textview_animator

**

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:duration="2000"
        android:propertyName="textColor"
        android:valueFrom="@color/green"
        android:valueTo="@color/yellow"></objectAnimator>

    <objectAnimator
        android:duration="2000"
        android:propertyName="translationX"
        android:valueFrom="0"
        android:valueTo="300"></objectAnimator>

    <objectAnimator
        android:duration="2000"
        android:propertyName="translationY"
        android:valueFrom="0"
        android:valueTo="300"></objectAnimator>

    <objectAnimator
        android:duration="2000"
        android:propertyName="rotationY"
        android:valueFrom="0"
        android:valueTo="360"></objectAnimator>

    <objectAnimator
        android:duration="2000"
        android:propertyName="scaleX"
        android:valueFrom="0"
        android:valueTo="2"></objectAnimator>


    <objectAnimator
        android:duration="2000"
        android:propertyName="alpha"
        android:valueFrom="0.1"
        android:valueTo="0.8"></objectAnimator>
</set>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值