Android AnimationUtils (动画)的使用
生活随笔
收集整理的這篇文章主要介紹了
Android AnimationUtils (动画)的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?AnimationUtils? 其實就是補間動畫(Tween Animation) 在xml 中的寫了動畫java 中調用
這邊打算就寫一個demo 簡答的記錄下它的使用詳細的請看
點擊查看,這篇博客很詳情的
AnimationUtils? ?在java 代碼中一般結合loadAnimation 使用 下面寫一個從右邊到左邊的動畫
首先看下效果圖
下面 上代碼吧 xml 里面就2個button 和一張圖片
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/start"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="開始" /><Buttonandroid:id="@+id/stop"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="結束" /></LinearLayout><ImageViewandroid:id="@+id/img"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:src="@mipmap/girl" /></LinearLayout>
java 里面的代碼:
public class MainActivity extends AppCompatActivity {private ImageView imageView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imageView = findViewById(R.id.img);final Animation animation = AnimationUtils.loadAnimation(this, R.anim.sacle_shape);// 如果沒有需求可以不寫這個監聽animation.setAnimationListener(new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {}@Overridepublic void onAnimationRepeat(Animation animation) {}});findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {imageView.startAnimation(animation);}});findViewById(R.id.stop).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {imageView.clearAnimation();}});}}
anim 文件里面的sacle_shape
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><scaleandroid:duration="2000"android:fillAfter="true"android:fromXScale="0.0"android:fromYScale="1.0"android:pivotX="90%"android:repeatCount="-1"android:repeatMode="restart"android:toXScale="1.0"android:toYScale="1.0" /></set>
這里面的屬性如果不是很清楚可查看剛才我給的鏈接,之前的博客都寫了,所有這里簡單記錄下
?關于這個監聽方法自己想到了動畫結束隱藏布局,不過前幾天幾天回顧java return 方法,如果忘記了可以回顧下
retrun 關鍵字
這里也是練習就寫了下面的demo?
動畫結束隱藏圖片。
public class MainActivity extends AppCompatActivity {private ImageView imageView;private Animation animation;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imageView = findViewById(R.id.img);findViewById(R.id.show).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {animation = getRighttoLeftAnimation(imageView);}});findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {imageView.startAnimation(animation);}});findViewById(R.id.stop).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {imageView.clearAnimation();}});}private Animation getRighttoLeftAnimation(final View view) {Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.sacle_shape);// 如果沒有需求可以不寫這個監聽animation1.setAnimationListener(new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {view.setVisibility(View.VISIBLE);}@Overridepublic void onAnimationEnd(Animation animation) {view.setVisibility(View.GONE);}@Overridepublic void onAnimationRepeat(Animation animation) {}});return animation1;}}
?
總結
以上是生活随笔為你收集整理的Android AnimationUtils (动画)的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 雅马哈xj6新的摩托多少钱?
- 下一篇: Android ViewAnimatio