Android之登录页的等待页面,AVI(手把手教你写)
话不多说,先看效果(不会上传视频,就看照片吧)
1,添加依赖
implementation 'com.wang.avi:library:2.1.3'
2,布局文件要这样写,一共就2个LinearLayout,第一个是登录用的,第二个是等待用的
<?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=".StarActivity">
<!--page1-->
<LinearLayout
android:id="@+id/page1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/bt_s_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击登录"/>
</LinearLayout>
<!--page2-->
<LinearLayout
android:gravity="center"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:id="@+id/page2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.wang.avi.AVLoadingIndicatorView
android:id="@+id/avi"
style="@style/AVLoadingIndicatorView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/page1"
android:visibility="visible"
app:indicatorColor="#0f0"
app:indicatorName="BallPulseIndicator" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
3,使用,在MainActivity中(我这里用的是StarActivity,你们自己换成自己的Activity)
解释都在代码中(结构如下)
public class StarActivity extends AppCompatActivity {
private LinearLayout mPage1;
private LinearLayout mPage2;
//hander机制
Handler mHandler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
switch (msg.what){
//登录成功刷新界面
case 1:
//登录界面变回原样
mPage1.setAlpha((float) 1);
//登录等待界面不可见
mPage2.setVisibility(View.INVISIBLE);
Toast.makeText(StarActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_star);
init();
}
//
private void init() {
//登录界面
mPage1 = findViewById(R.id.page1);
//登录等待界面
mPage2 = findViewById(R.id.page2);
//设置mPage2不可见
mPage2.setVisibility(View.GONE);
//点击登录
findViewById(R.id.bt_s_1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(StarActivity.this, "登录中...", Toast.LENGTH_SHORT).show();
//设置登录界面变灰
mPage1.setAlpha((float) 0.3);
//设置登录等待界面尅可见
mPage2.setVisibility(View.VISIBLE);
//进行你的操作
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//模拟登录成功
Message message = new Message();
message.what = 1;
message.obj = 1;
mHandler.sendMessage(message);
}
}).start();
}
});
}
}
4,运行试一下吧