package com.example.animator; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Button but; private ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 but = (Button) findViewById(R.id.but); img = (ImageView) findViewById(R.id.img); //图片点击事件 img.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this, "我在这里", Toast.LENGTH_SHORT).show(); } }); //演示属性动画 but.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //创建平移动画 ObjectAnimator translationX = ObjectAnimator.ofFloat(img,"translationX",0,300); //创建透明动画 ObjectAnimator alpha = ObjectAnimator.ofFloat(img, "alpha", 1.0f, 0f); //动画集合 AnimatorSet set = new AnimatorSet(); //添加动画 set.play(translationX).with(alpha); //动画耗时 set.setDuration(5000); //开启 set.start(); //动画监听 set.addListener(new AnimatorListenerAdapter() { @Override//动画结束时 public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); } @Override public void onAnimationRepeat(Animator animation) { super.onAnimationRepeat(animation); } @Override//动画开始时 public void onAnimationStart(Animator animation) { super.onAnimationStart(animation); } }); } }); }}
--------------------------------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="com.example.animator.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="动画演示" android:id="@+id/but"/> <ImageView android:layout_marginTop="50dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/img" android:src="@mipmap/ic_launcher"/> </LinearLayout>
ObjectAnimator(属性动画)
最新推荐文章于 2022-04-27 15:37:06 发布