目錄
scale动画?调节尺寸
alpha动画?调节透明度
rotate动画?旋转
translate动画?平移
首頁 web前端 html教學 Android动画学习--Tween Animation_html/css_WEB-ITnose

Android动画学习--Tween Animation_html/css_WEB-ITnose

Jun 24, 2016 am 11:43 AM

目录

  • 目录
  • Android动画学习
  • Tween Animation
  • scale动画调节尺寸
  • alpha动画调节透明度
  • rotate动画旋转
  • translate动画平移
  • Android动画学习

    android中动画分为3种:

    1. Tween Animation:通过对场景里的对象不断做图像变换(平移、缩放、旋转)产生的动画效果,即是一种渐变动画。
    2. Frame Animation:顺序播放事先做好的图像,是一种画面转换动画。
    3. Property Animation:属性动画,通过动态地改变对象的属性从而达到动画效果,属性动画为API 11新特性。

    这篇博客主要来分析Tween Animation的基本使用。

    Tween Animation

    Tween Animation有四种形式:

    1. alpha : 渐变透明动画效果。
    2. scale : 渐变尺寸伸缩动画效果。
    3. translate : 画面位置移动动画效果。
    4. rotate : 画面旋转动画效果。

    这四种动画实现方式都是通过Animation类和AnimationUtils配合实现的。动画效果可以预先配置在res/anim目录下的xml文件中。

    scale动画?调节尺寸

    scale的参数如下:

  • android:fromXScale:起始的X方向上相对自身的缩放比例,浮点值。例如1.0代表无变化,0.5代表起始时虽小一倍,2.0代表放大一倍。
  • android:toXScale:结束时X方向上相对自身的缩放比例。
  • android:fromYScale:起始时Y方向上相对自身的缩放比例。
  • android:toYScale:结束时Y方向上相对自身的缩放比例。
  • android:pivotX:缩放起点X轴坐标,可以是数值、百分比、百分数三种形式。(注意:起点指的是当前View左上角的坐标)
  • android:pivotY:缩放起点Y轴坐标。
  • 此外,Animation类是所有动画(scale、alpha、translate、rotate)的基类,从Animation类继承的属性:

  • android:duration:动画持续时间,以毫秒为单位。
  • android:fillAfter:如果设置为true,控件动画结束时,将保持动画最后时的状态。
  • android:fillBefore:如果设置为true,控件动画结束后,还原到开机动画前的状态。
  • android:fillEnabled:与android:fillBefore的效果相同,都是在动画结束时将控件还原到初始状态。
  • android:repeatCount:重复次数
  • android:repeatMode:重复类型,有reverse和restart两个值,reverse表示倒序回放,restart表示重新放一遍。
  • 光说不练习是不行的,这里给出一个anim的xml文件、一个layout布局文件和一个Activity类,来练习一下scale animation的使用。

    res/anim/anim_scale.xml文件如下:

    <?xml version="1.0" encoding="utf-8"?><scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="700" android:fromXScale="0.0" android:fromYScale="0.0" android:pivotX="150" android:pivotY="300" android:toXScale="1.4" android:toYScale="1.4" android:fillAfter="true" android:repeatCount="10" android:repeatMode="reverse"></scale>
    登入後複製

    res/layout/tween_animation_layout.xml文件如下:

    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >    <LinearLayout  android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" >        <Button  android:id="@+id/alpha_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/alpha" />        <Button  android:id="@+id/scale_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/scale" />        <Button  android:id="@+id/rotate_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/rotate" />        <Button  android:id="@+id/trans_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/trans" />    </LinearLayout>    <ImageView  android:id="@+id/anim_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:contentDescription="@null" android:src="@drawable/splash_logo" /></LinearLayout>
    登入後複製

    动画演示实现类实现如下:

    import com.example.photocrop.R;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.Button;import android.widget.ImageView;public class TweenAnimationTest extends Activity implements OnClickListener {    private Button alphaButton;    private Button scaleButton;    private Button rotateButton;    private Button transButton;    private ImageView animImageView;    @Override    protected void onCreate(Bundle savedInstanceState) {    // TODO Auto-generated method stub    super.onCreate(savedInstanceState);    setContentView(R.layout.tween_animation_layout);    initView();    }    private void initView() {    animImageView = (ImageView) findViewById(R.id.anim_image);    alphaButton = (Button) findViewById(R.id.alpha_button);    scaleButton = (Button) findViewById(R.id.scale_button);    rotateButton = (Button) findViewById(R.id.rotate_button);    transButton = (Button) findViewById(R.id.trans_button);    alphaButton.setOnClickListener(this);    scaleButton.setOnClickListener(this);    rotateButton.setOnClickListener(this);    transButton.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {    case R.id.alpha_button:        break;    case R.id.scale_button:        Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_scale);        animImageView.startAnimation(animation);        break;    case R.id.rotate_button:        break;    case R.id.trans_button:        break;        }    }}
    登入後複製

    alpha动画?调节透明度

    alpha的参数如下:

  • android:fromAlpha:动画开始的透明度,从0.0~1.0,0.0表示完全透明,1.0表示完全不透明。
  • android:toAlpha:动画结束的透明度,也是从0.0~1.0。
  • 从Animation类继承的属性:

  • android:duration:动画持续时间,以毫秒为单位。
  • android:fillAfter:如果设置为true,控件动画结束时,将保持动画最后时的状态。
  • android:fillBefore:如果设置为true,控件动画结束后,还原到开机动画前的状态。
  • android:fillEnabled:与android:fillBefore的效果相同,都是在动画结束时将控件还原到初始状态。
  • android:repeatCount:重复次数
  • android:repeatMode:重复类型,有reverse和restart两个值,reverse表示倒序回放,restart表示重新放一遍。
  • 我们以上一个TweenAnimationTest类为模板,当点击alpha button的时候,我们触发透明度动画效果。

    res/anim/anim_alpha.xml文件如下:

    <?xml version="1.0" encoding="utf-8"?><alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:fillBefore="true" android:fromAlpha="0.0" android:toAlpha="1.0" ></alpha>
    登入後複製

    响应点击alpha button的listener事件为:

    case R.id.alpha_button:    Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);    animImageView.startAnimation(alphaAnimation);    break;
    登入後複製

    rotate动画?旋转

    rotate参数如下:

  • android:fromDegrees:开始旋转的角度位置,正数值代表顺时针的角度,负数值代表逆时针旋转的角度。
  • android:toDegrees:结束时旋转到的角度位置,正数值代表顺时针的角度,负数值代表逆时针旋转的角度。
  • android:pivotX:旋转起点的X轴坐标。
  • android:pivotY:旋转起点的Y轴坐标。
  • 从Animation类继承的属性:

  • android:duration:动画持续时间,以毫秒为单位。
  • android:fillAfter:如果设置为true,控件动画结束时,将保持动画最后时的状态。
  • android:fillBefore:如果设置为true,控件动画结束后,还原到开机动画前的状态。
  • android:fillEnabled:与android:fillBefore的效果相同,都是在动画结束时将控件还原到初始状态。
  • android:repeatCount:重复次数
  • android:repeatMode:重复类型,有reverse和restart两个值,reverse表示倒序回放,restart表示重新放一遍。
  • res/anim/anim_rotate.xml文件如下:

    <?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fillAfter="true" android:fromDegrees="0" android:pivotX="0" android:pivotY="0" android:repeatCount="3" android:toDegrees="810" ></rotate>
    登入後複製

    响应点击alpha button的listener事件为:

    case R.id.rotate_button:    Animation rotateAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);    animImageView.startAnimation(rotateAnimation);    break;
    登入後複製

    translate动画?平移

    translate参数:

  • android:fromXDelta:起始X轴坐标。
  • android:fromYDelta:起始Y轴坐标。
  • android:toXDelta:结束X轴坐标。
  • android:toYDelta:结束Y轴坐标。
  • 从Animation类继承的属性:

  • android:duration:动画持续时间,以毫秒为单位。
  • android:fillAfter:如果设置为true,控件动画结束时,将保持动画最后时的状态。
  • android:fillBefore:如果设置为true,控件动画结束后,还原到开机动画前的状态。
  • android:fillEnabled:与android:fillBefore的效果相同,都是在动画结束时将控件还原到初始状态。
  • android:repeatCount:重复次数
  • android:repeatMode:重复类型,有reverse和restart两个值,reverse表示倒序回放,restart表示重新放一遍。
  • 注意:京东splash页面的进度条用的就是translate动画。

    res/anim/anim_rotate.xml文件如下:

    <?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fillAfter="true" android:fromXDelta="0.0" android:toXDelta="60%p" ></translate>
    登入後複製

    响应点击alpha button的listener事件为:

    case R.id.trans_button:    Animation transAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_translate);    animImageView.startAnimation(transAnimation);    break;
    登入後複製
    本網站聲明
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

    熱AI工具

    Undresser.AI Undress

    Undresser.AI Undress

    人工智慧驅動的應用程序,用於創建逼真的裸體照片

    AI Clothes Remover

    AI Clothes Remover

    用於從照片中去除衣服的線上人工智慧工具。

    Undress AI Tool

    Undress AI Tool

    免費脫衣圖片

    Clothoff.io

    Clothoff.io

    AI脫衣器

    AI Hentai Generator

    AI Hentai Generator

    免費產生 AI 無盡。

    熱門文章

    R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
    3 週前 By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O.最佳圖形設置
    3 週前 By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O.如果您聽不到任何人,如何修復音頻
    3 週前 By 尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25:如何解鎖Myrise中的所有內容
    3 週前 By 尊渡假赌尊渡假赌尊渡假赌

    熱工具

    記事本++7.3.1

    記事本++7.3.1

    好用且免費的程式碼編輯器

    SublimeText3漢化版

    SublimeText3漢化版

    中文版,非常好用

    禪工作室 13.0.1

    禪工作室 13.0.1

    強大的PHP整合開發環境

    Dreamweaver CS6

    Dreamweaver CS6

    視覺化網頁開發工具

    SublimeText3 Mac版

    SublimeText3 Mac版

    神級程式碼編輯軟體(SublimeText3)

    &gt; gt;的目的是什麼 元素? &gt; gt;的目的是什麼 元素? Mar 21, 2025 pm 12:34 PM

    本文討論了HTML&lt; Progress&gt;元素,其目的,樣式和與&lt; meter&gt;元素。主要重點是使用&lt; progress&gt;為了完成任務和LT;儀表&gt;對於stati

    &lt; datalist&gt;的目的是什麼。 元素? &lt; datalist&gt;的目的是什麼。 元素? Mar 21, 2025 pm 12:33 PM

    本文討論了html&lt; datalist&gt;元素,通過提供自動完整建議,改善用戶體驗並減少錯誤來增強表格。Character計數:159

    &lt; meter&gt;的目的是什麼。 元素? &lt; meter&gt;的目的是什麼。 元素? Mar 21, 2025 pm 12:35 PM

    本文討論了HTML&lt; meter&gt;元素,用於在一個範圍內顯示標量或分數值及其在Web開發中的常見應用。它區分了&lt; meter&gt;從&lt; progress&gt;和前

    HTML5中跨瀏覽器兼容性的最佳實踐是什麼? HTML5中跨瀏覽器兼容性的最佳實踐是什麼? Mar 17, 2025 pm 12:20 PM

    文章討論了確保HTML5跨瀏覽器兼容性的最佳實踐,重點是特徵檢測,進行性增強和測試方法。

    如何使用HTML5表單驗證屬性來驗證用戶輸入? 如何使用HTML5表單驗證屬性來驗證用戶輸入? Mar 17, 2025 pm 12:27 PM

    本文討論了使用HTML5表單驗證屬性,例如必需的,圖案,最小,最大和長度限制,以直接在瀏覽器中驗證用戶輸入。

    視口元標籤是什麼?為什麼對響應式設計很重要? 視口元標籤是什麼?為什麼對響應式設計很重要? Mar 20, 2025 pm 05:56 PM

    本文討論了視口元標籤,這對於移動設備上的響應式Web設計至關重要。它解釋瞭如何正確使用確保最佳的內容縮放和用戶交互,而濫用可能會導致設計和可訪問性問題。

    我如何使用html5&lt; time&gt; 元素以語義表示日期和時間? 我如何使用html5&lt; time&gt; 元素以語義表示日期和時間? Mar 12, 2025 pm 04:05 PM

    本文解釋了HTML5&lt; time&gt;語義日期/時間表示的元素。 它強調了DateTime屬性對機器可讀性(ISO 8601格式)的重要性,並在人類可讀文本旁邊,增強Accessibilit

    &lt; iframe&gt;的目的是什麼。 標籤?使用時的安全考慮是什麼? &lt; iframe&gt;的目的是什麼。 標籤?使用時的安全考慮是什麼? Mar 20, 2025 pm 06:05 PM

    本文討論了&lt; iframe&gt;將外部內容嵌入網頁,其常見用途,安全風險以及諸如對象標籤和API等替代方案的目的。

    See all articles