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

这篇博客详细介绍了如何在Android中使用Animation实现平移、缩放、渐变和旋转等补间动画效果,包括代码实现和实际效果图展示,适合Android开发者学习动画技术。
摘要由CSDN通过智能技术生成

效果图:


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#

源码下载:


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,作为一个语言模型 AI,我无法提交演示程序的运行效果图。但我可以告诉您如何实现这四种动画。 在Android程序,可以通过使用Animation类和它的子类,实现四种动画: 1. 透明度渐变动画 使用AlphaAnimation类实现透明度渐变动画。可以通过设置动画的起始透明度和结束透明度,以及动画的持续时来控制动画效果。 ``` AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f); alphaAnimation.setDuration(1000); view.startAnimation(alphaAnimation); ``` 2. 旋转动画 使用RotateAnimation类实现旋转动画。可以通过设置动画的起始角度和结束角度,以及动画的持续时来控制动画效果。 ``` RotateAnimation rotateAnimation = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation.setDuration(1000); view.startAnimation(rotateAnimation); ``` 3. 动画 使用ScaleAnimation类实现动画。可以通过设置动画的起始大小和结束大小,以及动画的持续时来控制动画效果。 ``` ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnimation.setDuration(1000); view.startAnimation(scaleAnimation); ``` 4. 平移动画 使用TranslateAnimation类实现平移动画。可以通过设置动画的起始位置和结束位置,以及动画的持续时来控制动画效果。 ``` TranslateAnimation translateAnimation = new TranslateAnimation(0.0f, 100.0f, 0.0f, 100.0f); translateAnimation.setDuration(1000); view.startAnimation(translateAnimation); ``` 以上是四种动画基本实现方式,可以根据需要进行修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值