Android 基础动画之 scale 渐变缩放

简介:       小菜最近在学习 ViewPager 的小动画,说来惭愧,工作这么久了一直没有认真了解过动画这部分,今天特意学习一下 Android 的基本动画。      Android 的基本的动画包括 alpha(透明度)/ scale(缩放)/ translate(位移) / rotate(旋转)四种,小菜今天学习一下 scale 渐变缩放动画效果。

      小菜最近在学习 ViewPager 的小动画,说来惭愧,工作这么久了一直没有认真了解过动画这部分,今天特意学习一下 Android 的基本动画。

      Android 的基本的动画包括 alpha(透明度)/ scale(缩放)/ translate(位移) / rotate(旋转)四种,小菜今天学习一下 scale 渐变缩放动画效果。

Activity 绑定动画事件:

mBtn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mV1.startAnimation(AnimationUtils.loadAnimation(AnimActivity.this, R.anim.anim_scale));
    }
});

layout.xml 显示动画效果

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
  <Button
      android:id="@+id/anim_btn1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginBottom="40dp"
      android:text="开始动画" />

  <View
      android:id="@+id/anim_v1"
      android:layout_width="300dp"
      android:layout_height="150dp"
      android:layout_gravity="center"
      android:background="@color/colorAccent" />
</LinearLayout>

anim.xml 设置动画属性

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3500"
    android:fromXScale="0.0"
    android:fromYScale="0.0"
    android:pivotX="100%p"
    android:pivotY="100%p"
    android:toXScale="1.0"
    android:toYScale="1.0" />

代码很简单,小菜接下来逐条学习一下 anim_scale 中各条属性:

1. android:duration="3500"

   duration 代表动画过程中持续时常;

2. android:fromXScale="0.0"

   fromXScale 代表初始时横向 View 比例,0.0为从没有开始动画,1.0即 View 原尺寸,2.0即 View 原尺寸两倍;建议与 toXScale 共同使用;

3. android:fromYScale="0.0"

   fromYScale 为初始时纵向 View 比例,与 fromXScale 使用相同;

4. android:toXScale="1.0"

   toXScale 代表动画过程中横向变化尺寸比例,一般与 fromXScale 共同使用;

5. android:toYScale="1.0"

   toYScale 代表动画过程中纵向变化尺寸比例,一般与 fromYScale 共同使用;

6. android:pivotX="100%p" android:pivotY="100%p"

   pivotXpivotY 是小菜重点学习的地方,小菜理解为动画起点坐标,可以为整数值、百分数(或者小数)、百分数p 三种样式。

  1. 整数值:android:pivotX="100"

   整数值类型是相对于自身 View 来定义,以自身 View 左上角的点为原点,水平向右为正,竖直向下为正的坐标系中计算,设置的整数值为 px,为固定值。

  1. 百分数/小数:android:pivotX="100%"

   百分数/小数类型是相对于自身 View 来定义,与整数值类似,只是坐标点是以自身 View 尺寸比例来计算而非固定值。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
  <Button
      android:id="@+id/anim_btn1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginBottom="40dp"
      android:text="开始动画" />
  <FrameLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
      <View
          android:id="@+id/anim_v1"
          android:layout_width="300dp"
          android:layout_height="150dp"
          android:layout_gravity="center"
          android:background="@color/colorAccent" />
    </FrameLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/background_dark" />
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_marginRight="150dp"
        android:background="@android:color/background_dark" />
  </FrameLayout>
