- android中提供了4中動畫:
- AlphaAnimation 透明度動畫效果
- ScaleAnimation 縮放動畫效果
- TranslateAnimation 位移動畫效果
- RotateAnimation 旋轉動畫效果
- 本節講解ScaleAnimation 動畫,
- ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
- 參數說明:
- 複製代碼 代碼如下:
- float fromX 動畫起始時 X座標上的伸縮尺寸
- float toX 動畫結束時 X座標上的伸縮尺寸
- float fromY 動畫起始時Y座標上的伸縮尺寸
- float toY 動畫結束時Y座標上的伸縮尺寸
- int pivotXType 動畫在X軸相對於物件位置類型
- float pivotXValue 動畫相對於物件的X座標的開始位置
- int pivotYType 動畫在Y軸相對於物件位置類型
- float pivotYValue 動畫相對於物件的Y座標的開始位置
- 代碼:
- 複製代碼 代碼如下:
- public class MainActivity extends Activity {
- ImageView image;
- Button start;
- Button cancel;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- image = (ImageView) findViewById(R.id.main_img);
- start = (Button) findViewById(R.id.main_start);
- cancel = (Button) findViewById(R.id.main_cancel);
- /** 設置縮放動畫 */
- final ScaleAnimation animation =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
- Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
- animation.setDuration(2000);//設置動畫持續時間
- /** 常用方法 */
- //animation.setRepeatCount(int repeatCount);//設置重複次數
- //animation.setFillAfter(boolean);//動畫執行完後是否停留在執行完的狀態
- //animation.setStartOffset(long startOffset);//執行前的等待時間
- start.setOnClickListener(new OnClickListener() {
- public void onClick(View arg0) {
- image.setAnimation(animation);
- /** 開始動畫 */
- animation.startNow();
- }
- });
- cancel.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- /** 結束動畫 */
- animation.cancel();
- }
- });
- }
- }
- 本節講解ScaleAnimation 動畫,
- ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
- 參數說明:
- 複製代碼 代碼如下:
- float fromX 動畫起始時 X座標上的伸縮尺寸
- float toX 動畫結束時 X座標上的伸縮尺寸
- float fromY 動畫起始時Y座標上的伸縮尺寸
- float toY 動畫結束時Y座標上的伸縮尺寸
- int pivotXType 動畫在X軸相對於物件位置類型
- float pivotXValue 動畫相對於物件的X座標的開始位置
- int pivotYType 動畫在Y軸相對於物件位置類型
- float pivotYValue 動畫相對於物件的Y座標的開始位置
- 代碼:
- 複製代碼 代碼如下:
- public class MainActivity extends Activity {
- ImageView image;
- Button start;
- Button cancel;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- image = (ImageView) findViewById(R.id.main_img);
- start = (Button) findViewById(R.id.main_start);
- cancel = (Button) findViewById(R.id.main_cancel);
- /** 設置縮放動畫 */
- final ScaleAnimation animation =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
- Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
- animation.setDuration(2000);//設置動畫持續時間
- /** 常用方法 */
- //animation.setRepeatCount(int repeatCount);//設置重複次數
- //animation.setFillAfter(boolean);//動畫執行完後是否停留在執行完的狀態
- //animation.setStartOffset(long startOffset);//執行前的等待時間
- start.setOnClickListener(new OnClickListener() {
- public void onClick(View arg0) {
- image.setAnimation(animation);
- /** 開始動畫 */
- animation.startNow();
- }
- });
- cancel.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- /** 結束動畫 */
- animation.cancel();
- }
- });
- }
- }
- 本節講解RotateAnimation 動畫,
- RotateAnimation (float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
- 參數說明:
- float fromDegrees:旋轉的開始角度。
- float toDegrees:旋轉的結束角度。
- int pivotXType:X軸的伸縮模式,可以取值爲ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
- float pivotXValue:X座標的伸縮值。
- int pivotYType:Y軸的伸縮模式,可以取值爲ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
- float pivotYValue:Y座標的伸縮值。
- 代碼:
- <div class="codetitle"><span style="CURSOR: pointer"><u>複製代碼</u></span> 代碼如下:</div><div id="code17269" class="codebody">
- public class MainActivity extends Activity {
- ImageView image;
- Button start;
- Button cancel;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- image = (ImageView) findViewById(R.id.main_img);
- start = (Button) findViewById(R.id.main_start);
- cancel = (Button) findViewById(R.id.main_cancel);
- /** 設置旋轉動畫 */
- final RotateAnimation animation =new RotateAnimation(0f,360f,Animation.RELATIVE_TO_SELF,
- 0.5f,Animation.RELATIVE_TO_SELF,0.5f);
- animation.setDuration(3000);//設置動畫持續時間
- /** 常用方法 */
- //animation.setRepeatCount(int repeatCount);//設置重複次數
- //animation.setFillAfter(boolean);//動畫執行完後是否停留在執行完的狀態
- //animation.setStartOffset(long startOffset);//執行前的等待時間
- start.setOnClickListener(new OnClickListener() {
- public void onClick(View arg0) {
- image.setAnimation(animation);
- /** 開始動畫 */
- animation.startNow();
- }
- });
- cancel.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- /** 結束動畫 */
- animation.cancel();
- }
- });
- }
- } </div>
- 本節講解TranslateAnimation動畫,TranslateAnimation比較常用,比如QQ,網易新聞菜單條的動畫,就可以用TranslateAnimation實現,
- 通過TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) 來定義動畫
- 參數說明:
- <div class="codetitle"><span style="CURSOR: pointer"><u>複製代碼</u></span> 代碼如下:</div><div id="code3207" class="codebody">
- float fromXDelta 動畫開始的點離當前View X座標上的差值
- float toXDelta 動畫結束的點離當前View X座標上的差值
- float fromYDelta 動畫開始的點離當前View Y座標上的差值
- float toYDelta 動畫開始的點離當前View Y座標上的差值
- </div>
- 常用方法:
- <div class="codetitle"><span style="CURSOR: pointer"><u>複製代碼</u></span> 代碼如下:</div><div id="code94921" class="codebody">
- animation.setDuration(long durationMillis);//設置動畫持續時間
- animation.setRepeatCount(int i);//設置重複次數
- animation.setRepeatMode(Animation.REVERSE);//設置反方向執行
- </div>
- Xml屬性:
- <div class="codetitle"><span style="CURSOR: pointer"><u>複製代碼</u></span> 代碼如下:</div><div id="code72894" class="codebody">
- android:duration:運行動畫的時間
- android:repeatCount:定義動畫重複的時間
- </div>
- 代碼:
- <div class="codetitle"><span style="CURSOR: pointer"><u>複製代碼</u></span> 代碼如下:</div><div id="code12415" class="codebody">
- public class MainActivity extends Activity {
- ImageView image;
- Button start;
- Button cancel;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- image = (ImageView) findViewById(R.id.main_img);
- start = (Button) findViewById(R.id.main_start);
- cancel = (Button) findViewById(R.id.main_cancel);
- /** 設置位移動畫 向右位移150 */
- final TranslateAnimation animation = new TranslateAnimation(0, 150,0, 0);
- animation.setDuration(2000);//設置動畫持續時間
- animation.setRepeatCount(2);//設置重複次數
- animation.setRepeatMode(Animation.REVERSE);//設置反方向執行
- start.setOnClickListener(new OnClickListener() {
- public void onClick(View arg0) {
- image.setAnimation(animation);
- /** 開始動畫 */
- animation.startNow();
- }
- });
- cancel.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- /** 結束動畫 */
- animation.cancel();
- }
- });
- }
- } </div><div class="codebody"> </div><div class="codebody"> </div><div class="codebody"> </div><div class="codebody"> </div>
- <pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code">本節講解AlphaAnimation 動畫,窗口的動畫效果,淡入淡出什麼的,有些遊戲的歡迎動畫,logo的淡入淡出效果就使用AlphaAnimation。
- 直接看代碼:
- 複製代碼 代碼如下:
- public class MainActivity extends Activity {
- ImageView image;
- Button start;
- Button cancel;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- image = (ImageView) findViewById(R.id.main_img);
- start = (Button) findViewById(R.id.main_start);
- cancel = (Button) findViewById(R.id.main_cancel);
- /** 設置透明度漸變動畫 */
- final AlphaAnimation animation = new AlphaAnimation(1, 0);
- animation.setDuration(2000);//設置動畫持續時間
- /** 常用方法 */
- //animation.setRepeatCount(int repeatCount);//設置重複次數
- //animation.setFillAfter(boolean);//動畫執行完後是否停留在執行完的狀態
- //animation.setStartOffset(long startOffset);//執行前的等待時間
- start.setOnClickListener(new OnClickListener() {
- public void onClick(View arg0) {
- image.setAnimation(animation);
- /** 開始動畫 */
- animation.startNow();
- }
- });
- cancel.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- /** 結束動畫 */
- animation.cancel();
- }
- });
- }
- }</pre><pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code"> </pre><pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code"> </pre><pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code"> </pre><pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code"> </pre><pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code"> </pre><pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code"> </pre><pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code"> </pre><pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code"> </pre><pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code"> </pre><pre code_snippet_id="94243" snippet_file_name="blog_20131203_1_5890611" class="java" name="code"> </pre><br>
- <pre></pre>
- <p></p>
- <p> </p>
- <p> </p>
- <p> </p>
- <p> </p>
- <p> </p>
- <pre></pre>