Android实现缩放动画

本文实例为大家分享了Android实现缩放动画的具体代码,供大家参考,具体内容如下

核心方法

public void startAnimation(Animation animation)

执行动画,参数可以是各种动画的对象,Animation的多态,也可以是组合动画,后面会有。

4个参数构造方法

/**
 * Constructor to use when building a ScaleAnimation from code
 * 
 * @param fromX Horizontal scaling factor to apply at the start of the animation
 * @param toX Horizontal scaling factor to apply at the end of the animation
 * @param fromY Vertical scaling factor to apply at the start of the animation
 * @param toY Vertical scaling factor to apply at the end of the animation
 */
public ScaleAnimation(float fromX, float toX, float fromY, float toY) {
    mResources = null;
    mFromX = fromX;
    mToX = toX;
    mFromY = fromY;
    mToY = toY;
    mPivotX = 0;
    mPivotY = 0;
}

用法

public void scale(View view) {
    // 创建缩放的动画对象
    ScaleAnimation sa = new ScaleAnimation(0f,1.0f,0f,1.0f);
    // 设置动画播放的时间
    sa.setDuration(1000);
    // 开始播放动画
    iv.startAnimation(sa);
}

效果

以图片左上角为原点,从没有,放大到图片原大小

6个参数构造方法

/**
    * Constructor to use when building a ScaleAnimation from code
    * 
    * @param fromX Horizontal scaling factor to apply at the start of the animation
    * @param toX Horizontal scaling factor to apply at the end of the animation
    * @param fromY Vertical scaling factor to apply at the start of the animation
    * @param toY Vertical scaling factor to apply at the end of the animation
    * @param pivotX The X coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the left edge. (This point remains fixed while the object changes size.)
    * @param pivotY The Y coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the top edge. (This point remains fixed while the object changes size.)
    */
   public ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY) {
       mResources = null;
       mFromX = fromX;
       mToX = toX;
       mFromY = fromY;
       mToY = toY;

       mPivotXType = ABSOLUTE;
       mPivotYType = ABSOLUTE;
       mPivotXValue = pivotX;
       mPivotYValue = pivotY;
       initializePivotPoint();
   }

前4个参数和上面的用法一样,后两个参数是设置图片缩放的原点,四个参数的构造默认将这两个参数都设置了0,所以是在图片左上角开始缩放

用法

ScaleAnimation sa = new ScaleAnimation(0f, 1.0f, 0f, 1.0f, iv.getWidth() / 2, iv.getHeight() / 2);
// 设置动画播放的时间
sa.setDuration(1000);
// 开始播放动画
iv.startAnimation(sa);

效果

以图片的中心为原点,从没有放大到图片原大小

8个参数构造方法

/**
    * Constructor to use when building a ScaleAnimation from code
    * 
    * @param fromX Horizontal scaling factor to apply at the start of the animation
    * @param toX Horizontal scaling factor to apply at the end of the animation
    * @param fromY Vertical scaling factor to apply at the start of the animation
    * @param toY Vertical scaling factor to apply at the end of the animation
    * @param pivotXType Specifies how pivotXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
    * @param pivotXValue The X coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the left edge. (This point remains fixed while the object changes size.) This value can either be an absolute number if pivotXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
    * @param pivotYType Specifies how pivotYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
    * @param pivotYValue The Y coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the top edge. (This point remains fixed while the object changes size.) This value can either be an absolute number if pivotYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
    */
   public ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) {
       mResources = null;
       mFromX = fromX;
       mToX = toX;
       mFromY = fromY;
       mToY = toY;

       mPivotXValue = pivotXValue;
       mPivotXType = pivotXType;
       mPivotYValue = pivotYValue;
       mPivotYType = pivotYType;
       initializePivotPoint();
   }

用法

