sweepViewSecond = findViewById(R.id.sweepViewSecond);
实现移动方法,setInterpolator实现加速或者变慢效果:
private void frameAnimation(){
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
int screen_x = metric.widthPixels; // 屏幕宽度(像素)
Animation translateAnimationSecond = new TranslateAnimation(- sweepViewSecond.getWidth(),screen_x+sweepViewSecond.getWidth(),0,0);//平移动画 从0,0,平移到100,100
translateAnimationSecond.setDuration(1200);//动画持续的时间为1.5s
translateAnimationSecond.setRepeatMode(Animation.RESTART);
translateAnimationSecond.setRepeatCount(Integer.MAX_VALUE);
translateAnimationSecond.setInterpolator(new AccelerateDecelerateInterpolator());
sweepViewSecond.startAnimation(translateAnimationSecond);
}
xml布局:
<RelativeLayout
android:layout_width="match_parent"
android:background="#e6e6e6"
android:layout_height="match_parent">
<View
android:id="@+id/sweepViewSecond"
android:layout_width="120dp"
android:padding="30dp"
android:layout_marginLeft="-120dp"
android:layout_height="match_parent"
android:background="@drawable/shape_gradient"
/>
</RelativeLayout>
shape.xml :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="0"
android:endColor="#30e6e6e6"
android:centerColor="#30ffffff"
android:startColor="#30e6e6e6"
android:type="linear"
/>
</shape>