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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • MySQL数据库(基础)

    MySQL数据库(基础)目录1.数据库概念1.1数据库是干嘛的?1.2数据库和数据结构是啥关系?​1.3两种类型的数据库2.MySQL数据库2.1MySQL数据库概念2.2MySQL基本操作2.2.1建立数据库2.2.2查看数据库2.2.3选中数据库2.2.4删除数据库2.3MySQL数据类型1.数据库概念1.1数据库是干嘛的?数据库的功能就是用来组织数据,组织很多很多的数据。这些数据通常都是存储在外存(磁盘)数据库提供的核…

    2022年7月24日
    3
  • 免备案空间收集

    免备案空间收集用了这个国外的免备空间,觉得其他的都弱爆了此空间无广告,大空间,速度快,而且是即时开通的,我去截图分享下。虽然是美国的,但是提供中文申请页面,但是用谷歌浏览器可以无障碍设置。这是登录进去时候的页面:这个是我目前的两个域名,每过24小时可以创建一个。这个是控制面板这个是我的账户信息可以看一下磁盘容量和带宽,都是大到用不完···,跟其他的需要付神马1块两块钱别人才提供给…

    2022年6月18日
    24
  • math.random()随机整数_随机函数rand公式

    math.random()随机整数_随机函数rand公式Math.round(Math.random()*x);Math.round(Math.random()*(y-x)+x);Math.ceil(Math.random()*x);

    2022年8月4日
    4
  • 新东方网课资源分享_新东方笔记大赛官网

    新东方网课资源分享_新东方笔记大赛官网
    新东方李老师的734条高频词组笔记(实在是太有用了!就转来了,没看过的,赶紧点吧~)

    1.abideby(=befaithfulto;obey)忠于;遵守。
    2.beabsentfrom….缺席,不在
    3.absenceofmind(=beingabsent-minded)心不在焉
    4.absorb(=takeuptheattentionof)吸引…的注意力(被动语态)beabsorbedin

    2022年9月12日
    3
  • 全局数据库名称.数据库名称.SID是什么关系?

    全局数据库名称.数据库名称.SID是什么关系?

    2022年2月24日
    44
  • python字符串的使用方法_python字符串常用函数

    python字符串的使用方法_python字符串常用函数python字符串常用方法find(sub[,start[,end]])在索引start和end之间查找字符串sub​找到,则返回最左端的索引值,未找到,则返回-1​start和end都可

    2022年7月28日
    4

发表回复

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

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