// 创建缩放的动画对象
ScaleAnimation sa = new ScaleAnimation(0f, 1.0f, 0f, 1.0f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
// 设置动画播放的时间
sa.setDuration(1000);
// 开始播放动画
iv.startAnimation(sa);

和上面6个参数的相比只是多了第5和第7个参数,分别设置他们的类型,注释里面已经说明了,可以设置Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF、Animation.RELATIVE_TO_PARENT类型

效果

效果和上面一样,以图片的中心为原点,从没有放大到图片原大小。

设置动画重复播放的次数的方法

/**
 * Sets how many times the animation should be repeated. If the repeat
 * count is 0, the animation is never repeated. If the repeat count is
 * greater than 0 or {@link #INFINITE}, the repeat mode will be taken
 * into account. The repeat count is 0 by default.
 *
 * @param repeatCount the number of times the animation should be repeated
 * @attr ref android.R.styleable#Animation_repeatCount
 */
public void setRepeatCount(int repeatCount) {
    if (repeatCount < 0) {
        repeatCount = INFINITE;
    }
    mRepeatCount = repeatCount;
}

使用

sa.setRepeatCount(2);

设置动画重复播放的模式的方法

/**
 * Defines what this animation should do when it reaches the end. This
 * setting is applied only when the repeat count is either greater than
 * 0 or {@link #INFINITE}. Defaults to {@link #RESTART}. 
 *
 * @param repeatMode {@link #RESTART} or {@link #REVERSE}
 * @attr ref android.R.styleable#Animation_repeatMode
 */
public void setRepeatMode(int repeatMode) {
    mRepeatMode = repeatMode;
}

使用

sa.setRepeatMode(ScaleAnimation.REVERSE);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android Compose衰减动画Animatable使用详解

    目录 前言 animateDecay splineBasedDecay rememberSplineBasedDecay exponentialDecay 实战 最后 前言 之前介绍了 Animatable 动画以及其 animateTo和 snapTo两个开启动画 api 的使用,实际上 Animatable 除了这两个 api 以外还有一个 animateDecay即本篇要介绍的衰减动画. 什么是衰减动画呢?就是动画速度由快到慢最后停止,最常见的应用场景就是惯性动画,比如滑动列表时手指松开后

  • Android代码实现新年贺卡动画示例详解

    目录 引言 需要使用到的知识点 思路分析 代码实现 调用动画框架API 引言 什么?兔了个兔?吐了还要吐?首先今天,我们自己用android程序实现一个兔年的新年贺卡.下面就是见证美好的时刻,上效果. 好,我们来使用Android动画的知识,来实现这样一个动画效果吧. 需要使用到的知识点 架构设计.Android视图动画.TypeEvaluator.Path.组合模式.代理模式. 思路分析 我们回顾动画的种类,补间动画.帧动画.属性动画以及Android View自带的视图动画.我们今天自己基于

  • Android Activity共享元素动画示例解析

    目录 正文 TransitionManager介绍 Scene(场景) 生成场景 Transition(过渡) OverlayView和ViewGroupOverlay GhostView Activity的共享元素源码分析 我们先以ActivityA打开ActivityB为例 ActivityB返回ActivityA SharedElementCallback回调总结 正文 所谓Activity共享元素动画,就是从ActivityA跳转到ActivityB 通过控制某些元素(View)从Act

  • Android 补间动画及组合AnimationSet常用方法详解

    目录 补间动画 RotateAnimation 动画示例 ScaleAnimation 动画示例 TranslateAnimation 动画示例 AlphaAnimation 动画示例 AnimationSet 动画组合 动画示例 补间动画 Android常用的四种补间动画分别为RotateAnimation.ScaleAnimation.TranslateAnimation.AlphaAnimation,他们的父类为Animation,UML类图如下: 父类通用方法有: public void

  • Android Compose 属性动画使用探索详解

    目录 前言 使用探索 ObjectAnimator 使用探索 ValueAnimator 使用探索 Compose 函数中使用属性动画 实战 上传开始动画 上传进度动画 上传完成动画 最后 前言 Jetpack Compose(简称 Compose )是 Google 官方推出的基于 Kotlin 语言的 Android 新一代 UI 开发框架,其采用声明式的 UI 编程特性使得 Android 应用界面的编写和维护变得更加简单. 本专栏将详细介绍在使用 Compose 进行 UI 开发中如何实

  • Android动效Compose贝塞尔曲线动画规格详解

    目录 正文 贝塞尔曲线 解析动画曲线 曲线源码分析 总结 正文 写Compose动画的时候使用animateXAsState的时候会注意到一个参数——animationSpec,如下: val borderRadius by animateIntAsState( targetValue = if (isRound) 100 else 0, animationSpec = tween( durationMillis = 3000, easing = LinearEasing ) ) 此处就不深入探

  • Android Flutter实现五种酷炫文字动画效果详解

    目录 前言 波浪涌动效果 波浪线跳动文字组 彩虹动效 滚动广告牌效果 打字效果 其他效果 自定义效果 总结 前言 偶然逛国外博客,看到了一个介绍文字动画的库,进入 pub 一看,立马就爱上这个动画库了,几乎你能想到的文字动画效果它都有!现在正式给大家安利一下这个库:animated_text_kit.本篇我们介绍几个酷炫的效果,其他的效果大家可以自行查看官网文档使用. 波浪涌动效果 波浪涌动 上面的动画效果只需要下面几行代码,其中loadUntil用于控制波浪最终停留的高度,取值是0-1.0,如

  • Android控件系列之相册Gallery&Adapter适配器入门&控件缩放动画入门

    学习目的: 1.掌握在Android中如何建立Gallery 2.初步理解Android适配器的原理 3.实现简单的控件缩放动画 简介: 1.Gallery是Android内置的一个控件,它可以继承若干图片甚至是其他控件 2.Gallery自带了滚动播放图片功能,此功能您可以通过模拟器拖曳鼠标或者在手机上拖拽验证 3.Gallery需要适配器来传输数据,如果您不熟悉"适配器设计模式",可以将适配器理解为某厂商的电脑适配器,只要这个厂商的所有型号的电脑都能使用该适配器,也就是说,设计新型

  • Android补间动画基本使用(位移、缩放、旋转、透明)

    本文讲述了Android补间动画基本使用(位移.缩放.旋转.透明).分享给大家供大家参考,具体如下: 补间动画 原形态变成新形态时为了过渡变形过程,生成的动画就叫补间动画 位移.旋转.缩放.透明 位移: 参数10指的是X的起点坐标,但不是指屏幕x坐标为10的位置,而是imageview的 真实X + 10 参数150指的是X的终点坐标,它的值是imageview的 真实X + 150 //创建为位移动画对象,设置动画的初始位置和结束位置 TranslateAnimation ta = new T

  • Android列表实现单选点击缩放动画效果

    recycleView单选的时候,一般的处理就是选中的item做个stroke或者字体颜色改变,但要提升用户体验就得加点动画了.也就是点击选中的元素放大,同时之前选中的item缩小,不便截gif图,只能放一张静态图,大家脑补脑补~ 图中的CheckBox,代码实现其实是imageview,它的选中.取消也是有动画的,不是控制visible,而是通过改变图片透明度来实现选中取消的. 具体看代码: import android.animation.ObjectAnimator; import and

  • Android 三种动画详解及简单实例

    Android 三种动画详解 帧动画 一张张图片不断的切换,形成动画效果 在drawable目录下定义xml文件,子节点为animation-list,在这里定义要显示的图片和每张图片的显示时长 <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="

  • 浅谈Android中视图动画的属性与使用

    简介 Android动画主要包括视图动画和属性动画,视图动画包括Tween动画和Frame动画,Tween动画又包括渐变动画.平移动画.缩放动画.旋转动画. Tween动画的基本属性 目标 View: 时常 duration; 开始状态 fromXXX; 结束动画 toXXX; 开始时间 startOffset; 重复次数 repeatCount; 时间轴 interpolator(插值器). 代码示例 xml实现 <?xml version="1.0" encoding=&qu

  • Android实现旋转动画的两种方式案例详解

    目录 练习案例 效果展示 前期准备 自定义 View java代码编写 方法一 方法二 易错点总结: 练习案例 视差动画 - 雅虎新闻摘要加载 效果展示 前期准备 第一步:准备好颜色数组 res => values => colors.xml <color name="orange">#FF9600</color> <color name="aqua">#02D1AC</color> <color n

  • Android编程实现动画自动播放功能

    本文实例讲述了Android编程实现动画自动播放功能.分享给大家供大家参考,具体如下: private ImageView image; private AnimationDrawable animDrawable = new AnimationDrawable(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.la

  • iOS UITableView展开缩放动画实例代码

    Swift - UITableView展开缩放动画 效果 源码:https://github.com/YouXianMing/Swift-Animations // // HeaderViewTapAnimationController.swift // Swift-Animations // // Created by YouXianMing on 16/8/9. // Copyright © 2016年 YouXianMing. All rights reserved. // import

  • Android 图片缩放实例详解

    本文实现Android中的图片的缩放效果 首先设计布局: <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_par

随机推荐