Animation的使用「建议收藏」

Animation的使用「建议收藏」Animation(动画)有两种分类:补间动画(Tween)和帧动画(Frame)补间动画主要有以下几种:旋转(RotateAnimation)平移(TranslateAnimation)拉伸(ScaleAnimation)透明度(AlphaAnimation)实现的方式:1.实例相应的动画对象2.加载资源中的动画文件动画的属性d

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

Animation(动画)有两种分类:补间动画(Tween)和帧动画(Frame)

补间动画主要有以下几种:

旋转(RotateAnimation)

平移(TranslateAnimation)

拉伸(ScaleAnimation)

透明度(AlphaAnimation)

实现的方式:

1.实例相应的动画对象

2.加载资源中的动画文件

动画的属性

duration:动画持续的时间

filiAfter:为true保持结束时的状态,为false变回最初的状态

repeatCount:重复的次数(不包括第一次)

startOffset:距离动画开始的时间

repeatMode:1表示重新开始,2表示从最后一个状态往回逆序播放

帧动画:

实现方法

1.实例AnimationDrawable

2.在drawable新建类型为animation-list的xml文件,然后加载该文件

代码如下

package com.example.animaction;

import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.CycleInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
@SuppressLint("NewApi")
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		TextView tv = (TextView) findViewById(R.id.tv);
		/**
		 * 透明度动画
		 */
		// 方法一:
		// 参数1为起始透明度,参数2为结束透明度
		// AlphaAnimation alphaAnimation = new AlphaAnimation(1,
		// 0.5f);//1能自动转换为float,0.5默认为double值
		AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
		// 设置执行时间,默认为0
		alphaAnimation.setDuration(3000);
		// 重复次数
		alphaAnimation.setRepeatCount(3);
		// 重复模式,Animation.RESTART:从头开始,Animation.REVERSE:逆序
		alphaAnimation.setRepeatMode(Animation.REVERSE);
		// 设置结束时状态,为true保持结束时状态,false变回原先的状态
		alphaAnimation.setFillAfter(true);

		// 方法二:
		// 加载资源中的动画文件
		Animation alphaAnimation1 = AnimationUtils.loadAnimation(this,
				R.anim.alpha_anim);
		tv.startAnimation(alphaAnimation1);

		/**
		 * 平移动画
		 */
		/*
		 * @parm
		 * fromXType-->起始x坐标的类型,三种类型:Animation.ABSOLUTE,RELATIVE_TO_PARENT,
		 * RELATIVE_TO_SELF
		 * 
		 * @parm fromXValue-->如果类型为ABSOLUTE,值就为绝对值,单位px
		 * RELATIVE_TO_PARENT,相对于父控件的位置,值为float(-1~1) RELATIVE_TO_SELF:相对自身控件的位置
		 * 
		 * @parm toXType -->结束时x坐标的位置
		 * 
		 * @parm toXValue -->结束时x坐标的值 其他四个参数与上面一样
		 * 
		 * 另一个构造方法默认使用ABSOLUTE类型
		 */
		// 表示从相对自身控件0.5的位置水平平移到父控件0.5的位置
		TranslateAnimation translateAnimation = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_PARENT,
				0.5f, 0, 0, 0, 0);
		translateAnimation.setDuration(3000);
		tv.startAnimation(translateAnimation);

		/**
		 * 缩放动画
		 */
		/*
		 * x方向:fromX ,toX-->缩放的比例从0.5(会直接变成原来的一半,没有动画效果)变成1.5 y方向:fromY ,toY
		 * 后面四个参数确定缩放的中心点。 4个参数类型为相对自身控件 6个参数类型为绝对的
		 */
		// 如果类型是相对父控件,就相当于把控件拉大到父控件中心的位置
		ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1.0f, 0, 1.0f,
				Animation.RELATIVE_TO_PARENT, 0.5f,
				Animation.RELATIVE_TO_PARENT, 0.5f);
		scaleAnimation.setDuration(3000);
		tv.startAnimation(scaleAnimation);

		/**
		 * 旋转动画
		 */
		/*
		 * fromDegrees-->起始角度,toDegrees-->结束角度 后面四个参数为确定旋转的中心点
		 */
		RotateAnimation rotateAnimation = new RotateAnimation(0, 180,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		rotateAnimation.setDuration(5000);
		// 循环播放,参数循环的次数,会在setDuration的时间中循环完,循环为0~180,0~-180算一次
		CycleInterpolator cycleInterpolator = new CycleInterpolator(20);
		rotateAnimation.setInterpolator(cycleInterpolator);
		tv.startAnimation(rotateAnimation);

		/**
		 * 动画集合
		 */
		// 方法一:
		Animation set = AnimationUtils.loadAnimation(this, R.anim.set);
		// start马上执行,set可能不会马上执行
		tv.startAnimation(set);

		// 如果参数为true,所以动画都执行同一个变速器即set设置的变速器,false执行各自的变速器
		AnimationSet set2 = new AnimationSet(false);
		set2.addAnimation(rotateAnimation);
		scaleAnimation.setStartOffset(10000);
		set2.addAnimation(scaleAnimation);
		
		
		/**
		 * 帧动画
		 */
		//第一种
		//AnimationDrawable间接继承Drawable
		AnimationDrawable ad = new AnimationDrawable();
		Drawable frame1 = getResources().getDrawable(R.drawable.ic_launcher);
		ad.addFrame(frame1, 1000/*显示的时间*/);
		//动画只执行一次
		ad.setOneShot(true);
		tv.setBackground(ad);
		ad.start();//开始动画
		ad.stop();//结束之后再开始从头开始播放
		
		//第二种
		tv.setBackgroundResource(R.drawable.list);
		AnimationDrawable ad1 = (AnimationDrawable) tv.getBackground();
		ad1.start();
	}

	@Override
	public void finish() {
		super.finish();
		// 当startActivity或者finish之后,设置页面切换的动作效果
		overridePendingTransition(/* 进入的页面的动画 */R.anim.alpha_anim, /* 离开的页面的动画 */
				R.anim.set);
	}

}

Jetbrains全家桶1年46,售后保障稳定

XMLz

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1"
    android:toAlpha="0.5"
    android:duration="3000"
    android:repeatMode="restart"
    android:repeatCount="3">
    

</alpha>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <alpha
        android:duration="3000"
        android:fromAlpha="0"
        android:toAlpha="1" />

    <!-- 只能设置相对自身的缩放 -->
    <scale
        android:duration="3000"
        android:fromXScale="0"
        android:fromYScale="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1"
        android:toYScale="1" />
    
    <rotate 
        android:duration="3000"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fromDegrees="0"
        android:toDegrees="360"/>
    
    <translate 
        android:duration="3000"
        android:fromXDelta="0"
        android:toXDelta="100%"/>

    <!-- android:startOffset="3000":三秒之后才会执行 -->
    <alpha
        android:duration="3000"
        android:fromAlpha="1"
        android:startOffset="3000"
        android:toAlpha="0" />

</set>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/230824.html原文链接:https://javaforall.net

(0)
上一篇 2025年6月17日 下午12:15
下一篇 2025年6月17日 下午12:43


相关推荐

  • 豆包怎么使用详细教程

    豆包怎么使用详细教程

    2026年3月12日
    5
  • pycharm如何设置背景图片_如何设置word背景图片

    pycharm如何设置背景图片_如何设置word背景图片打开Pycharm点击左上角File如图:然后选择找到Settings点击进入,打开Appearance&Behavior,选择Appearance如图:最后找到BackgroundImage,选择好自己要设置的图片,Opacity可以调解好透明度,点击OK就可以了看效果:…

    2022年8月25日
    11
  • 数据库读写分离的理解

    数据库读写分离的理解读写分离 基本的原理是让主数据库处理事务性增 改 删操作 INSERT UPDATE DELETE 而从数据库处理 SELECT 查询操作 数据库复制被用来把事务性操作导致的变更同步到集群中的从数据库 nbsp nbsp nbsp nbsp nbsp nbsp 为什么要分库 分表 读写分 nbsp nbsp nbsp nbsp nbsp nbsp 单表的数据量限制 当单表数据量到一定条数之后数据库性能会显著下降 数据多了之后 对数据库的读 写就会很多 分库减少单台数据库的压力 接触过几个

    2026年3月17日
    1
  • 大模型开发指南:12款热门AI Agent工具对比分析,建议程序员收藏学习

    大模型开发指南:12款热门AI Agent工具对比分析,建议程序员收藏学习

    2026年3月16日
    4
  • 三、行列式的几何意义

    三、行列式的几何意义三 行列式的几何意义 nbsp 行列式的定义 行列式是由一些数据排列成的方阵经过规定的计算方法而得到的一个数 当然 如果行列式中含有未知数 那么行列式就是一个多项式 它本质上代表一个数值 这点请与矩阵区别开来 矩阵只是一个数表 行列式还要对这个数表按照规则进一步计算 最终得到一个实数 复数或者多项式 一阶行列式 注意不是绝对值 二阶行列式三阶行列式 N 阶行列式

    2026年3月26日
    2
  • jsp分页功能实现两种方法(html如何实现分页功能)

    本期的jsp入门学习内容:实现JSP分页显示的方法。今天给大家带来实现jsp分页显示的代码,简单的7个步骤就可以实现JSP的分页显示,有需要的朋友可以参考一下,学习些jsp开发的知识。正式开始此次的jsp入门教程的学习:1、MySQL的limit关键字(DAO)2、jQuery load函数(页面JS)MySQL的limit关键词能够完结抽取必定规模(n

    2022年4月17日
    275

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号