分享歌曲
github:[https://github.com/apl-devs/AppIntro](https://github.com/apl-devs/AppIntro) 官方介绍 > AppIntro is an Android Library that helps you make a cool intro for your app, like the ones in Google apps.使用方法
1.添加到 build.gradle
{% codeblock %}
repositories {
mavenCentral()
}
dependencies {
compile 'com.github.paolorotolo:appintro:4.1.0'
}
{% endcodeblock %}
2.添加一个继承自 AppIntro 或者 AppIntro2 的 Activity
{% codeblock lang:Java %}
public class IntroActivity extends AppIntro {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Note here that we DO NOT use setContentView();
// Add your slide fragments here.
// AppIntro will automatically generate the dots indicator and buttons.
//两种方式添加引导也,一个自定义的fragment,二是通过AppIntroFragment来自动图片标题的引导页
addSlide(firstFragment);
addSlide(secondFragment);
addSlide(thirdFragment);
addSlide(fourthFragment);
// Instead of fragments, you can also use our default slide
// Just set a title, description, background and image. AppIntro will do the rest.
addSlide(AppIntroFragment.newInstance(title, description, image, backgroundColor));
// OPTIONAL METHODS
// Override bar/separator color.
//颜色选择
setBarColor(Color.parseColor("#3F51B5"));
setSeparatorColor(Color.parseColor("#2196F3"));
// Hide Skip/Done button.
//跳过按钮
showSkipButton(false);
setProgressButtonEnabled(false);
// Turn vibration on and set intensity.
// NOTE: you will probably need to ask VIBRATE permission in Manifest.
//震动设置
setVibrate(true);
setVibrateIntensity(30);
}
//事件
@Override
public void onSkipPressed(Fragment currentFragment) {
super.onSkipPressed(currentFragment);
// Do something when users tap on Skip button.
}
@Override
public void onDonePressed(Fragment currentFragment) {
super.onDonePressed(currentFragment);
// Do something when users tap on Done button.
}
@Override
public void onSlideChanged(@Nullable Fragment oldFragment, @Nullable Fragment newFragment) {
super.onSlideChanged(oldFragment, newFragment);
// Do something when the slide changes.
}
}
{% endcodeblock %}
几大重点
- 继承自 AppIntro 的 Activity 不需要 setContentView();(这坑我采了)
- 通过 addSlide()来一个个添加引导页
- 可自重写跳过、完成、滑动事件
- 使用布局来直接添加引导页使用 AppIntro2
3.在 Manifest 中定义 Activity(如果是自己新建的是 class 而不是 Activity)
{% codeblock %}
{% endcodeblock %}
动画效果
{% codeblock lang:Java %}
setFadeAnimation()
setZoomAnimation()
setFlowAnimation()
setSlideOverAnimation()
setDepthAnimation()
{% endcodeblock %}
实践
定义 MaterialGuideActivity 继承 AppIntro2,实现图片引导页,添加动画,重写完成和跳过事件来记录是否为第一次引导。
{% codeblock lang:Java %}
//省略 package 和 import
public class MaterialGuideActivity extends AppIntro2 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// AppIntro do not need setContentView
// setContentView(R.layout.material_guide1);
addSlide(IntroSlideUtil.newInstance(R.drawable.guide1));
addSlide(IntroSlideUtil.newInstance(R.drawable.guide2));
addSlide(IntroSlideUtil.newInstance(R.drawable.guide3));
addSlide(IntroSlideUtil.newInstance(R.drawable.guide4));
showSkipButton(true);
setVibrate(true);
setVibrateIntensity(30);
// setFadeAnimation();
// setFlowAnimation();
// setZoomAnimation();
setDepthAnimation();
}
//记录是否已经看过引导页
@Override
public void onSkipPressed(Fragment currentFragment) {
startActivity(new Intent(MaterialGuideActivity.this, MainActivity.class));
SharePrefereceTool.setPrefBoolean(MaterialGuideActivity.this, "guide_showed", true);
finish();
}
@Override
public void onDonePressed(Fragment currentFragment) {
startActivity(new Intent(MaterialGuideActivity.this, MainActivity.class));
SharePrefereceTool.setPrefBoolean(MaterialGuideActivity.this, "guide_showed", true);
finish();
}
}
{% endcodeblock %}
IntroSlideUtil.java
{% codeblock lang:Java %}
//省略 package 和 import
/*
*AppIntro 中布局使用 fragment,
-
如果是图片直接返回全屏图片
-
如果是布局则 inflate 返回
*/
public class IntroSlideUtil extends Fragment {private static final String IMAGE_RES_ID = "imageResId";
private int imageId;
private static final String LAYOUT_RES_ID = "layoutResId";
private int layoutId;
private static final String FLAG_LAYOUT = "flag";
private boolean flag = false;public static IntroSlideUtil newInstance(int imageResId) {
IntroSlideUtil sample = new IntroSlideUtil();Bundle args = new Bundle(); args.putInt(IMAGE_RES_ID, imageResId); sample.setArguments(args); return sample;
}
public static IntroSlideUtil newInstance(int layoutResId, boolean isLayout) {
IntroSlideUtil sample = new IntroSlideUtil();
Bundle args = new Bundle();
args.putInt(LAYOUT_RES_ID, layoutResId);
args.putBoolean(FLAG_LAYOUT, isLayout);
sample.setArguments(args);
return sample;
}@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null && getArguments().containsKey(IMAGE_RES_ID)) {
imageId = getArguments().getInt(IMAGE_RES_ID);
} else if (getArguments() != null && getArguments().containsKey(LAYOUT_RES_ID)) {
layoutId = getArguments().getInt(LAYOUT_RES_ID);
flag = getArguments().getBoolean(FLAG_LAYOUT);
}
}@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
if (flag) {
return inflater.inflate(layoutId, container, false);
} else {
ImageView imageView = new ImageView(getContext());
Glide.with(getContext()).load(imageId).into(imageView);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
Log.e("tag", getContext().toString());
imageView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
return imageView;
}
}
}
{% endcodeblock %}
效果图
{% asset_img screenshot1.png screenshot1 %}{% asset_img screenshot2.png screenshot2 %}
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于