先看效果展示
一、添加一个加载控件
我们这个加载页面最好选择Framelayout作为载体因为我们要把加载控件放到显示内容的上方
该控件布局代码如下
<ProgressBar
android:id="@+id/loading_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
二、实现方法
我们要在该页面对应的java文件里去具体的方法
这个内容加载的本质是一个内容淡入淡出的过程
具体步骤如下:
首先在我们刚点击进去的一瞬间是没有内容的,只能出现一个加载画面所以我们要先消除我们的内容
view.setVisibility(View.GONE);
我们可以设置内容加载的时间
private int shortAnimationDuration;
shortAnimationDuration = getResources().getInteger(
android.R.integer.config_longAnimTime);
这里面的config_longAnimTime
这个值是config里面自带的一个时间参数,我们可以通过Ctrl+鼠标左键进去修改它的值,初始值是500,我改成1500了
<!-- The duration (in milliseconds) of a short animation. -->
<integer name="config_shortAnimTime">200</integer>
<!-- The duration (in milliseconds) of a medium-length animation. -->
<integer name="config_mediumAnimTime">400</integer>
<!-- The duration (in milliseconds) of a long animation. -->
<integer name="config_longAnimTime">1500</integer>
然后我们需要写两个方法函数,一个用于淡入过程,一个用于淡出过程
淡入过程
private void crossappear() {
view.setAlpha(0f);
view.setVisibility(View.VISIBLE);
// Set the content view to 0% opacity but visible, so that it is visible
// (but fully transparent) during the animation.
loadingView.setAlpha(1f);
loadingView.animate()
.alpha(0f)
.setDuration(shortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
loadingView.setVisibility(View.GONE);
}
});
// Animate the content view to 100% opacity, and clear any animation
// listener set on the view.
view.animate()
.alpha(1f)
.setDuration(shortAnimationDuration)
.setListener(null);
// Animate the loading view to 0% opacity. After the animation ends,
// set its visibility to GONE as an optimization step (it won't
// participate in layout passes, etc.)
}
淡出过程
private void crossfade()
{
// Set the content view to 0% opacity but visible, so that it is visible
// (but fully transparent) during the animation.
view.setAlpha(1f);
view.setVisibility(View.VISIBLE);
// Animate the content view to 100% opacity, and clear any animation
// listener set on the view.
view.animate()
.alpha(0f)
.setDuration(shortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
view.setVisibility(View.GONE);
}
});
// Animate the loading view to 0% opacity. After the animation ends,
// set its visibility to GONE as an optimization step (it won't
// participate in layout passes, etc.)
}
setVisibility()
的功能是设置视图是否可见
setAlpha()
的功能是设置视图的透明度大小
该页面所有代码如下:
package com.example.myapplication;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB_MR1)
public class MainActivity2 extends AppCompatActivity{
private ImageView imageView;
private TextView textView1,textView2;
private Button button;
private View view;
private View loadingView;
private int shortAnimationDuration;
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB_MR1)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
initDetail();
// loadingView.setVisibility(View.INVISIBLE);
view.setVisibility(View.GONE);
shortAnimationDuration = getResources().getInteger(
android.R.integer.config_longAnimTime);
crossappear();
}
private void crossfade()
{
// Set the content view to 0% opacity but visible, so that it is visible
// (but fully transparent) during the animation.
view.setAlpha(1f);
view.setVisibility(View.VISIBLE);
// Animate the content view to 100% opacity, and clear any animation
// listener set on the view.
view.animate()
.alpha(0f)
.setDuration(shortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
view.setVisibility(View.GONE);
}
});
// Animate the loading view to 0% opacity. After the animation ends,
// set its visibility to GONE as an optimization step (it won't
// participate in layout passes, etc.)
}
private void crossappear() {
view.setAlpha(0f);
view.setVisibility(View.VISIBLE);
// Set the content view to 0% opacity but visible, so that it is visible
// (but fully transparent) during the animation.
loadingView.setAlpha(1f);
loadingView.animate()
.alpha(0f)
.setDuration(shortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
loadingView.setVisibility(View.GONE);
}
});
// Animate the content view to 100% opacity, and clear any animation
// listener set on the view.
view.animate()
.alpha(1f)
.setDuration(shortAnimationDuration)
.setListener(null);
// Animate the loading view to 0% opacity. After the animation ends,
// set its visibility to GONE as an optimization step (it won't
// participate in layout passes, etc.)
}
private void initDetail(){
Intent intent = getIntent();
String first = intent.getStringExtra("名字");
String second = intent.getStringExtra("介绍");
textView1.setText(first);
textView2.setText(second);
imageView.setImageResource(intent.getIntExtra("tx",1));
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity2.this,first,Toast.LENGTH_SHORT).show();
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crossfade();
finish();
}});
}
private void initView()
{
view = findViewById(R.id.D_item);
loadingView = findViewById(R.id.loading_spinner);
imageView =findViewById(R.id.V_Animal);
textView1 = findViewById(R.id.T_name);
textView2 = findViewById(R.id.T_introduce);
button = findViewById(R.id.B_fanhui);
}
}
最后要注意的是,当我们点击按钮退出加载页面之后要在finish()
结束此activity之前要将之前展示的内容初始化,也就是将它隐藏,方便下次点进去时重新加载。