</LinearLayout>
  1. 百分数 + p

   这种方式是最特殊的,小菜理解为自身 View 与相对于某个父容器的大小,并非单纯的根据父容器大小尺寸位置。小菜为了测试方便,设置了一个固定的 400dp*400dp 的 LinearLayout,测试百分数 + p 的方式都正常,但是如果设置 View 居中或其他情况时跟小菜想的很有差距,小菜测试了很久,终于有一些了解。

   百分比 + p 这种方式是相对的,既与父容器相关也与自身 View 相关,当设置 View 位置为居中或其他位置时,整个移动的坐标系也会变化,原点并非直接父容器左上角而是自身 View 左上角,整个移动布局根据 View 平移;而父容器是一个框架,动画的范围大小为父容器大小且只在父容器中进行展示。如图:

   小菜这才了解到刚开始测试时并未设置 LinearLayoutgravity 或自身 Viewlayout_gravity 属性,默认是居左上角,此时与父容器左上角重合。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
  <Button
      android:id="@+id/anim_btn1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginBottom="40dp"
      android:text="开始动画" />
  <FrameLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" >
    <LinearLayout
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:background="#8099cc00"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="125dp"
        android:gravity="center"
        android:orientation="horizontal" />
    <LinearLayout
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:background="#80008577"
        android:gravity="center"
        android:orientation="horizontal" >
      <View
          android:id="@+id/anim_v1"
          android:layout_width="300dp"
          android:layout_height="150dp"
          android:background="@color/colorAccent"
          android:gravity="center"
          android:text="Hello World!" />
    </LinearLayout>
    <View
        android:layout_width="4dp"
        android:layout_height="4dp"
        android:layout_marginLeft="248dp"
        android:layout_marginTop="123dp"
        android:background="@android:color/background_dark" />
    <View
        android:layout_width="4dp"
        android:layout_height="4dp"
        android:layout_marginLeft="48dp"
        android:layout_marginTop="323dp"
        android:background="@android:color/background_dark" />
    <View
        android:layout_width="4dp"
        android:layout_height="4dp"
        android:layout_marginLeft="248dp"
        android:layout_marginTop="323dp"
        android:background="@android:color/background_dark" />
    <View
        android:layout_width="4dp"
        android:layout_height="4dp"
        android:layout_marginLeft="448dp"
        android:layout_marginTop="523dp"
        android:background="@android:color/background_dark" />
  </FrameLayout>
</LinearLayout>

7. android:interpolator="@android:anim/accelerate_decelerate_interpolator"

   interpolator 代表缩放动画曲线,即动画由大变小,变换速率等,小菜目前还未学习到,后期补充。


      小菜的动画部分是短板,正在从零学习,不对的请多多指正!

