国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android補間動畫基本使用(位移、縮放、旋轉、透明)

2019-10-22 18:11:20
字體:
來源:轉載
供稿:網友

本文講述了Android補間動畫基本使用(位移、縮放、旋轉、透明)。分享給大家供大家參考,具體如下:

補間動畫

原形態變成新形態時為了過渡變形過程,生成的動畫就叫補間動畫
位移、旋轉、縮放、透明

位移:

參數10指的是X的起點坐標,但不是指屏幕x坐標為10的位置,而是imageview的 真實X + 10
參數150指的是X的終點坐標,它的值是imageview的 真實X + 150

//創建為位移動畫對象,設置動畫的初始位置和結束位置

TranslateAnimation ta = new TranslateAnimation(10, 150, 20, 140);

1.  x坐標的起點位置,如果相對于自己,傳0.5f,那么起點坐標就是 真實X + 0.5 * iv寬度;

2.  x坐標的終點位置,如果傳入2,那么終點坐標就是 真實X + 2 * iv的寬度;

3.  y坐標的起點位置,如果傳入0.5f,那么起點坐標就是 真實Y + 0.5 * iv高度;

4.  y坐標的終點位置,如果傳入2,那么終點坐標就是 真實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);

動畫播放相關的設置

 //設置動畫持續時間ta.setDuration(2000); //動畫重復播放的次數ta.setRepeatCount(1); //動畫重復播放的模式ta.setRepeatMode(Animation.REVERSE); //動畫播放完畢后,組件停留在動畫結束的位置上ta.setFillAfter(true); //播放動畫iv.startAnimation(ta);

縮放:

1.參數0.1f表示動畫的起始寬度是真實寬度的0.1倍
2.參數4表示動畫的結束寬度是真實寬度的4倍
3.縮放的中心點在iv左上角

ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4);

1.  參數0.1f和4意義與上面相同

2.  改變縮放的中心點:傳入的兩個0.5f,類型都是相對于自己,這兩個參數改變了縮放的中心點

3.  中心點x坐標 = 真實X + 0.5 * iv寬度

4.  中心點Y坐標 = 真實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);

旋轉:

1.  20表示動畫開始時的iv的角度

2.  360表示動畫結束時iv的角度

3.  默認旋轉的圓心在iv左上角

RotateAnimation ra = new RotateAnimation(20, 360);

1.  20,360的意義和上面一樣

2.  指定圓心坐標,相對于自己,值傳入0.5,那么圓心的x坐標:真實X + iv寬度 * 0.5

3.  圓心的Y坐標:真實Y + iv高度 * 0.5

RotateAnimation ra = new RotateAnimation(20, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

所有動畫一起飛

 //創建動畫集合 AnimationSet set = new AnimationSet(false); //往集合中添加動畫 set.addAnimation(aa); set.addAnimation(sa); set.addAnimation(ra); iv.startAnimation(set);

效果如圖:

android,補間動畫,位移,縮放,旋轉,透明

布局代碼:

 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="旋轉" />  <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);  //設置播放時間  ta.setDuration(2000);  //設置重復次數  ta.setRepeatCount(1);  //動畫重復播放的模式  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);  //設置播放時間  sa.setDuration(2000);  //設置重復次數  sa.setRepeatCount(1);  //動畫重復播放的模式  sa.setRepeatMode(Animation.ABSOLUTE);  //動畫播放完畢后,組件停留在動畫結束的位置上  sa.setFillAfter(true);  iv.startAnimation(sa);  }  //透明  public void alpha(View view) {  aa = new AlphaAnimation(0, 1);  //設置播放時間  aa.setDuration(2000);  //設置重復次數  aa.setRepeatCount(1);  //動畫重復播放的模式  aa.setRepeatMode(Animation.REVERSE);  //動畫播放完畢后,組件停留在動畫結束的位置上 // aa.setFillAfter(true);  iv.startAnimation(aa);  }  //旋轉  public void rotate(View view) {  ra = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f,  Animation.RELATIVE_TO_SELF, 0.5f);  //設置播放時間  ra.setDuration(2000);  //設置重復次數  ra.setRepeatCount(1);  //動畫重復播放的模式  ra.setRepeatMode(Animation.REVERSE);  //動畫播放完畢后,組件停留在動畫結束的位置上  ra.setFillAfter(true);  iv.startAnimation(ra);  }  //位移、縮放、透明、旋轉同時進行  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);  } } 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新干县| 沙坪坝区| 本溪市| 罗平县| 沛县| 诸城市| 兴隆县| 化德县| 乳山市| 江山市| 托克逊县| 蓬安县| 厦门市| 四平市| 凉山| 芒康县| 镇远县| 桑植县| 东源县| 福鼎市| 兴国县| 襄垣县| 平乐县| 连云港市| 丽水市| 新田县| 伊春市| 韶关市| 策勒县| 霍山县| 华阴市| 张北县| 合作市| 尼木县| 宜川县| 绥棱县| 固原市| 淮阳县| 沂源县| 云和县| 衡阳县|