演示
1、activity_main.xml当中设置布局
源代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:src="@drawable/amin_pgbar" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="96dp"
android:layout_marginLeft="96dp"
android:layout_marginTop="244dp"
android:text="Java"
android:textSize="25sp"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="88dp"
android:layout_marginLeft="88dp"
android:layout_marginTop="244dp"
android:text="Python"
android:textSize="25sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2、引入图片
3、创建amin_pgbar.xml
源代码
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item
android:drawable="@drawable/m1"
android:duration="200"
/>
<item
android:drawable="@drawable/m2"
android:duration="200"
/>
<item
android:drawable="@drawable/m3"
android:duration="200"
/>
<item
android:drawable="@drawable/m4"
android:duration="200"
/>
<item
android:drawable="@drawable/m5"
android:duration="200"
/>
<item
android:drawable="@drawable/m6"
android:duration="200"
/>
<item
android:drawable="@drawable/m7"
android:duration="200"
/>
<item
android:drawable="@drawable/m8"
android:duration="200"
/>
<item
android:drawable="@drawable/m9"
android:duration="200"
/>
<item
android:drawable="@drawable/m10"
android:duration="200"
/>
<item
android:drawable="@drawable/m11"
android:duration="200"
/>
<item
android:drawable="@drawable/m12"
android:duration="200"
/>
<item
android:drawable="@drawable/m13"
android:duration="200"
/>
<item
android:drawable="@drawable/m14"
android:duration="200"
/>
<item
android:drawable="@drawable/m15"
android:duration="200"
/>
<item
android:drawable="@drawable/m16"
android:duration="200"
/>
<item
android:drawable="@drawable/m17"
android:duration="200"
/>
<item
android:drawable="@drawable/m18"
android:duration="200"
/>
<item
android:drawable="@drawable/m19"
android:duration="200"
/>
<item
android:drawable="@drawable/m20"
android:duration="200"
/>
<item
android:drawable="@drawable/m21"
android:duration="200"
/>
<item
android:drawable="@drawable/m22"
android:duration="200"
/>
</animation-list>
4、MainActivity当中
package com.example.application01;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img_pgbar = (ImageView) findViewById(R.id.imageView);
AnimationDrawable ad = (AnimationDrawable) img_pgbar.getDrawable();
ad.start();
}
}