Android中补间动画1----Animation的基本使用代码实现(平移,缩放,渐变,旋转)
2017-11-21 17:40
996 查看
效果图:
<?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#
源码下载:
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#
源码下载:
相关文章推荐
- Android中补间动画2----Animation的基本使用代码实现复合使用
- Android中属性动画1----ObjectAnimator的基本使用(平移,缩放,渐变,旋转)
- android游戏开发5-10 补间动画 透明度渐变 旋转 缩放 平移
- Android补间动画基本使用(位移、缩放、旋转、透明)
- 动画效果之渐变、缩放、平移、旋转<代码实现>
- 【Android动画】之Tween动画 (渐变、缩放、位移、旋转)代码中的实现
- Android 实现属性动画平移,旋转,缩放,渐变 《H》
- Android开发--图形图像与动画(二)--Animation实现图像的 渐变、缩放、位移、旋转
- Android-补间动画基本使用(位移、缩放、旋转、透明)
- Android 补间动画(动画列表,平移,旋转,缩放,渐变,动画集合)
- Android旋转、平移、缩放和透明度渐变的补间动画
- 【Android开发】动画范例2-旋转、平移、缩放和透明度渐变的补间动画
- Android开发--图形图像与动画(二)--Animation实现图像的 渐变、缩放、位移、旋转
- Android 补间动画,具体介绍了透明度、平移,旋转,缩放4种动画效果的实现,简单易懂
- Android动画之渐变动画(Tween Animation)详解 (渐变、缩放、位移、旋转)
- Android基础-简单的动画实现 *补间动画 *透明度渐变 *旋转动画 *位移动画 *如果需要几个效果整合到一起,需要将所有效果全部放到set里
- 讲述属性动画的使用 —使用动画旋转、平移、渐变和缩放
- android 中四种补间动画(透明,平移,缩放,旋转)
- android 视图动画的使用,Animation重写 实现旋转效果
- Android基础-简单的动画实现 *补间动画 *透明度渐变 *旋转动画 *位移动画 *如果需要几个效果整合到一起,需要将所有效果全部放到set里