前言
紧接着上篇博客,我们一起来实现一下引导页功能这个常用于向第一次安装这个软件的用户介绍软件的特性和功能,分页轮播。
绘制布局
线性布局:ViewPage(滑动视图)+Button(按钮)+LinearLayout(圆点)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
>
<androidx.viewpager.widget.ViewPager
android:id="@+id/new_pg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/new_bg3" />
<Button
android:id="@+id/sat_new"
android:layout_width="match_parent"
android:background="#00000000"
android:layout_height="50dp"
android:textColor="#FF9D00"
android:textSize="30dp"
android:text="立即体验"
android:layout_marginTop="600dp"/>
<LinearLayout
android:id="@+id/pointer"
android:layout_below="@+id/sat_new"
android:layout_width="match_parent"
android:layout_height="20dp"
android:gravity="center"
android:orientation="horizontal">
</LinearLayout>
</RelativeLayout>
实现功能
首先定义一个两张以上图片的数组(int[] imgs = {};),然后编写viewpage适配器(格式固定写法,套用即可),getCount() 方法里可以指定显示可滑动图片的张数,最后在guidePoint.setBackgroundResource(R.drawable.yuandian) 里设置圆点样式。
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import java.util.ArrayList;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
public class StarActivity extends AppCompatActivity {
private ViewPager guideVp;
//定义一个圆点容器
private LinearLayout guideLl;
//定义一个在最后一页显示的按钮
private Button guideBtn;
//定义一个图片数组
public int[] imgs = {R.drawable.new_bg1,R.drawable.new_bg2,R.drawable.new_bg3};
public ArrayList<ImageView> imageViewArrayList;
private int guidePointPsition = 0; //上次显示的位置
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//全屏展示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_new);
//控件初始化
initView();
//小圆点
initData();
//获取适配器对象
GuideAdapter guideAdapter = new GuideAdapter(imageViewArrayList);
//应用适配器
guideVp.setAdapter(guideAdapter);
guideVp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
guideLl.getChildAt(position).setEnabled(true);
guideLl.getChildAt(guidePointPsition).setEnabled(false);
guidePointPsition = position;
if (imageViewArrayList.size()-1 == position){
//如果到了最后一页,就显示按钮
guideBtn.setVisibility(View.VISIBLE);
}else{
guideBtn.setVisibility(View.GONE);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
guideBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(StarActivity.this, LoginActivity.class));
}
});
}
private void initData() {
imageViewArrayList = new ArrayList<>();
for (int i = 0; i < imgs.length; i++) {
ImageView imageView = new ImageView(this);
imageView.setBackgroundResource(imgs[i]);
imageViewArrayList.add(imageView);
ImageView guidePoint = new ImageView(this);
//小圆点样式设置
guidePoint.setBackgroundResource(R.drawable.yuandian);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
if (i == 0)
guidePoint.setEnabled(true);
else {
guidePoint.setEnabled(false);
params.setMarginStart(15);
}
guidePoint.setLayoutParams(params);
guideLl.addView(guidePoint);
}
}
private void initView() {
guideVp = findViewById(R.id.new_pg);
guideBtn = findViewById(R.id.sat_new);
guideLl = findViewById(R.id.pointer);
guideBtn.setVisibility(View.GONE);
}
//ViewPager适配器
class GuideAdapter extends PagerAdapter {
private ArrayList<ImageView> imageViews;
public GuideAdapter(ArrayList<ImageView> imageViews) {
this.imageViews = imageViews;
}
@Override
public int getCount() {
return imageViewArrayList.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView view = imageViewArrayList.get(position);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}
效果展示
按钮没有固定对象,所以效果展示上有点问题,解决方法如下:
可见占位:
android:visibility=“visible”;
Java代码:view.setVisibility(View.VISIBLE);
不可见占位:
android:visibility=“invisible”;
Java代码:view.setVisibility(View.INVISIBLE);
隐藏不占位:
android:visibility=“gone”;
Java代码:view.setVisibility(View.GONE);
相关文章: Android开发-欢迎引导模块(欢迎)