溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android控件Tween動畫(補間動畫)實現(xiàn)方法示例

發(fā)布時間:2020-09-21 15:43:07 來源:腳本之家 閱讀:118 作者:遲做總比不做強 欄目:移動開發(fā)

本文實例講述了Android控件Tween動畫(補間動畫)實現(xiàn)方法。分享給大家供大家參考,具體如下:

Android動畫中的Tween動畫:是把控件對象不斷的進行圖像變化來產(chǎn)生旋轉(zhuǎn)、平移、放縮和漸變等動畫效果。

/**
 * 控件Tween動畫
 * 
 * @description:
 * @author ldm
 * @date 2016-6-22 下午5:26:24
 */
public class TweenActivity extends Activity {
  private SeekBar seekBarX;// 拖動條控件
  private SeekBar seekBarY;
  private SeekBar scaleSeekBarX;
  private SeekBar scaleSeekBarY;
  private SeekBar rotationSeekBarX;
  private SeekBar rotationSeekBarY;
  private SeekBar rotationSeekBarZ;
  private Button button;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tween);
    initViews();
    initEvents();
  }
  /**
   * 
   * @description:初始化控件
   * @author ldm
   * @date 2016-6-22 下午5:26:26
   */
  private void initViews() {
    button = (Button) findViewById(R.id.button);
    seekBarX = (SeekBar) findViewById(R.id.translationX);
    seekBarX.setMax(400);
    seekBarY = (SeekBar) findViewById(R.id.translationY);
    seekBarY.setMax(800);
    scaleSeekBarX = (SeekBar) findViewById(R.id.scaleX);
    scaleSeekBarX.setMax(50);
    scaleSeekBarX.setProgress(10);
    scaleSeekBarY = (SeekBar) findViewById(R.id.scaleY);
    scaleSeekBarY.setMax(50);
    scaleSeekBarY.setProgress(10);
    rotationSeekBarX = (SeekBar) findViewById(R.id.rotationX);
    rotationSeekBarX.setMax(360);
    rotationSeekBarY = (SeekBar) findViewById(R.id.rotationY);
    rotationSeekBarY.setMax(360);
    rotationSeekBarZ = (SeekBar) findViewById(R.id.rotationZ);
    rotationSeekBarZ.setMax(360);
  }
  /**
   * 
   * @description:控件設置監(jiān)聽事件
   * @author ldm
   * @date 2016-6-22 下午5:26:26
   */
  private void initEvents() {
    // 按鈕X方向平移動畫
    seekBarX.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
      public void onStopTrackingTouch(SeekBar seekBar) {
      }
      public void onStartTrackingTouch(SeekBar seekBar) {
      }
      public void onProgressChanged(SeekBar seekBar, int progress,
          boolean fromUser) {
        // X方向平移
        button.setTranslationX((float) progress);
      }
    });
    // 按鈕Y方向平移動畫
    seekBarY.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
      public void onStopTrackingTouch(SeekBar seekBar) {
      }
      public void onStartTrackingTouch(SeekBar seekBar) {
      }
      public void onProgressChanged(SeekBar seekBar, int progress,
          boolean fromUser) {
        // Y方向平移
        button.setTranslationY((float) progress);
      }
    });
    // 按鈕X方向縮放動畫
    scaleSeekBarX
        .setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
          public void onStopTrackingTouch(SeekBar seekBar) {
          }
          public void onStartTrackingTouch(SeekBar seekBar) {
          }
          public void onProgressChanged(SeekBar seekBar,
              int progress, boolean fromUser) {
            // X方向縮放
            button.setScaleX((float) progress / 10f);
          }
        });
    // 按鈕Y方向縮放動畫
    scaleSeekBarY
        .setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
          public void onStopTrackingTouch(SeekBar seekBar) {
          }
          public void onStartTrackingTouch(SeekBar seekBar) {
          }
          public void onProgressChanged(SeekBar seekBar,
              int progress, boolean fromUser) {
            // Y方向縮放
            button.setScaleY((float) progress / 10f);
          }
        });
    // 按鈕X方向旋轉(zhuǎn)動畫
    rotationSeekBarX
        .setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
          public void onStopTrackingTouch(SeekBar seekBar) {
          }
          public void onStartTrackingTouch(SeekBar seekBar) {
          }
          public void onProgressChanged(SeekBar seekBar,
              int progress, boolean fromUser) {
            // X方向旋轉(zhuǎn)
            button.setRotationX((float) progress);
          }
        });
    // 按鈕Y方向旋轉(zhuǎn)動畫
    rotationSeekBarY
        .setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
          public void onStopTrackingTouch(SeekBar seekBar) {
          }
          public void onStartTrackingTouch(SeekBar seekBar) {
          }
          public void onProgressChanged(SeekBar seekBar,
              int progress, boolean fromUser) {
            // Y方向旋轉(zhuǎn)
            button.setRotationY((float) progress);
          }
        });
    // 按鈕Z方向旋轉(zhuǎn)動畫
    rotationSeekBarZ
        .setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
          public void onStopTrackingTouch(SeekBar seekBar) {
          }
          public void onStartTrackingTouch(SeekBar seekBar) {
          }
          public void onProgressChanged(SeekBar seekBar,
              int progress, boolean fromUser) {
            // 設置旋轉(zhuǎn)
            button.setRotation((float) progress);
          }
        });
  }
}

布局文件R.layout.activity_tween

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:splitMotionEvents="true" >
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dip"
    android:orientation="horizontal"
    android:splitMotionEvents="true" >
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:paddingLeft="5dip"
      android:paddingRight="5dip"
      android:text="TX"
      android:textStyle="bold" />
    <SeekBar
      android:id="@+id/translationX"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:orientation="horizontal" />
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:paddingLeft="15dip"
      android:paddingRight="5dip"
      android:text="TY"
      android:textStyle="bold" />
    <SeekBar
      android:id="@+id/translationY"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:orientation="horizontal" />
  </LinearLayout>
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dip"
    android:orientation="horizontal"
    android:splitMotionEvents="true" >
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:paddingLeft="5dip"
      android:paddingRight="5dip"
      android:text="SX"
      android:textStyle="bold" />
    <SeekBar
      android:id="@+id/scaleX"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:orientation="horizontal" />
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:paddingLeft="15dip"
      android:paddingRight="5dip"
      android:text="SY"
      android:textStyle="bold" />
    <SeekBar
      android:id="@+id/scaleY"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:orientation="horizontal" />
  </LinearLayout>
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dip"
    android:orientation="horizontal"
    android:splitMotionEvents="true" >
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:paddingLeft="5dip"
      android:paddingRight="5dip"
      android:text="X"
      android:textStyle="bold" />
    <SeekBar
      android:id="@+id/rotationX"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:orientation="horizontal" />
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:paddingLeft="15dip"
      android:paddingRight="5dip"
      android:text="Y"
      android:textStyle="bold" />
    <SeekBar
      android:id="@+id/rotationY"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:orientation="horizontal" />
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:paddingLeft="15dip"
      android:paddingRight="5dip"
      android:text="Z"
      android:textStyle="bold" />
    <SeekBar
      android:id="@+id/rotationZ"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:orientation="horizontal" />
  </LinearLayout>
  <Button
    android:id="@+id/rotatingButton"
    android:layout_width="200dip"
    android:layout_height="150dip"
    android:layout_marginLeft="50dip"
    android:layout_marginTop="50dip"
    android:text="Rotating Button" />
</LinearLayout>

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)動畫技巧匯總》、《Android開發(fā)入門與進階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》

希望本文所述對大家Android程序設計有所幫助。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI