vitamio和ijkplayer(bilibili开源)框架
首先创建项目,搭建环境
首先修改style.xml,(修改为NoActionBar)
在这里插入代码片<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
我们项目使用的是MVP的结构架构
1.抽取基类
在base包下创建一个BaseActivity
abstract class BaseActivity :AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(getLayoutId())
initListener()
initData()
}
//初始化数据
open protected fun initData() {
}
//adater和listener有关
open protected fun initListener(){
}
//获取布局id
abstract fun getLayoutId(): Int
}
接着在base包下创建一个BaseFragment
abstract class BaseFragment : Fragment(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
init()
}
//初始化fragment
protected fun init() {
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return initView()
}
//获取布局id
abstract fun initView(): View?
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
initListener()
initData()
}
//初始化listen
protected fun initListener() {
}
//初始化数据
protected fun initData(){
}
}
欢迎界面
在ui/activity文件下创建一个SpiashActivity类
class SpiashActivity :BaseActivity(){
override fun getLayoutId(): Int {
return R.layout.activity_splash
}
}
创建activity_splash.xml文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/splashimage"
android:scaleX="1.5"
android:scaleY="1.5"
android:scaleType="centerCrop"
android:src="@drawable/splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ImageView>
</LinearLayout>
在style.xml文件中添加新元素
<style name="AppTheme.FullScreen">
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
修改AndroidManifest文件
...
<activity android:name=".ui.activity.SpiashActivity"
android:theme="@style/AppTheme.FullScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ui.activity.MainActivity">
</activity>
...
最后修改SpiashActivity类,添加动画和跳转
class SpiashActivity :BaseActivity(),ViewPropertyAnimatorListener{
override fun getLayoutId(): Int {
return R.layout.activity_splash
}
override fun initData() {
//开始欢迎界面动画缩小
ViewCompat.animate(splashimage).scaleX(1.0f).scaleY(1.0f).setListener(this).setDuration(2000)
}
override fun onAnimationEnd(view: View?) {
//动画结束进入主界面
val intent=Intent(this,MainActivity::class.java)
startActivity(intent)
//避免退出主界面后,进入欢迎界面
finish()
}
override fun onAnimationCancel(view: View?) {
}
override fun onAnimationStart(view: View?) {
}
}
创建主界面
首先在github搜索bottombar引入库
compile 'com.roughike:bottom-bar:2.3.1'
修改主界面activity_main.ml
<LinearLayout 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=".ui.activity.MainActivity"
android:orientation="vertical">
<include layout="@layout/toolbar"></include>
<FrameLayout
android:id="@+id/container"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
></FrameLayout>
<com.roughike.bottombar.BottomBar
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="60dp"
app:bb_tabXmlResource="@xml/bottombar_tabs"
></com.roughike.bottombar.BottomBar>
</LinearLayout>
再创建bottombar_tabs.xml文件
<tabs>
<tab
id="@+id/tab_shouye"
icon="@drawable/home"
title="首页" />
<tab
id="@+id/tab_mv"
icon="@drawable/mv"
title="MV" />
<tab
id="@+id/tab_vbang"
icon="@drawable/bangdan"
title="V榜" />
<tab
id="@+id/tab_yuedan"
icon="@drawable/music"
title="悦单" />
</tabs>
创建Toolbar按钮跳转设置界面
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
toolbar.inflateMenu(R.menu.menu)
toolbar.setOnMenuItemClickListener{
when(it?.itemId){
R.id.setting -> {
//跳转到设置界面
val intent = Intent(this, Setting::class.java)
startActivity(intent)
}
}
false
}
}
}
修改activity_setting.xml文件
<LinearLayout 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"
android:orientation="vertical"
tools:context=".ui.activity.Setting">
<include layout="@layout/toolbar"></include>
<fragment
android:id="@+id/setting_fragment"
class="com.example.audio.ui.fragment.SettingFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
></fragment>
</LinearLayout>
创建SettingFragment类
class SettingFragment : PreferenceFragment(){
override fun onCreateView(
inflater: LayoutInflater?,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
addPreferencesFromResource(R.xml.setting)
return super.onCreateView(inflater, container, savedInstanceState)
}
override fun onPreferenceTreeClick(
preferenceScreen: PreferenceScreen?,
preference: Preference?
): Boolean {
val key =preference?.key
when(key){
"about" ->{
Toast.makeText(activity,"关于",Toast.LENGTH_SHORT).show()
}
}
return super.onPreferenceTreeClick(preferenceScreen, preference)
}
}
在xml文件下创建setting.xml文件
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:key="clear_cache"
android:title="清理缓存"
></Preference>
<SwitchPreference
android:key="push"
android:title="推送通知"
></SwitchPreference>
<SwitchPreference
android:key="no_wifi"
android:title="非Wifi下加载图片"
></SwitchPreference>
<Preference
android:key="about"
android:title="关于"
></Preference>
</PreferenceScreen>
bottomBar设置跳转界面
首先我们创建一个FrangmentUtil工具类
object FragmentUtil {
val homefragment by lazy { HomeFragment() }
val mvfragment by lazy { MvFragment() }
val vbangFragment by lazy { VbangFragment() }
val yuedanFragment by lazy { YuedanFragment() }
fun getFragment(fragmentId:Int):BaseFragment{
when(fragmentId){
R.id.tab_shouye -> return homefragment
R.id.tab_mv -> return mvfragment
R.id.tab_vbang -> return vbangFragment
R.id.tab_yuedan -> return yuedanFragment
}
return homefragment
}
}
其次我们修改MainActivity中的代码
class MainActivity : BaseActivity() {
...
override fun initListener() {
super.initListener()
...
//设置bottombar上面按钮监听
bottomBar.setOnTabSelectListener {
val transaction =supportFragmentManager.beginTransaction()
transaction.replace(R.id.container,FragmentUtil.getFragment(it))
transaction.commit()
}
}
override fun getLayoutId(): Int {
return R.layout.activity_main
}
}