ScreenAdaptActivity.java 2.9 KB
Newer Older
B
Blankj 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
package com.blankj.androidutilcode.feature.core.screen;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blankj.androidutilcode.R;
import com.blankj.androidutilcode.base.BaseActivity;
import com.blankj.utilcode.util.BarUtils;
B
Blankj 已提交
14
import com.blankj.utilcode.util.LogUtils;
B
Blankj 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/09/27
 *     desc  : demo about ScreenUtils
 * </pre>
 */
public class ScreenAdaptActivity extends BaseActivity {

    private TextView tvUp;
    private TextView tvDown;

    public static void start(Context context) {
        Intent starter = new Intent(context, ScreenAdaptActivity.class);
        context.startActivity(starter);
    }

    @Override
    public void initData(@Nullable Bundle bundle) {
        if (ScreenUtils.isPortrait()) {
B
Blankj 已提交
39
            ScreenUtils.adaptScreen4VerticalSlide(this, 540);
B
Blankj 已提交
40
        } else {
B
Blankj 已提交
41
            ScreenUtils.adaptScreen4HorizontalSlide(this, 540);
B
Blankj 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        }
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_screen_adapt;
    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {
        tvUp = findViewById(R.id.tv_up);
        tvDown = findViewById(R.id.tv_down);
        if (!ScreenUtils.isPortrait()) {
            updateLayout();
        }
    }

    @Override
    public void doBusiness() {

    }

    @Override
    public void onWidgetClick(View view) {

    }

    public void toggleFullScreen(View view) {
        ScreenUtils.toggleFullScreen(this);
        updateLayout();
    }

    private void updateLayout() {
        int statusBarHeight = BarUtils.getStatusBarHeight();
        int statusBarHeightInDp = SizeUtils.px2dp(this, statusBarHeight);
        ViewGroup.LayoutParams upLayoutParams = tvUp.getLayoutParams();
        ViewGroup.LayoutParams downLayoutParams = tvDown.getLayoutParams();
        if (ScreenUtils.isFullScreen(this)) {
B
Blankj 已提交
80
            int height = 360 / 2;
B
Blankj 已提交
81 82 83 84 85 86 87 88 89
            String s = height + "dp";
            upLayoutParams.height = SizeUtils.dp2px(this, height);
            tvUp.setLayoutParams(upLayoutParams);
            tvUp.setText(s);

            downLayoutParams.height = SizeUtils.dp2px(this, height);
            tvDown.setLayoutParams(downLayoutParams);
            tvDown.setText(s);
        } else {
B
Blankj 已提交
90
            int height = 360 / 2 - statusBarHeightInDp / 2;
B
Blankj 已提交
91 92 93 94 95 96 97 98 99 100 101
            String s = height + "dp";
            upLayoutParams.height = SizeUtils.dp2px(this, height);
            tvUp.setLayoutParams(upLayoutParams);
            tvUp.setText(s);

            downLayoutParams.height = SizeUtils.dp2px(this, height);
            tvDown.setLayoutParams(downLayoutParams);
            tvDown.setText(s);
        }
    }
}