|
| 1 | +package com.blankj.androidutilcode.feature.core.screen; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.Intent; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.support.annotation.Nullable; |
| 7 | +import android.view.View; |
| 8 | +import android.view.ViewGroup; |
| 9 | +import android.widget.TextView; |
| 10 | + |
| 11 | +import com.blankj.androidutilcode.R; |
| 12 | +import com.blankj.androidutilcode.base.BaseActivity; |
| 13 | +import com.blankj.utilcode.util.BarUtils; |
| 14 | +import com.blankj.utilcode.util.ScreenUtils; |
| 15 | +import com.blankj.utilcode.util.SizeUtils; |
| 16 | + |
| 17 | +/** |
| 18 | + * <pre> |
| 19 | + * author: Blankj |
| 20 | + * blog : http://blankj.com |
| 21 | + * time : 2016/09/27 |
| 22 | + * desc : demo about ScreenUtils |
| 23 | + * </pre> |
| 24 | + */ |
| 25 | +public class ScreenAdaptActivity extends BaseActivity { |
| 26 | + |
| 27 | + private TextView tvUp; |
| 28 | + private TextView tvDown; |
| 29 | + |
| 30 | + public static void start(Context context) { |
| 31 | + Intent starter = new Intent(context, ScreenAdaptActivity.class); |
| 32 | + context.startActivity(starter); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public void initData(@Nullable Bundle bundle) { |
| 37 | + if (ScreenUtils.isPortrait()) { |
| 38 | + ScreenUtils.adaptScreen4VerticalSlide(this, 360); |
| 39 | + } else { |
| 40 | + ScreenUtils.adaptScreen4HorizontalSlide(this, 360); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public int bindLayout() { |
| 46 | + return R.layout.activity_screen_adapt; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void initView(Bundle savedInstanceState, View contentView) { |
| 51 | + tvUp = findViewById(R.id.tv_up); |
| 52 | + tvDown = findViewById(R.id.tv_down); |
| 53 | + if (!ScreenUtils.isPortrait()) { |
| 54 | + updateLayout(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void doBusiness() { |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onWidgetClick(View view) { |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + public void toggleFullScreen(View view) { |
| 69 | + ScreenUtils.toggleFullScreen(this); |
| 70 | + updateLayout(); |
| 71 | + } |
| 72 | + |
| 73 | + private void updateLayout() { |
| 74 | + int statusBarHeight = BarUtils.getStatusBarHeight(); |
| 75 | + int statusBarHeightInDp = SizeUtils.px2dp(this, statusBarHeight); |
| 76 | + ViewGroup.LayoutParams upLayoutParams = tvUp.getLayoutParams(); |
| 77 | + ViewGroup.LayoutParams downLayoutParams = tvDown.getLayoutParams(); |
| 78 | + if (ScreenUtils.isFullScreen(this)) { |
| 79 | + int height = 720 / 2 / 2; |
| 80 | + String s = height + "dp"; |
| 81 | + upLayoutParams.height = SizeUtils.dp2px(this, height); |
| 82 | + tvUp.setLayoutParams(upLayoutParams); |
| 83 | + tvUp.setText(s); |
| 84 | + |
| 85 | + downLayoutParams.height = SizeUtils.dp2px(this, height); |
| 86 | + tvDown.setLayoutParams(downLayoutParams); |
| 87 | + tvDown.setText(s); |
| 88 | + } else { |
| 89 | + int height = 720 / 2 / 2 - statusBarHeightInDp / 2; |
| 90 | + String s = height + "dp"; |
| 91 | + upLayoutParams.height = SizeUtils.dp2px(this, height); |
| 92 | + tvUp.setLayoutParams(upLayoutParams); |
| 93 | + tvUp.setText(s); |
| 94 | + |
| 95 | + downLayoutParams.height = SizeUtils.dp2px(this, height); |
| 96 | + tvDown.setLayoutParams(downLayoutParams); |
| 97 | + tvDown.setText(s); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments