⾃定义Unity闪屏ForAndroid
嗯,⼯作中遇到策划提了⼀个⼩需求:酷炫的闪屏。。
我们知道unity⾃带的闪屏。。。真·⼀张图⽚,其实没有任何的动画效果,所以来看⼀下怎么定义⾃⼰的闪屏⽅案吧。
基本思路就是⽤Android的闪屏内嵌到Unity中,通过jar包和Android资源以及程序启动Activity设置,实现⾃定义的闪屏。
⾸先使⽤Eclipse或Android Studio新建⼀个Library⼯程,新建闪屏Activity及布局⽂件,布局⽂件很简单,⼀张match_parent的图⽚就可以了,闪屏代码包含两个动画,缩放和透明度变化,可以⾃⼰定制别的效果,⽐如旋转动画什么的。
源码:
public class Splash extends Activity {
final String TAG = "Splash";
private final int maxSupport = 5;
AnimationListenerBack animationListener;
// AlphaAnimation alphaAni;
ImageView splashView;
Class<?> next;
private boolean ispause;
private long splashDur = 3000;
private float fromAlpha = 0.1f;
private float toAlpha = 1.0f;
private float fromScale = 1.0f;
private float toScale = 1.2f;
private int splashCount = 1;
private int currentIndex = 1;
private WeakReference<Drawable> splash;//弱引⽤防⽌OOM
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setondraweble();
setContentView(R.layout.activity_splash1);
splashView = this.findViewById(R.id.splash);
getSplashCount();
initView();
}
private void getSplashCount() {
// TODO Auto-generated method stub
String count = getString(R.string.splash_count);
splashCount = Integer.parseInt(count);
if(splashCount > maxSupport)
splashCount = maxSupport;
nextLoader(getString(R.string.main_activity));
}
private void nextLoader(String className) {
ClassLoader loader = ClassLoader();
try {
;
Log.i(TAG, "loadClass: ===> " + className);
next = loader.loadClass(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
Log.e("Splash", String.format("not find main entry, please check strings setting main_activity"));
}
}
private void initView() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
splash = new WeakReference<Drawable>(getResources().getDrawable(getSplashId(currentIndex)));
} else {
splash = new WeakReference<Drawable>(getResources().getDrawable(getSplashId(currentIndex)));
}
splashView.());
initAnimation();
}
private int getSplashId(int curIndex) {
int id = R.mipmap.sp1;
switch (curIndex) {
case1:
id = R.mipmap.sp1;
break;
case2:
id = R.mipmap.sp2;
break;
case3:
id = R.mipmap.sp3;
break;
case4:
id = R.mipmap.sp4;
break;
case5:
id = R.mipmap.sp5;
break;
default:
break;
}
return id;
}
private void initAnimation() {
AnimationSet aniSet = new AnimationSet(true);
WindowManager wm = getWindowManager();
int centerX = wm.getDefaultDisplay().getWidth() / 2;
int centerY = wm.getDefaultDisplay().getHeight() / 2;
Log.i("center", String.format("## centerX:%d, centerY:%d", centerX, centerY));
WeakReference<ScaleAnimation> scaleAnimation = new WeakReference<>(new ScaleAnimation(fromScale, toScale, fromScale, toScale, centerX, cen WeakReference<AlphaAnimation> alphaAnimation = new WeakReference<>(new AlphaAnimation(this.fromAlpha, Alpha));
aniSet.setDuration(splashDur);
aniSet.setFillAfter(true);
aniSet.());
aniSet.());
animationListener = new AnimationListenerBack();
aniSet.setAnimationListener(animationListener);
splashView.startAnimation(aniSet);
}
private void endAnimation(){
WeakReference<AlphaAnimation> end = new WeakReference<>(new AlphaAnimation(toAlpha, 0.01f));
<().setFillAfter(true);
<().setDuration(1000);
<().setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
currentIndex++;
initView();
initView();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
splashView.());
}
private void setondraweble() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN; Window window = Window();
window.setFlags(flag, flag);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
ispause = true;
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
ispause = false;
}
private class AnimationListenerBack implements AnimationListener {
@Override
public void onAnimationStart(Animation animation) {
/
/ TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
if(currentIndex < splashCount){
endAnimation();
}else{
Intent intent = new Intent();
if (next == null) {
try {
throw new Exception("not find main entry!");
} catch (Exception e) {
e.printStackTrace();
return;
}
}
intent.setClass(Splash.this, next);
startActivity(intent);
finish();
}
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
⽤到的知识点:安卓动画Animation Java 弱引⽤WeakReference
Java 反射
适⽤平台:Andorid
⽀持⼯具:
1. Eclipse
eclipse android2. Android Studio
3. Unity
最⼤⽀持闪屏数量:5
关于使⽤:
在Eclipse中使⽤:
引⽤Libarbry⼯程:SplashUtilsLibrary
设置启动Activity:
将manifest xml⽂件中的启动⼊⼝注释,如图,或者设置为Library⼯程的Splash为主Activity.
}
}
在Android Studio中使⽤:
导⼊Module SplashUtilsLibrary:
File>New>Import New Moudle ,选择SplashUtilsLibrary⼯程即可
并在gradle⾥进⾏引⽤:
设置启动Activity,设置⽅式和Eclipse中⼀致.
在Unity中使⽤:
导⼊资源:
Res⽬录: 对应unity⽬录:plugins/Android/res
闪屏图⽚资源
将⾃定义的闪屏图⽚导⼊到Unity对应⽬录中:以sp格式命名layout:
导⼊布局⽂件l到unity对应⽬录在l中添加以下配置:
修改splash_count的值即可修改闪屏数量,最⼤⽀持5张.在AndroidManifest中添加配置:<string name ="main_activity">dy.splashdemo.MainActivity</string ><string name ="splash_count">3</string >
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论