這篇文章主要介紹了如何在Android中實(shí)現(xiàn)一個(gè)補(bǔ)間動(dòng)畫(huà)效果,此處通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考價(jià)值,需要的朋友可以參考下:
創(chuàng)新互聯(lián)建站主打移動(dòng)網(wǎng)站、網(wǎng)站制作、成都網(wǎng)站制作、網(wǎng)站改版、網(wǎng)絡(luò)推廣、網(wǎng)站維護(hù)、國(guó)際域名空間、等互聯(lián)網(wǎng)信息服務(wù),為各行業(yè)提供服務(wù)。在技術(shù)實(shí)力的保障下,我們?yōu)榭蛻舫兄Z穩(wěn)定,放心的服務(wù),根據(jù)網(wǎng)站的內(nèi)容與功能再?zèng)Q定采用什么樣的設(shè)計(jì)。最后,要實(shí)現(xiàn)符合網(wǎng)站需求的內(nèi)容、功能與設(shè)計(jì),我們還會(huì)規(guī)劃穩(wěn)定安全的技術(shù)方案做保障。
補(bǔ)間動(dòng)畫(huà)
原形態(tài)變成新形態(tài)時(shí)為了過(guò)渡變形過(guò)程,生成的動(dòng)畫(huà)就叫補(bǔ)間動(dòng)畫(huà)
位移、旋轉(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)畫(huà)對(duì)象,設(shè)置動(dòng)畫(huà)的初始位置和結(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)畫(huà)播放相關(guān)的設(shè)置
//設(shè)置動(dòng)畫(huà)持續(xù)時(shí)間 ta.setDuration(2000); //動(dòng)畫(huà)重復(fù)播放的次數(shù) ta.setRepeatCount(1); //動(dòng)畫(huà)重復(fù)播放的模式 ta.setRepeatMode(Animation.REVERSE); //動(dòng)畫(huà)播放完畢后,組件停留在動(dòng)畫(huà)結(jié)束的位置上 ta.setFillAfter(true); //播放動(dòng)畫(huà) iv.startAnimation(ta);
縮放:
1.參數(shù)0.1f表示動(dòng)畫(huà)的起始寬度是真實(shí)寬度的0.1倍
2.參數(shù)4表示動(dòng)畫(huà)的結(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)畫(huà)開(kāi)始時(shí)的iv的角度
2. 360表示動(dòng)畫(huà)結(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)畫(huà)一起飛
//創(chuàng)建動(dòng)畫(huà)集合 AnimationSet set = new AnimationSet(false); //往集合中添加動(dòng)畫(huà) 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)畫(huà)重復(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)畫(huà)重復(fù)播放的模式 sa.setRepeatMode(Animation.ABSOLUTE); //動(dòng)畫(huà)播放完畢后,組件停留在動(dòng)畫(huà)結(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)畫(huà)重復(fù)播放的模式 aa.setRepeatMode(Animation.REVERSE); //動(dòng)畫(huà)播放完畢后,組件停留在動(dòng)畫(huà)結(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)畫(huà)重復(fù)播放的模式 ra.setRepeatMode(Animation.REVERSE); //動(dòng)畫(huà)播放完畢后,組件停留在動(dòng)畫(huà)結(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); } }
到此這篇關(guān)于如何在Android中實(shí)現(xiàn)一個(gè)補(bǔ)間動(dòng)畫(huà)效果的文章就介紹到這了,更多相關(guān)內(nèi)容請(qǐng)搜索創(chuàng)新互聯(lián)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持創(chuàng)新互聯(lián)!
文章名稱:如何在Android中實(shí)現(xiàn)一個(gè)補(bǔ)間動(dòng)畫(huà)效果
網(wǎng)頁(yè)網(wǎng)址:http://chinadenli.net/article2/gpdiic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、服務(wù)器托管、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)公司、標(biāo)簽優(yōu)化、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)