Android入门笔记 - 界面开发 - Animation
16lz
2021-01-26
今天我们来看看Android中的动画效果,实例比较简单:
- 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;@Overrideprotected 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic void onClick(View v) {Animation animAlpha = new RotateAnimation(0.0f, 90.0f);animAlpha.setDuration(1000*3);mImg.startAnimation(animAlpha);}});}@Overridepublic 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() {@Overridepublic 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(安卓)animation - 基础
- Activity四种启动模式(整合)
- python服务器与android客户端socket通信实例
- Android中判断手机是否联网实例
- Android中使用HTTP服务上传文件
- Android(安卓)环形布局: FloatingActionButton + ConstraintLayou
- Android从Fragment跳转到其他Activity的简单实例
- MTK Android(安卓)P 开机无动画,无铃声问题和开机铃声不同步,首次
- Android(安卓)Activity 启动模式(Launch Mode)