今天來講一下Android的動畫,一般動畫的操作有多種,平移,單轉,旋轉,漸變等等,今天主要通過代碼的注釋來講解一下,後期還會補充,慢慢更新。
屬性動畫
ValueAnimator
ObjectAnimator.ofFloat
1.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"
android:padding="15dp"
tools:context=".ObjectAnimatorActivity">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="15sp"
android:gravity="center"
android:text="動畫測試"
android:textColor="#FFFFFF"
android:background="#000000"/>
</LinearLayout>
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2csUTU65EMJRlTzkkeYhnRzwEMW1mY1RzRapnTtxkb5ckYplTeMZTTINGMShUYfRHelRHLwEzX39GZhh2css2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xyayFWbyVGdhd3LcV2Zh1Wa9M3clN2byBXLzN3btg3Pn5GcugjM1EjM1EjMyEDOwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
(2)activity代碼
package com.example.scrollview;
import androidx.appcompat.app.AppCompatActivity;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class ObjectAnimatorActivity extends AppCompatActivity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_object_animator);
tv=findViewById(R.id.tv);
tv.animate().translationYBy(500).setDuration(2000).start();
//沿Y軸在2s内平移500個機關
tv.animate().alpha(0).setDuration(2000).start();
//2秒鐘的漸變,從有到無
// ValueAnimator valueAnimator=ValueAnimator.ofInt(0,100);
// valueAnimator.setDuration(2000);
// valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
// @Override
// public void onAnimationUpdate(ValueAnimator valueAnimator) {
// //valueAnimator實際的值
// Log.d("aaaaa",valueAnimator.getAnimatedValue()+"");
// //動畫的進度0-1之間,可以進行判斷進行響應
// Log.d("aaaaaaa",valueAnimator.getAnimatedFraction()+"");
// }
// });
// valueAnimator.start();//動畫的啟動
// ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(tv,"translationY",0,500,200);
// //将tv從0移動到500再移動200
// objectAnimator.setDuration(2000);
// objectAnimator.start();//動畫的啟動
}
}