android开发之Animations的使用(二)
本博文主要讲述的是android开发中的animation动画效果的使用,和上一篇博文不同的是,此次四种动画效果,主要使用的是xml文件实现的,提高了代码的可重用性和可维护性.
使用的基本过程例如以下:
1、首先在res目录下创建一个anim目录
2、在此目录中新建四种动画效果的xml文件(scale.xml,rotate.xml,alpha.xml,translate.xml)
3、在代码中直接使用AnimationUtils调用静态方法loadAnimation载入创建一个animation对象
4、执行animation对象
实例代码例如以下:
package com.example.animationtest2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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 MainActivity extends Activity {
private ImageView imageView = null;
private Button scaleButton = null;
private Button translateButton = null;
private Button rotateButton = null;
private Button alphaButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView)findViewById(R.id.myImage);
scaleButton = (Button)findViewById(R.id.scaleButton);
translateButton = (Button)findViewById(R.id.translateButton);
rotateButton = (Button)findViewById(R.id.rotateButton);
alphaButton = (Button)findViewById(R.id.alphaButton);
scaleButton.setOnClickListener(new setScaleListener());
translateButton.setOnClickListener(new setTranslateListener());
rotateButton.setOnClickListener(new setRotateListener());
alphaButton.setOnClickListener(new setAlphaListener());
}
//动画缩放效果监听器
class setScaleListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);
imageView.startAnimation(animation);
}
}
//动画移动效果监听器
class setTranslateListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);
imageView.startAnimation(animation);
}
}
//旋转动画效果监听器
class setRotateListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
imageView.startAnimation(animation);
}
}
//渐入渐出动画效果监听器
class setAlphaListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
imageView.startAnimation(animation);
}
}
@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;
}
}
主布局文件main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<LinearLayout
android:id="@+id/imgLayout"
android:layout_below="@id/myText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="55dp"
>
<ImageView
android:id="@+id/myImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
</LinearLayout>
<Button
android:id="@+id/scaleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/imgLayout"
android:text="@string/scale"
/>
<Button
android:id="@+id/translateButton"
android:layout_below="@id/scaleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/translate"
/>
<Button
android:id="@+id/rotateButton"
android:layout_below="@id/translateButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/rotate"
/>
<Button
android:id="@+id/alphaButton"
android:layout_below="@id/rotateButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/alpha"
/>
</RelativeLayout>
四种动画效果的xml文件分别例如以下:
scale.xml:
<?
xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="true" xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1"
android:toXScale="0"
android:fromYScale="1"
android:toYScale="0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"
/>
<!-- 50%p 表示 相对父控件的中间位置 -->
<!-- 50% 表示相对自身的中间位置 -->
<!-- 50 表示绝对位置 -->
</set>
rotate.xml:
<?
xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="true" xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="-360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"
/>
<!-- 50%p 表示 相对父控件的中间位置 -->
<!-- 50% 表示相对自身的中间位置 -->
<!-- 50 表示绝对位置 -->
<!-- 360 表示顺时针 -360表示逆时针旋转 -->
</set>
alpha.xml:
<?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="true" xmlns:android="http://schemas.android.com/apk/res/android"> <!-- set相当于AnimationSet对象 -->
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="2000"
/>
</set>
translate.xml:
<?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="true" xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%"
android:toXDelta="50%"
android:fromYDelta="0%"
android:toYDelta="100%"
android:duration="2000"
/>
<!-- 50%p 表示 相对父控件的中间位置 -->
<!-- 50% 表示相对自身的中间位置 -->
<!-- 50 表示绝对位置 -->
</set>
实现效果例如以下:
按下相关button查看相关效果
相关文章
- android 置灰不可点击,Android Studio 运行按钮灰色的完美解决方法
- Eclipse+ADT+Android SDK 搭建安卓开发环境「建议收藏」
- android telephony 原理解析与开发指南_Android逆向pdf
- Android Services Library_android freeware
- Android开发使用Gradle Kotlin的体验与建议
- 【Android应用开发】 Universal Image Loader ( 使用简介 | 示例代码解析 )
- 【Android FFMPEG 开发】FFMPEG 音视频同步 ( 音视频同步方案 | 视频帧 FPS 控制 | H.264 编码 I / P / B 帧 | PTS | 音视频同步 )
- 【Android 高性能音频】Oboe 开发流程 ( 导入 Oboe 库 | 使用预构建的二进制库和头文件 | 编译 Oboe 源码 )
- 【Android 安全】DEX 加密 ( Java 工具开发 | 解压 apk 文件 | 加密生成 dex 文件 | 打包未签名 apk 文件 | 文件解压缩相关代码 )
- 【Android NDK 开发】Kotlin 语言中使用 NDK ( 创建支持 Kotlin 的 NDK 项目 | Kotlin 语言中使用 NDK 要点 | 代码示例 )
- 【Android 组件化】使用 Gradle 实现组件化 ( 组件 / 集成模式下的 Library Module 开发 )
- 【Android 逆向】Android 进程注入工具开发 ( Visual Studio 开发 Android NDK 应用 | VS 自带的 Android 平台应用创建与配置 )
- 【Android 逆向】Android 进程注入工具开发 ( 总结 | 源码编译 | 逆向环境搭建使用 | 使用进程注入工具进行逆向操作 ) ★★★
- 【Android Gradle 插件】组件化中的 Gradle 构建脚本实现 ⑤ ( 优化 Gradle 构建脚本 | 构建脚本结构 | 闭包定义及用法 | 依赖配置 | android 块配置 )
- Android统计图表绘制:基于新版MPAndroidChart绘制数理统计柱状图详解手机开发
- Android Volley网络通信框架的使用(一):Volley的简单用法详解手机开发
- Android开发中遇到的问题(二)——新建android工程的时候eclipse没有生成MainActivity和layout布局详解手机开发
- [android] fragment的生命周期和通讯详解手机开发
- [android] 内容提供者实现详解手机开发
- Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅详解手机开发
- android画图之贝塞尔曲线讲解详解手机开发
- android插件化之路详解手机开发
- Android开发之Path详解手机开发
- Android Remote Views详解手机开发
- Android Studio2.3支持构建缓存,以及更多新特性详解手机开发
- Android Studio 导入项目 出现安装Error:Cause: failed to find target with hash string ‘android-23’ 等错误详解手机开发
- android sdk里的各目录作用详解手机开发
- Android 写一个属于自己的Rxjava(一)详解手机开发
- android开发教程之ubuntu使用adb连接小米2的步骤和adb调试方法
- android开发教程之使用线程实现视图平滑滚动示例