Android入门笔记 - 界面开发 - Animation
2014-12-08 22:51
369 查看
今天我们来看看Android中的动画效果,实例比较简单:
AlphaAnimation:透明度动画
ScaleAnimation:缩放动画
TranslateAnimation:移动位置动画
RotateAnimation:旋转角度动画
先贴代码:
这个实例完全使用代码实现的,当然也可以使用xml文件实现,我们先来看这个实例:
2. 然后就是在Activity中加载这个动画就完成了:
好了,动画其实使用起来很简单,其他的请大家自己去研究!!
AlphaAnimation:透明度动画
ScaleAnimation:缩放动画
TranslateAnimation:移动位置动画
RotateAnimation:旋转角度动画
先贴代码:
这个实例完全使用代码实现的,当然也可以使用xml文件实现,我们先来看这个实例:
package com.example.demo5_03_animation; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView mImg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImg = (ImageView) findViewById(R.id.img); Button btn_alpha = (Button) findViewById(R.id.btn_alpha); btn_alpha.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Animation animAlpha = new AlphaAnimation(1.0f, 0.0f); animAlpha.setDuration(1000*3); mImg.startAnimation(animAlpha); } }); Button btn_scale = (Button) findViewById(R.id.btn_scale); btn_scale.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Animation animScale = new ScaleAnimation(1.0f, 1.3f, 1.0f, 1.3f); animScale.setDuration(1000*3); mImg.startAnimation(animScale); } }); Button btn_translate = (Button) findViewById(R.id.btn_translate); btn_translate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Animation animAlpha = new TranslateAnimation(-mImg.getWidth(), 0, 0, 0); animAlpha.setDuration(1000*1); mImg.startAnimation(animAlpha); } }); Button btn_rotate = (Button) findViewById(R.id.btn_rotate); btn_rotate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Animation animAlpha = new RotateAnimation(0.0f, 90.0f); animAlpha.setDuration(1000*3); mImg.startAnimation(animAlpha); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }也没有什么好讲的,这里主要是给大家提供实例。
然后我们来看看用xml文件怎么实现:
1. 使用xml来配置 animation, 首先需要在 res/ 下创建 anim/ 文件夹, 创建想要的动画配置文件,比如: res/anim/anim_alpha.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:duration="2000" android:fromAlpha="1.0" android:toAlpha="0.5" /> </set>
2. 然后就是在Activity中加载这个动画就完成了:
Button btn_alpha = (Button) findViewById(R.id.btn_alpha); btn_alpha.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Animation animAlpha = new AlphaAnimation(1.0f, 0.0f); // animAlpha.setDuration(1000*3); // mImg.startAnimation(animAlpha); Animation animationAlpha = AnimationUtils.loadAnimation(mContext, R.anim.anim_alpha); mImg.startAnimation(animationAlpha); } });这里是把上一个例子中的透明度动画注释了,然后用xml加载方式写的。
好了,动画其实使用起来很简单,其他的请大家自己去研究!!
相关文章推荐
- Android入门笔记 - 界面开发 - Notification, NotificationManager
- Android入门笔记 - 界面开发 - 帧动画
- Android入门笔记 - 界面开发 - ProgressBar,Handler
- Android入门笔记 - 界面开发 - TextView,Button,EditText,Toast
- Android入门笔记 - 界面开发 - GridView, BaseAdapter
- Android入门笔记 - 界面开发 - ProgressDialog
- Android入门笔记 - 界面开发 - RadioButton,CheckBox,Spinner,AutoCompleteTextView,DatePicker,TimePicker
- Android入门笔记 - 界面开发 - Menu,Dialog
- Android游戏开发学习笔记(一):tweened animation自定义动画的实现
- Android 开发笔记——环境入门
- Android 开发学习笔记(五)—— 最简单的注册界面
- Android 开发笔记 动画效果 --Animation 动画专题研究(xml)
- Android开发入门必知:应用界面组成
- Android开发入门学习笔记之三
- Android开发入门笔记
- Android开发笔记--制作启动界面splash
- [Android开发笔记]-----做一个启动界面
- Android 开发笔记——环境入门
- android ->在界面上使用URI编程 ----开发笔记1
- Android开发入门学习笔记之二