原理:
1.把方框的位置在xml中布置好
2.把扫描线的图片也布置好
3.用平移动画,移动y轴,循环的次数反复。
不过我网上搜了搜很多方法都是实时绘制出来的,计算点的位置然后重绘出来。我的第一感觉是完全没必要,其实这个东西本质上就是一张贴图加一个平移动画效果,所以就自己做了一个小小例子。
扫描的动画效果实则就是平移动画
<RelativeLayout
android:id="@+id/box_layout"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:background="@drawable/box" >
<ImageView
android:id="@+id/scan_line"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:background="@drawable/line"/>
</RelativeLayout>
代码实现:
可以设置来回移动,移动多个参数(重要)
ImageView mQrLineView = (ImageView) findViewById(R.id.scan_line);
TranslateAnimation mAnimation = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0f, TranslateAnimation.ABSOLUTE,0f,TranslateAnimation.RELATIVE_TO_PARENT, 0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.9f);
mAnimation.setDuration(1500);
mAnimation.setRepeatCount(-1);
mAnimation.setRepeatMode(Animation.RESTART);
mAnimation.setInterpolator(new LinearInterpolator());
mQrLineView.setAnimation(mAnimation);
下载地址: