本文講述了Android補(bǔ)間動(dòng)畫(huà)基本使用(位移、縮放、旋轉(zhuǎn)、透明)。分享給大家供大家參考,具體如下:
補(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è)置
1
2
3
4
5
6
7
8
9
10
|
//設(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à)一起飛
1
2
3
4
5
6
7
|
//創(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); |
效果如圖:
布局代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
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); } } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/u014657752/article/details/48089939