通过AndroidManifest配置来设计游戏的闪屏页

在为游戏接入渠道SDK时,很多渠道都要求设置闪屏页来启动游戏。下面就通过配置AndroidManifest来启动闪屏页。

1、配置AndroidManifest的启动Activity

<!--配置游戏Activity即闪屏页跳转到的Activity-->
<meta-data
   android:name="GAME_LAUNCH_ACTIVITY"
   android:value="com.dexlin.demo.GameActivity"/>

 <!--根据游戏实际情况修改screenOrientation属性 启动页-->
<activity
    android:name="com.dexlin.demo.GameLaunchActivity"
    android:configChanges="screenSize|keyboardHidden|orientation"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    android:label="@string/app_name"
    android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<!--填写游戏Activity-->
<activity
     android:name="com.dexlin.demo.GameActivity"
     android:configChanges="screenSize|keyboardHidden|orientation"
     android:label="@string/app_name"
     android:screenOrientation="portrait">
</activity>
  

2、在assert文件夹下放一张图片:  game_sdk_logo.png

3、设计闪屏页Activity

public class GameLaunchActivity extends Activity {
    private static final int PIC_SHOW_WIDTH_PADING = 40;
    private Bitmap mBitmap;
    private ImageView logoView;

    public GameLaunchActivity() {
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(!this.isTaskRoot() && this.getIntent().hasCategory("android.intent.category.LAUNCHER") && this.getIntent().getAction() != null && this.getIntent().getAction().equals("android.intent.action.MAIN")) {
            this.finish();
        } else {
            this.getWindow().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));
            boolean hasFindPic = this.initView();
            if(hasFindPic) {
                this.logoView.postDelayed(new Runnable() {
                    public void run() {
                        GameLaunchActivity.this.startGame();
                    }
                }, 3000L);
            } else {
                this.startGame();
            }

        }
    }

    private void startGame() {
        try {
            ApplicationInfo e = this.getPackageManager().getApplicationInfo(this.getPackageName(), 128);
            String gameLaunchActivity = e.metaData.getString("GAME_LAUNCH_ACTIVITY");
            Intent intent = new Intent();
            intent.setClassName(this, gameLaunchActivity);
            this.startActivity(intent);
        } catch (Exception var4) {
            Toast.makeText(this, "启动游戏失败,检查游戏启动类配置", 1).show();
        }

        this.finish();
    }

    protected void onDestroy() {
        super.onDestroy();
        if(this.mBitmap != null) {
            this.mBitmap.recycle();
            this.mBitmap = null;
        }

    }

    private boolean initView() {
        this.mBitmap = this.getFitBitmap();
        if(this.mBitmap != null) {
            this.logoView = new ImageView(this);
            this.logoView.setScaleType(ScaleType.FIT_XY);
            this.logoView.setImageBitmap(this.mBitmap);
            LayoutParams params = new LayoutParams(-2, -2);
            params.gravity = 17;
            FrameLayout main = new FrameLayout(this);
            main.addView(this.logoView, params);
            this.setContentView(main);
        }

        return this.mBitmap != null;
    }

    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(this.mBitmap != null) {
            Bitmap bitmap = this.mBitmap;
            bitmap.recycle();
        }

        this.mBitmap = this.getFitBitmap();
        if(this.mBitmap != null && this.logoView != null) {
            this.logoView.setImageBitmap(this.mBitmap);
        }

    }

    private Bitmap getFitBitmap() {
        Bitmap mBitmap = this.getBitmap("game_sdk_logo.png");
        if(mBitmap != null) {
            mBitmap = this.getScaleBitmap(mBitmap, (double)this.getScale(mBitmap));
        }

        return mBitmap;
    }

    private float getScale(Bitmap mBitmap) {
        float scale = 1.0F;
        int height = getScreenHeight(this);
        int screenW = getScreenWidth(this);
        Configuration config = this.getResources().getConfiguration();
        int orientation = config.orientation;
        int bitmapH = mBitmap.getHeight();
        int width = mBitmap.getWidth();
        float scaleSize;
        if(orientation == 2) {
            scaleSize = (float)((double)height * 0.33D);
            scale = scaleSize / (float)bitmapH;
            int scaleW = (int)(scale * (float)width);
            if(scaleW > screenW - 40) {
                scale = (float)(screenW - 40) / (float)width;
            }
        } else {
            scaleSize = (float)((double)screenW * 0.57D);
            scale = scaleSize / (float)width;
        }

        return scale;
    }

    public static int getScreenWidth(Activity context) {
        return context.getWindowManager().getDefaultDisplay().getWidth();
    }

    public static int getScreenHeight(Activity context) {
        return context.getWindowManager().getDefaultDisplay().getHeight();
    }

    public Bitmap getScaleBitmap(Bitmap bitmap, double scale) {
        return scale == 1.0D?bitmap:this.getScaleBitmap(bitmap, (double)bitmap.getWidth() * scale, (double)bitmap.getHeight() * scale);
    }

    public Bitmap getScaleBitmap(Bitmap bitmap, double newWidth, double newHeight) {
        if(null == bitmap) {
            return null;
        } else {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            double scaleWidth = newWidth / (double)width;
            double scaleHeight = newHeight / (double)height;
            double scale = Math.min(scaleWidth, scaleHeight);
            Matrix matrix = new Matrix();
            matrix.postScale((float)scale, (float)scale);
            Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
            if(bitmap != null && !bitmap.isRecycled()) {
                bitmap.recycle();
            }

            return result;
        }
    }

    private Bitmap getBitmap(String srcFileName) {
        AssetManager assetManager = this.getAssets();
        InputStream is = null;
        Bitmap bitmap = null;

        try {
            is = assetManager.open(srcFileName);
            bitmap = BitmapFactory.decodeStream(is);
        } catch (Exception var14) {
            ;
        } finally {
            if(is != null) {
                try {
                    is.close();
                } catch (IOException var13) {
                    ;
                }
            }

        }

        return bitmap;
    }
}












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值