Android補(bǔ)間動(dòng)畫基本使用(位移、縮放、旋轉(zhuǎn)、透明)
本文講述了Android補(bǔ)間動(dòng)畫基本使用(位移、縮放、旋轉(zhuǎn)、透明)。分享給大家供大家參考,具體如下:
補(bǔ)間動(dòng)畫
原形態(tài)變成新形態(tài)時(shí)為了過(guò)渡變形過(guò)程,生成的動(dòng)畫就叫補(bǔ)間動(dòng)畫
位移、旋轉(zhuǎn)、縮放、透明
位移:
參數(shù)10指的是X的起點(diǎn)坐標(biāo),但不是指屏幕x坐標(biāo)為10的位置,而是imageview的 真實(shí)X + 10
參數(shù)150指的是X的終點(diǎn)坐標(biāo),它的值是imageview的 真實(shí)X + 150
//創(chuàng)建為位移動(dòng)畫對(duì)象,設(shè)置動(dòng)畫的初始位置和結(jié)束位置
TranslateAnimation ta = new TranslateAnimation(10, 150, 20, 140);
1. x坐標(biāo)的起點(diǎn)位置,如果相對(duì)于自己,傳0.5f,那么起點(diǎn)坐標(biāo)就是 真實(shí)X + 0.5 * iv寬度;
2. x坐標(biāo)的終點(diǎn)位置,如果傳入2,那么終點(diǎn)坐標(biāo)就是 真實(shí)X + 2 * iv的寬度;
3. y坐標(biāo)的起點(diǎn)位置,如果傳入0.5f,那么起點(diǎn)坐標(biāo)就是 真實(shí)Y + 0.5 * iv高度;
4. y坐標(biāo)的終點(diǎn)位置,如果傳入2,那么終點(diǎn)坐標(biāo)就是 真實(shí)Y + 2 * iv高度。
TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2);
動(dòng)畫播放相關(guān)的設(shè)置
//設(shè)置動(dòng)畫持續(xù)時(shí)間 ta.setDuration(2000); //動(dòng)畫重復(fù)播放的次數(shù) ta.setRepeatCount(1); //動(dòng)畫重復(fù)播放的模式 ta.setRepeatMode(Animation.REVERSE); //動(dòng)畫播放完畢后,組件停留在動(dòng)畫結(jié)束的位置上 ta.setFillAfter(true); //播放動(dòng)畫 iv.startAnimation(ta);
縮放:
1.參數(shù)0.1f表示動(dòng)畫的起始寬度是真實(shí)寬度的0.1倍
2.參數(shù)4表示動(dòng)畫的結(jié)束寬度是真實(shí)寬度的4倍
3.縮放的中心點(diǎn)在iv左上角
ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4);
1. 參數(shù)0.1f和4意義與上面相同
2. 改變縮放的中心點(diǎn):傳入的兩個(gè)0.5f,類型都是相對(duì)于自己,這兩個(gè)參數(shù)改變了縮放的中心點(diǎn)
3. 中心點(diǎn)x坐標(biāo) = 真實(shí)X + 0.5 * iv寬度
4. 中心點(diǎn)Y坐標(biāo) = 真實(shí)Y + 0.5 * iv高度
ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4, Animation.RELATIVETOSELF, 0.5f, Animation.RELATIVETOSELF, 0.5f);
透明:
0為完全透明,1為完全不透明
AlphaAnimation aa = new AlphaAnimation(0, 0.5f);
旋轉(zhuǎn):
1. 20表示動(dòng)畫開(kāi)始時(shí)的iv的角度
2. 360表示動(dòng)畫結(jié)束時(shí)iv的角度
3. 默認(rèn)旋轉(zhuǎn)的圓心在iv左上角
RotateAnimation ra = new RotateAnimation(20, 360);
1. 20,360的意義和上面一樣
2. 指定圓心坐標(biāo),相對(duì)于自己,值傳入0.5,那么圓心的x坐標(biāo):真實(shí)X + iv寬度 * 0.5
3. 圓心的Y坐標(biāo):真實(shí)Y + iv高度 * 0.5
RotateAnimation ra = new RotateAnimation(20, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
所有動(dòng)畫一起飛
//創(chuàng)建動(dòng)畫集合 AnimationSet set = new AnimationSet(false); //往集合中添加動(dòng)畫 set.addAnimation(aa); set.addAnimation(sa); set.addAnimation(ra); iv.startAnimation(set);
效果如圖:
布局代碼:
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_alignParentBottom="true" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="translate" android:text="平移" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="scale" android:text="縮放" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="alpha" android:text="透明" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="rotate" android:text="旋轉(zhuǎn)" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="fly" android:text="一起飛" /> </LinearLayout> </RelativeLayout>
MainActivity.java
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView iv; private TranslateAnimation ta; private ScaleAnimation sa; private AlphaAnimation aa; private RotateAnimation ra; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); } //平移 public void translate(View view) { // ta = new TranslateAnimation(10, 100, 20, 200); ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 1.5f); //設(shè)置播放時(shí)間 ta.setDuration(2000); //設(shè)置重復(fù)次數(shù) ta.setRepeatCount(1); //動(dòng)畫重復(fù)播放的模式 ta.setRepeatMode(Animation.REVERSE); iv.startAnimation(ta); } //縮放 public void scale(View view) { // sa = new ScaleAnimation(2, 4, 2, 4, iv.getWidth() / 2, iv.getHeight() / 2); sa = new ScaleAnimation(0.5f, 2, 0.1f, 3, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //設(shè)置播放時(shí)間 sa.setDuration(2000); //設(shè)置重復(fù)次數(shù) sa.setRepeatCount(1); //動(dòng)畫重復(fù)播放的模式 sa.setRepeatMode(Animation.ABSOLUTE); //動(dòng)畫播放完畢后,組件停留在動(dòng)畫結(jié)束的位置上 sa.setFillAfter(true); iv.startAnimation(sa); } //透明 public void alpha(View view) { aa = new AlphaAnimation(0, 1); //設(shè)置播放時(shí)間 aa.setDuration(2000); //設(shè)置重復(fù)次數(shù) aa.setRepeatCount(1); //動(dòng)畫重復(fù)播放的模式 aa.setRepeatMode(Animation.REVERSE); //動(dòng)畫播放完畢后,組件停留在動(dòng)畫結(jié)束的位置上 // aa.setFillAfter(true); iv.startAnimation(aa); } //旋轉(zhuǎn) public void rotate(View view) { ra = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //設(shè)置播放時(shí)間 ra.setDuration(2000); //設(shè)置重復(fù)次數(shù) ra.setRepeatCount(1); //動(dòng)畫重復(fù)播放的模式 ra.setRepeatMode(Animation.REVERSE); //動(dòng)畫播放完畢后,組件停留在動(dòng)畫結(jié)束的位置上 ra.setFillAfter(true); iv.startAnimation(ra); } //位移、縮放、透明、旋轉(zhuǎn)同時(shí)進(jìn)行 public void fly(View view) { AnimationSet set = new AnimationSet(false); set.addAnimation(ta); set.addAnimation(sa); set.addAnimation(aa); set.addAnimation(ra); iv.startAnimation(set); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
實(shí)現(xiàn)Android studio設(shè)置自動(dòng)導(dǎo)包及自動(dòng)導(dǎo)包快捷鍵
這篇文章主要介紹了實(shí)現(xiàn)Android studio設(shè)置自動(dòng)導(dǎo)包及自動(dòng)導(dǎo)包快捷鍵的相關(guān)資料,需要的朋友可以參考下2017-05-05Android電池電量監(jiān)聽(tīng)的示例代碼
本篇文章主要介紹了Android電池電量監(jiān)聽(tīng)的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10Android手勢(shì)密碼view學(xué)習(xí)筆記(一)
這篇文章主要為大家詳細(xì)介紹了Android手勢(shì)密碼view的學(xué)習(xí)筆記,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android studio開(kāi)發(fā)小型對(duì)話機(jī)器人app(實(shí)例代碼)
這篇文章主要介紹了Android studio開(kāi)發(fā)一個(gè)小型對(duì)話機(jī)器人app,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04flutter實(shí)現(xiàn)點(diǎn)擊事件
這篇文章主要為大家詳細(xì)介紹了flutter實(shí)現(xiàn)點(diǎn)擊事件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08Android復(fù)選框CheckBox與開(kāi)關(guān)按鈕Switch及單選按鈕RadioButton使用示例詳解
這篇文章主要介紹了Android復(fù)選框CheckBox與開(kāi)關(guān)按鈕Switch及單選按鈕RadioButton使用示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-09-09Flutter 實(shí)現(xiàn)酷炫的3D效果示例代碼
這篇文章主要介紹了Flutter 實(shí)現(xiàn)酷炫的3D效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07