需求说明
一个箭头图片在某个区域做上升的动画,到区域顶部时,消失的部分又从底部出现,如此循环往复。
先看实现的效果图,如下。
缺点:实际上并不是连贯的循环重复,而是第二次上升的箭头消失并没有再从底部出来,这是用简单动画实现的劣势。
优点:简单,便于理解和实现。
原理说明
用两张图片箭头一起做向上平移的动画,就可实现该功能。
一张图片放在运动区域下方,另一张图片放在运动区域的两部位置下方,然后一起使用同一个平移相同距离的动画,使之实现上述效果。
代码实现
首先布局设定好ImageView的具体宽高,然后用marginBottom负数的方式使之置于运动区域下方,第二个箭头margin的距离是运动区域的高度+自身的高度。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".animation.ArrowMoveActivity"
tools:showIn="@layout/activity_arrow_move">
<FrameLayout
android:id="@+id/fl_move_area"
android:background="@color/color_list_divider"