一,布局为
<?xml version="1.0" encoding="utf-8"?>
<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_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.jiangtunjf.jgapplication.Main19Activity">
<ImageView
android:id="@+id/imageview"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:src="@drawable/test_shape">
</ImageView>
</LinearLayout>
二,
test_shape 对应shape为
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="1080.0"> <shape android:innerRadiusRatio="4" android:shape="ring" android:thicknessRatio="12" android:useLevel="false"> <gradient android:endColor="@color/colorAccent" android:startColor="#FFFFFF" android:type="sweep"/> </shape> </rotate>
其中,对应属性
首先,shape根元素有些属性只适用于ring类型,先过目下这些属性吧: android:innerRadius 内环的半径 android:innerRadiusRatio 浮点型,以环的宽度比率来表示内环的半径,默认为3,表示内环半径为环的宽度除3 该值会被android:innerRadius覆盖 android:thickness 环的厚度 android:thicknessRatio 浮点型,以环的宽度比率来表示环的厚度,默认为9,表示环的厚度为环的宽度除以9,该值会被android:thickness覆盖 android:useLevel 一般为false,否则可能环形无法显示,只有作为LevelListDrawable使用时才设为true
三,对应,Activity为
public class Main19Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main19); ImageView imageView = findViewById(R.id.imageview); RotateDrawable animationDrawable = (RotateDrawable) imageView.getDrawable(); ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 10000); valueAnimator.setDuration(4000); valueAnimator.setInterpolator(new LinearInterpolator()); valueAnimator.setRepeatCount(ValueAnimator.INFINITE); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { Integer animatedValue = (Integer) animation.getAnimatedValue(); animationDrawable.setLevel(animatedValue); } }); valueAnimator.start(); } }
四,效果图