赞
踩
在Unity应用启动时,加自定义的开屏动画意义
1、可以遮住Unity个人版的启动log
2、防止unity黑屏太久时间导致玩家不愿意等待
#步骤
一、创建动画的布局界面
activity_main.xml的布局设置
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:layout_centerInParent="false"> <ImageView android:id="@+id/splash_frame" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:adjustViewBounds="false" android:scaleType="fitCenter" android:src="@drawable/sequence" tools:src="@drawable/sequence" /> </RelativeLayout>
动画布局
sequence.xml
drawable/splash_ani_0表示drawable文件夹中的图片名字
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true"
>
<item android:duration="40" android:drawable="@drawable/splash_ani_0"/>
<item android:duration="40" android:drawable="@drawable/splash_ani_1"/>
</animation-list>
二、在代码中调用
在主Activity代码中调用
// 启动图组件 private ImageView bgView = null; private View view = null; AnimationDrawable animationDrawable = null; //mUnityPlayer是UnityPlayerActivity中的变量,可以直接使用 @Override // com.unity3d.player.UnityPlayerActivity public void onCreate(Bundle savedInstanceState) { //在onCreate方法中通过UI线程的方式调用 activity.runOnUiThread(new Runnable() { @Override public void run() { showSplash(); } }); } public void showSplash(){ Log.i(Tag, "showSplash"); if (bgView != null || mUnityPlayer == null) return; //先为画布添加一层背景 bgView = new ImageView(this); int color_splash_bg = R.color.color_splash_bg; bgView.setBackgroundColor(getResources().getColor(color_splash_bg)); mUnityPlayer.addView(bgView); //将我们创建的活动页添加到应用画布上 LayoutInflater flater = LayoutInflater.from(this); int layoutID = getResources().getIdentifier("activity_main", "layout", getPackageName()); view = flater.inflate(layoutID, null); int frame_id = view.getResources().getIdentifier("splash_frame", "id", getPackageName()); ImageView frameView = (ImageView)view.findViewById(frame_id); mUnityPlayer.addView(view); //启用动画,此处利用程序动态控制方便后面关闭 animationDrawable = (AnimationDrawable) frameView.getDrawable(); animationDrawable.start(); }
这样子就能在启动UnityActivity时展示一个开屏动画了
既然有显示,那当然需要隐藏了,在UI线程中调用就行,可以在进入到下载或者登录界面时关闭这个动画
private void hideSplash() { Animation ani = new AlphaAnimation(1f, 0f); ani.setDuration(100); ani.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { Log.i(Tag, "hideSplash onAnimationEnd"); if(animationDrawable != null){ animationDrawable.stop(); animationDrawable = null; } if (view != null) { mUnityPlayer.removeView(view); view = null; } if (bgView != null) { mUnityPlayer.removeView(bgView); bgView = null; } } @Override public void onAnimationRepeat(Animation animation) { } }); bgView.startAnimation(ani); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。