Android AnimationUtils (动画)的使用
生活随笔
收集整理的這篇文章主要介紹了
Android AnimationUtils (动画)的使用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?AnimationUtils? 其實(shí)就是補(bǔ)間動(dòng)畫(Tween Animation) 在xml 中的寫了動(dòng)畫java 中調(diào)用
這邊打算就寫一個(gè)demo 簡答的記錄下它的使用詳細(xì)的請看
點(diǎn)擊查看,這篇博客很詳情的
AnimationUtils? ?在java 代碼中一般結(jié)合loadAnimation 使用 下面寫一個(gè)從右邊到左邊的動(dòng)畫
首先看下效果圖
下面 上代碼吧 xml 里面就2個(gè)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="結(jié)束" /></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);// 如果沒有需求可以不寫這個(gè)監(jiān)聽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>
這里面的屬性如果不是很清楚可查看剛才我給的鏈接,之前的博客都寫了,所有這里簡單記錄下
?關(guān)于這個(gè)監(jiān)聽方法自己想到了動(dòng)畫結(jié)束隱藏布局,不過前幾天幾天回顧java return 方法,如果忘記了可以回顧下
retrun 關(guān)鍵字
這里也是練習(xí)就寫了下面的demo?
動(dòng)畫結(jié)束隱藏圖片。
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);// 如果沒有需求可以不寫這個(gè)監(jiān)聽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;}}
?
總結(jié)
以上是生活随笔為你收集整理的Android AnimationUtils (动画)的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 雅马哈xj6新的摩托多少钱?
- 下一篇: Android ViewAnimatio