YFAndroidLibs之SplshView的用法及源码解析
关于(About)
SplashView故名思意,是指进入应用时的SPlash(闪屏),效果图如下:
设计思路及主要接口(Features)
该SplashView主要的技术点有:
1.自定义declare-styleable
<declare-styleable name = "SplashView">
<attr format="integer" name="time"/>
<attr format = "reference" name = "Bg" />
<attr format = "reference" name = "txt_style" />
</declare-styleable>
从而实现黑盒操作,可直接在xml中定义属性。
2.继承CountDownTimer,实现时间递减:
class TimeCount extends CountDownTimer {
/**
* @param millisInFuture
* @param countDownInterval
*/
public TimeCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
time.setText("("+(millisUntilFinished/1000)+"s)");
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
//计时完成
time.setText("("+0+"s)");
//跳转操作
onFinishListener.setFinish();
}
}
3.自定义完成监听器接口:OnFinishListener
/**
*设置读秒完成后的操作
*@author yihui
*@version 1.0
*/
public interface OnFinishListener {
void setFinish();
}
4.设置直接点击跳过事件:
/**
* 设置点击事件
*/
public void setJumpClick(OnClickListener listener){
time_bt.setOnClickListener(listener);
}
主要暴露接口和设置:
- 设置闪屏图片;
- 设置持续时间;
- 继承点击跳过监听器;
- 继承读秒完成监听器,处理读秒完成后的事件跳转;
- 设置界面字体样式。
- 其他还在扩展中。。
使用
导入包:
详见我的另外一篇博文:http://blog.csdn.net/u011072613/article/details/53889596
在布局文件中添加
<com.example.yf_library.splash.SplashView
android:id="@+id/sp_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:time="6000"
app:Bg="@drawable/guide2"
app:txt_style="@drawable/flag_01"
/>
记得要在根布局下添加:
xmlns:app="http://schemas.android.com/apk/res/你的包名"
代码设置:
1.设置背闪屏图片:
sp_view.setBg(getDrawable(R.drawable.splash));
当然你也可以在布局中设置:
app:Bg="@drawable/splash"
2.持续时间设置
sp_view.setTime(t,new OnFinishListener() {
@Override
public void setFinish() {
// TODO Auto-generated method stub
//在此处设置读秒完成后的操作
Intent mIntent=new Intent(getApplicationContext(),CommonActivity.class);
startActivity(mIntent);
}
});
或者这样:
app:time="6000"
此时你可以省略setTime方法中的time,转而调用:
sp_view.setTime(new OnFinishListener() {
@Override
public void setFinish() {
// TODO Auto-generated method stub
Intent mIntent=new Intent(getApplicationContext(),CommonActivity.class);
startActivity(mIntent);
}
});
如果你既没有在xml布局中设置时间,也没有在代码中设置时间,那么系统将会调用默认的闪屏时间(6秒)。
3.像大多数闪屏一样,本SplashView也提供了,直接跳转监听器
sp_view.setJumpClick(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//可以在此处设置跳转
Toast.makeText(SpalshViewActivity.this, "点击跳过", 1).show();;
Intent mIntent=new Intent(getApplicationContext(),CommonActivity.class);
startActivity(mIntent);
}
});
4.如果你觉得“跳过:(6s)”的字体样式不好看,提供了setTxtStyle:
/*
* 设置字体样式
*/
@SuppressLint("NewApi")
public void setTxtStyle(int resId){
time_ll.setBackgroundResource(resId);
}
也可以通过:
/**
* 设置字体背景
*/
@SuppressLint(“NewApi”)
public void setTextBg(int drawable){
time_ll.setBackgroundResource(drawable);
}
设置字体背景。
大概的方法就这么多,Splash相对简单,但是在一个Android程序中,承担的任务比较中,基本上每次退出重新进入都会能看到它,所以关键是性能。
该SplashView后期还会继续扩展。。。。(比如添加多个动画界面,多个连接界面等),敬请期待。