Android动画学习--Tween Animation_html/css_WEB-ITnose
目录
android中动画分为3种:
- Tween Animation:通过对场景里的对象不断做图像变换(平移、缩放、旋转)产生的动画效果,即是一种渐变动画。
- Frame Animation:顺序播放事先做好的图像,是一种画面转换动画。
- Property Animation:属性动画,通过动态地改变对象的属性从而达到动画效果,属性动画为API 11新特性。
这篇博客主要来分析Tween Animation的基本使用。
Tween AnimationTween Animation有四种形式:
- alpha : 渐变透明动画效果。
- scale : 渐变尺寸伸缩动画效果。
- translate : 画面位置移动动画效果。
- rotate : 画面旋转动画效果。
这四种动画实现方式都是通过Animation类和AnimationUtils配合实现的。动画效果可以预先配置在res/anim目录下的xml文件中。
scale动画?调节尺寸
scale的参数如下:
此外,Animation类是所有动画(scale、alpha、translate、rotate)的基类,从Animation类继承的属性:
光说不练习是不行的,这里给出一个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的参数如下:
从Animation类继承的属性:
我们以上一个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参数如下:
从Animation类继承的属性:
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参数:
从Animation类继承的属性:
注意:京东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;

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

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

熱門話題

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

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

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

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

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

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

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