Android Animation之TranslateAnimation(平移動(dòng)畫)
TranslateAnimation(平移動(dòng)畫)的意思無非就是一張圖片或其他從一個(gè)位置到達(dá)另外一個(gè)位置。直接代碼分析,相關(guān)重要屬性參數(shù)解釋都在代碼中。
1、首先編寫main.xml文件。
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ImageView
android:id="@+id/image"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/car_one1"/>
</RelativeLayout>
2、接下來編寫MainActivity.java文件。
package com.example.dell.bitmapproject;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image =(ImageView)findViewById(R.id.image);
image.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements View.OnClickListener {
@Override
public void onClick(View v) {
/*
AnimationSet相當(dāng)于一個(gè)動(dòng)畫的集合,true表示使用Animation的interpolator
false則是使用自己的。
Interpolator 被用來修飾動(dòng)畫效果,定義動(dòng)畫的變化率,可以使存在的動(dòng)畫效果
accelerated(加速),decelerated(減速),repeated(重復(fù)),bounced(彈跳)等。
*/
AnimationSet animationSet = new AnimationSet(true);
/*
Animation還有幾個(gè)方法
setFillAfter(boolean fillAfter)
如果fillAfter的值為真的話,動(dòng)畫結(jié)束后,控件停留在執(zhí)行后的狀態(tài)
setFillBefore(boolean fillBefore)
如果fillBefore的值為真的話,動(dòng)畫結(jié)束后,控件停留在動(dòng)畫開始的狀態(tài)
setStartOffset(long startOffset)
設(shè)置動(dòng)畫控件執(zhí)行動(dòng)畫之前等待的時(shí)間
setRepeatCount(int repeatCount)
設(shè)置動(dòng)畫重復(fù)執(zhí)行的次數(shù)
*/
TranslateAnimation translateAnimation = new TranslateAnimation(
//X軸初始位置
Animation.RELATIVE_TO_SELF, 0.0f,
//X軸移動(dòng)的結(jié)束位置
Animation.RELATIVE_TO_SELF,0.5f,
//y軸開始位置
Animation.RELATIVE_TO_SELF,0.0f,
//y軸移動(dòng)后的結(jié)束位置
Animation.RELATIVE_TO_SELF,1.5f);
//3秒完成動(dòng)畫
translateAnimation.setDuration(2000);
//如果fillAfter的值為真的話,動(dòng)畫結(jié)束后,控件停留在執(zhí)行后的狀態(tài)
animationSet.setFillAfter(true);
//將AlphaAnimation這個(gè)已經(jīng)設(shè)置好的動(dòng)畫添加到 AnimationSet中
animationSet.addAnimation(translateAnimation);
//啟動(dòng)動(dòng)畫
MainActivity.this.image.startAnimation(animationSet);
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android使用ViewPager實(shí)現(xiàn)圖片自動(dòng)切換
這篇文章主要為大家詳細(xì)介紹了android使用ViewPager實(shí)現(xiàn)圖片自動(dòng)切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
android圖庫(kù)豎屏不顯示status bar的解決方法
圖庫(kù)在JB和JB2的版本上顯示的行為是:橫屏全屏顯示,豎屏?xí)@示status bar,圖庫(kù)在JB和JB2的版本上顯示的行為是:橫屏全屏顯示,豎屏?xí)@示status bar,具體實(shí)現(xiàn)方法如下,不會(huì)的朋友可以參考下哈2013-06-06
Android編程實(shí)現(xiàn)點(diǎn)擊EditText之外的控件隱藏軟鍵盤功能
這篇文章主要介紹了Android編程實(shí)現(xiàn)點(diǎn)擊EditText之外的控件隱藏軟鍵盤功能,涉及Android控件的功能、屬性及相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
Android?Jetpack?組件LiveData源碼解析
這篇文章主要為大家介紹了Android?Jetpack?組件LiveData源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

