android的引导页库:Introduction
jopen
9年前
介绍:
又一个引导页库
运行效果:
使用说明:
build.gradle:
dependencies { compile ('com.rubengees:introduction:1.0.5@aar'){ transitive = true; } }
如果不行,看看是否有一个新版本。如果没有新版本并且还是不行,试试添加这个到build.gradle:
repositories { maven { url "http://dl.bintray.com/rubengees/maven" } }
Usage
如下创建一个IntroductionBuilder:
new IntroductionBuilder(this) //this is the Activity you want to start from.
添加一些Slide(页面)到你的引导界面:
new IntroductionBuilder(this).withSlides(generateSlides())
private List<Slide> generateSlides() { List<Slide> result = new ArrayList<>(); result.add(new Slide().withTitle("Some title").withDescription("Some description"). withColorResource(R.color.green).withImage(R.drawable.myImage)); result.add(new Slide().withTitle("Another title").withDescription("Another description") .withColorResource(R.color.indigo).withImage(R.drawable.myImage2)); return result; }
最后介绍你自己
new IntroductionBuilder(this).withSlides(generateSlides()).introduceMyself();
这很简单对吧?
你可以做很多自定义,下面讲讲解。
Options
你可以让用户决定,就要设置一样。在silde中添加选项决定要显示的元素:
new Slide().withTitle("Feature is doing something").withOption(new Option("Enable the feature")) .withColorResource(R.color.orange).withImage(R.drawable.image));
当用户跑完了引导介绍,你会在onActivityResult中收到选中的Options。读取结果:
<pre>@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == IntroductionBuilder.INTRODUCTION_REQUEST_CODE && resultCode == RESULT_OK) { String result = "User chose: "; for (Option option : data.<Option>getParcelableArrayListExtra(IntroductionActivity. OPTION_RESULT)) { result += option.getPosition() //The position of the Slide + (option.isActivated() ? " enabled" : " disabled"); } } }</pre>
使用 Gif作为图片
本library支持GIF。就像普通drawable一样添加就是了:
result.add(new Slide().withTitle("Some title").withDescription("Some description"). withColorResource(R.color.green).withImageResource(R.drawable.myGIF));
这将添加GIF,当导航到这个silde的时候gif会被自动播放。
Runtime Premissions
Android Marshmallow 引入了运行时权限,使用这个库可以轻松请求。为此,你可以如下添加一个全局的listener:
new IntroductionBuilder(this).withSlides(slides) .withOnSlideChangedListener(new IntroductionConfiguration.OnSlideChangedListener() { @Override public void onSlideChanged(int from, int to) { if (from == 0 && to == 1) { if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 12); } } } }).introduceMyself();
你可以检查是否授权,如下:
<pre>@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == 12) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "Permission was successfully granted!", Toast.LENGTH_LONG) .show(); } } }</pre>
风格
有两种风格:Translucent 和 Fullscreen。要应用其中一种风格,如下:
new IntroductionBuilder(this).withSlides(generateSlides()) .withStyle(IntroductionBuilder.STYLE_FULLSCREEN).introduceMyself();
Translucent 是默认风格。
More
更多的解释以及所有api可以在Wiki找到。
Minimum Sdk
The minimum required sdk is 10 (2.3.3 Gingerbread)
Libraries used in this project
-
android-gif-drawable For the GIFs.
-
SystemBarTint For the translucent style.
-