您的位置:首页 > 移动开发 > Android开发

Android中补间动画1----Animation的基本使用代码实现(平移,缩放,渐变,旋转)

2017-11-21 17:40 996 查看
效果图:



MainActivity

package com.zhh.android;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 Button btnScale;
//  旋转
private Button btnRotate;
//  渐变
private Button btnAlpha;
//  平移动画
private Button btnTranslation;

private ImageView ivLauncher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
myOnclick();
}
/**
* 初始化控件
*/
private void initView() {
btnScale = (Button)findViewById(R.id.btnScale);
btnRotate = (Button)findViewById(R.id.btnRotate);
ivLauncher = (ImageView)findViewById(R.id.ivLauncher);
btnAlpha = (Button)findViewById(R.id.btnAlpha);
btnTranslation = (Button)findViewById(R.id.btnTranslation);
}
/**
* 点击事件
*/
private void myOnclick() {
//      缩放
btnScale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setScaleAnimation();
}
});
//      旋转
btnRotate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setRotateAnimation();
}
});
//      渐变
btnAlpha.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setAlphaAnimation();
}
});
//      平移
btnTranslation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setTranslationAnimation();
}
});
//      跳转到下一页
ivLauncher.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,Main2Activity.class));
}
});
}
/**
* 缩放动画
*/
private void setScaleAnimation(){
//               效果:宽度从0.5到1.5,高度从0.0到1.0,缩放的圆心为顶部中心点,延迟1s开始,持续2s,最终还原
//               1创建动画对象
//              Animation.ABSOLUTE绝对的
//        ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f,1.5f,0.0f,1.0f,
//                Animation.ABSOLUTE,ivLauncher.getWidth()/2,Animation.ABSOLUTE,0f);
//              Animation.RELATIVE_TO_SELF相对自己
ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f,1.5f,0.0f,1.0f,
Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0f);
//               2设置
//                延迟一秒开始
scaleAnimation.setStartOffset(1000);
//                持续两秒
scaleAnimation.setDuration(2000);
//                最终还原
scaleAnimation.setFillBefore(true);
//               3启动动画
ivLauncher.startAnimation(scaleAnimation);
}
/**
* 旋转动画
*/
private void setRotateAnimation(){
//        效果以图片中心点为中心,从负90度到正90度,持续5s
//        1创建动画对象
RotateAnimation rotateAnimation = new RotateAnimation(-90,90,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//        2设置
rotateAnimation.setDuration(5000);
//        3启动动画
ivLauncher.startAnimation(rotateAnimation);

}
/**
* 渐变动画
*/
private void setAlphaAnimation(){
//        效果:从完全透明,到完全不透明
//        1创建动画对象
AlphaAnimation alphaAnimation =new AlphaAnimation(0f,1f);
//        2设置
alphaAnimation.setDuration(5000);
//        3启动动画
ivLauncher.startAnimation(alphaAnimation);
}

/**
* 平移动画
*/
private void setTranslationAnimation(){
//        效果,向右移动一个自己的宽度,向下移动一个自己的高度,持续2秒
//      1创建动画对象   Animation.RELATIVE_TO_SELF,0f, Animation.RELATIVE_TO_SELF,平移的是左上角的坐标
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f, Animation.RELATIVE_TO_SELF,1f, Animation.RELATIVE_TO_SELF,0f,  Animation.RELATIVE_TO_SELF,1f);
//      2设置
translateAnimation.setDuration(2000);
//      3启动动画
ivLauncher.startAnimation(translateAnimation);
}

}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context="com.zhh.android.MainActivity"
android:orientation="vertical"
>
<Button
android:id="@+id/btnScale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="缩放动画"
android:textSize="30dp"
/>
<Button
android:id="@+id/btnRotate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="旋转动画"
android:textSize="30dp"
/>
<Button
android:id="@+id/btnAlpha"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="渐变动画"
android:textSize="30dp"
/>
<Button
android:id="@+id/btnTranslation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="平移动画"
android:textSize="30dp"
/>
<ImageView
android:id="@+id/ivLauncher"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:src="@mipmap/cyl"
android:layout_gravity="center_horizontal"
/>

</LinearLayout>
参考视频:
http://www.gulixueyuan.com/course/112/task/1793/show#

源码下载:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