目录
相关文章
|
Android开发 开发者
Android利用SVG实现动画效果
本文介绍了如何在Android中利用SVG实现动画效果。首先通过定义`pathData`参数(如M、L、Z等)绘制一个简单的三角形SVG图形,然后借助`objectAnimator`实现动态的线条绘制动画。文章详细讲解了从配置`build.gradle`支持VectorDrawable,到创建动画文件、关联SVG与动画,最后在Activity中启动动画的完整流程。此外,还提供了SVG绘制原理及工具推荐,帮助开发者更好地理解和应用SVG动画技术。
600 30
|
Android开发 UED 计算机视觉
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
本文介绍了一款受游戏“金铲铲之战”启发的Android自定义View——线条等待动画的实现过程。通过将布局分为10份,利用`onSizeChanged`测量最小长度,并借助画笔绘制动态线条,实现渐变伸缩效果。动画逻辑通过四个变量控制线条的增长与回退,最终形成流畅的等待动画。代码中详细展示了画笔初始化、线条绘制及动画更新的核心步骤,并提供完整源码供参考。此动画适用于加载场景,提升用户体验。
703 5
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
|
API Android开发 开发者
Android颜色渐变动画效果的实现
本文介绍了在Android中实现颜色渐变动画效果的方法,重点讲解了插值器(TypeEvaluator)的使用与自定义。通过Android自带的颜色插值器ArgbEvaluator,可以轻松实现背景色的渐变动画。文章详细分析了ArgbEvaluator的核心代码,并演示了如何利用Color.colorToHSV和Color.HSVToColor方法自定义颜色插值器MyColorEvaluator。最后提供了完整的源码示例,包括ColorGradient视图类和MyColorEvaluator类,帮助开发者更好地理解和应用颜色渐变动画技术。
385 3
|
Android开发 开发者
Android SVG动画详细例子
本文详细讲解了在Android中利用SVG实现动画效果的方法,通过具体例子帮助开发者更好地理解和应用SVG动画。文章首先展示了动画的实现效果,接着回顾了之前的文章链接及常见问题(如属性名大小写错误)。核心内容包括:1) 使用阿里图库获取SVG图形;2) 借助工具将SVG转换为VectorDrawable;3) 为每个路径添加动画绑定属性;4) 创建动画文件并关联SVG;5) 在ImageView中引用动画文件;6) 在Activity中启动动画。文末还提供了完整的代码示例和源码下载链接,方便读者实践操作。
568 65
|
XML Java Maven
Android线条等待动画JMWorkProgress(可添加依赖直接使用)
这是一篇关于Android线条等待动画JMWorkProgress的教程文章,作者计蒙将其代码开源至GitHub,提升可读性。文章介绍了如何通过添加依赖库使用该动画,并详细讲解了XML与Java中的配置方法,包括改变线条颜色、宽度、添加文字等自定义属性。项目已支持直接依赖集成(`implementation &#39;com.github.Yufseven:JMWorkProgress:v1.0&#39;`),开发者可以快速上手实现炫酷的等待动画效果。文末附有GitHub项目地址,欢迎访问并点赞支持!
379 26
|
XML Android开发 数据格式
Android中SlidingDrawer利用透明动画提示效果
本文介绍了在Android中使用`SlidingDrawer`实现带有透明动画提示效果的方法。通过XML布局配置`SlidingDrawer`的把手(handle)和内容(content),结合Activity中的代码实现动态动画效果。最终实现了交互性强、视觉效果良好的滑动抽屉功能。
171 1
Android中SlidingDrawer利用透明动画提示效果
|
XML Java Android开发
Android 动画之帧动画 + 补间动画 + 属性动画
本文介绍了Android开发中的三种动画类型:帧动画、补间动画和属性动画。帧动画通过依次播放一系列静态图片实现动态效果,支持Java代码与XML两种实现方式。补间动画基于起始和结束位置自动生成过渡效果,涵盖透明度、位移、旋转、缩放及组合动画等多种形式,并可搭配插值器优化动画过程。属性动画则通过改变对象属性实现动画,支持透明度、位移、旋转、缩放及组合动画,灵活性更高且适用于更复杂的场景。文中提供了详细的代码示例,帮助开发者快速上手。
582 15
|
Android开发 开发者
Android自定义view之围棋动画(化繁为简)
本文介绍了Android自定义View的动画实现,通过两个案例拓展动态效果。第一个案例基于`drawArc`方法实现单次动画,借助布尔值控制动画流程。第二个案例以围棋动画为例,从简单的小球直线运动到双向变速运动,最终实现循环动画效果。代码结构清晰,逻辑简明,展示了如何化繁为简实现复杂动画,帮助读者拓展动态效果设计思路。文末提供完整源码,适合初学者和进阶开发者学习参考。
228 0
Android自定义view之围棋动画(化繁为简)
|
Java Android开发 开发者
Android自定义view之围棋动画
本文详细介绍了在Android中自定义View实现围棋动画的过程。从测量宽高、绘制棋盘背景,到创建固定棋子及动态棋子,最后通过属性动画实现棋子的移动效果。文章还讲解了如何通过自定义属性调整棋子和棋盘的颜色及动画时长,并优化视觉效果,如添加渐变色让白子更明显。最终效果既可作为围棋动画展示,也可用作加载等待动画。代码完整,适合进阶开发者学习参考。
276 0
|
XML Java API
Android翻转动画(卡片翻转效果)
本文介绍了如何实现卡片翻转动画效果,通过Android中的ObjectAnimator结合不同插值器(LinearInterpolator、AccelerateInterpolator、DecelerateInterpolator)完成平滑过渡。示例中以按钮点击触发动画,核心逻辑包括判断视图可见性、设置旋转角度及处理初始Bug(如第一次点击异常)。最终提供完整代码(Java与XML布局),并指出将按钮事件替换为屏幕监听即可满足右滑触发需求。适合初学者学习动画实现原理。
465 0

热门文章

最新文章