Java Code Examples for android.view.Display#getMetrics()

The following examples show how to use android.view.Display#getMetrics() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LoginDialog.java    From blade-player with GNU General Public License v3.0 6 votes vote down vote up
private void setLayoutSize() {
    WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);

    int dialogWidth = ViewGroup.LayoutParams.MATCH_PARENT;
    int dialogHeight = ViewGroup.LayoutParams.MATCH_PARENT;

    // If width or height measured in dp exceeds accepted range,
    // use max values and convert them back to pixels before setting the size.
    if (metrics.widthPixels / metrics.density > MAX_WIDTH_DP) {
        dialogWidth = (int) (MAX_WIDTH_DP * metrics.density);
    }

    if (metrics.heightPixels / metrics.density > MAX_HEIGHT_DP) {
        dialogHeight = (int) (MAX_HEIGHT_DP * metrics.density);
    }

    LinearLayout layout = (LinearLayout) findViewById(R.id.com_spotify_sdk_login_webview_container);
    layout.setLayoutParams(new FrameLayout.LayoutParams(dialogWidth, dialogHeight, Gravity.CENTER));
}
 
Example 2
Source File: ScreenInfo.java    From MobileInfo with Apache License 2.0 6 votes vote down vote up
private static boolean checkHasNavigationBar(Context context) {
    if (context instanceof Activity) {
        WindowManager windowManager = ((Activity) context).getWindowManager();
        Display d = windowManager.getDefaultDisplay();

        DisplayMetrics realDisplayMetrics = new DisplayMetrics();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            d.getRealMetrics(realDisplayMetrics);
        }

        int realHeight = realDisplayMetrics.heightPixels;
        int realWidth = realDisplayMetrics.widthPixels;

        DisplayMetrics displayMetrics = new DisplayMetrics();
        d.getMetrics(displayMetrics);

        int displayHeight = displayMetrics.heightPixels;
        int displayWidth = displayMetrics.widthPixels;

        return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
    }
    return false;
}
 
Example 3
Source File: ResourceHelper.java    From MaterialDesignSupport with MIT License 6 votes vote down vote up
public static int getScreenHeight() {
    Context context = CoreMaterialApplication.getContext();
    if (context == null) {
        return -1;
    }

    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    if (windowManager == null) {
        return -1;
    }

    Display display = windowManager.getDefaultDisplay();
    if (display == null) {
        return -1;
    }

    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    int height = metrics.heightPixels;

    return height;
}
 
Example 4
Source File: MyActivity.java    From googleads-mobile-android-examples with Apache License 2.0 6 votes vote down vote up
private AdSize getAdSize() {
  // Determine the screen width (less decorations) to use for the ad width.
  Display display = getWindowManager().getDefaultDisplay();
  DisplayMetrics outMetrics = new DisplayMetrics();
  display.getMetrics(outMetrics);

  float density = outMetrics.density;

  float adWidthPixels = adContainerView.getWidth();

  // If the ad width isn't known, default to the full screen width.
  if (adWidthPixels == 0) {
    adWidthPixels = outMetrics.widthPixels;
  }

  int adWidth = (int) (adWidthPixels / density);
  return AdSize.getCurrentOrientationBannerAdSizeWithWidth(this, adWidth);
}
 
Example 5
Source File: BaseApplication.java    From letv with Apache License 2.0 5 votes vote down vote up
@TargetApi(17)
public void initScreenInfo() {
    Display display = ((WindowManager) getSystemService("window")).getDefaultDisplay();
    DisplayMetrics dm = new DisplayMetrics();
    display.getMetrics(dm);
    sWidth = dm.widthPixels;
    sHeight = dm.heightPixels;
    sDensity = dm.density;
    if (LetvUtils.getSDKVersion() >= 17) {
        display.getRealMetrics(dm);
        sRawHeight = dm.heightPixels;
        return;
    }
    sRawHeight = sHeight;
}
 
Example 6
Source File: CanPhotoHelper.java    From CanPhotos with Apache License 2.0 5 votes vote down vote up
/**
 * 得到屏幕相关参数
 *
 * @param context
 * @return
 */
public DisplayMetrics getScreenDisplayMetrics(Context context) {
    WindowManager manager = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);

    DisplayMetrics displayMetrics = new DisplayMetrics();
    Display display = manager.getDefaultDisplay();
    display.getMetrics(displayMetrics);

    return displayMetrics;

}
 
Example 7
Source File: Phlex.java    From Phlex with Apache License 2.0 5 votes vote down vote up
public static int px2dp(Context context, int px) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    DisplayMetrics displaymetrics = new DisplayMetrics();
    display.getMetrics(displaymetrics);
    return (int) (px / displaymetrics.density + 0.5f);
}
 
Example 8
Source File: DisplayUtility.java    From Loop with Apache License 2.0 5 votes vote down vote up
public static int px2dp(Context context, int px) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();

    DisplayMetrics displaymetrics = new DisplayMetrics();
    display.getMetrics(displaymetrics);

    return (int) (px / displaymetrics.density + 0.5f);
}
 
Example 9
Source File: Phlex.java    From Phlex with Apache License 2.0 5 votes vote down vote up
public static int dp2px(Context context, int dp) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    DisplayMetrics displaymetrics = new DisplayMetrics();
    display.getMetrics(displaymetrics);
    return (int) (dp * displaymetrics.density + 0.5f);
}
 
Example 10
Source File: ScrollerActivity.java    From Animer with Apache License 2.0 5 votes vote down vote up
private void measureDisplay() {
    Display display = getWindowManager().getDefaultDisplay();
    DisplayMetrics outMetrics = new DisplayMetrics ();
    display.getMetrics(outMetrics);
    float density  = getResources().getDisplayMetrics().density;
    float dpHeight = outMetrics.heightPixels / density;
    float dpWidth  = outMetrics.widthPixels / density;
    screenHeight = dpToPx(dpHeight,getResources());
    screenWidth = dpToPx(dpWidth,getResources());
    cellSize =  (int) getResources().getDimension(R.dimen.cell_size_dp);
    Log.e("inDP","doHeight"+ dpHeight + "dpWidth" + dpWidth);
}
 
Example 11
Source File: ImmersiveModeCompatPromptBackground.java    From MaterialTapTargetPrompt with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
protected DisplayMetrics getDisplayMetrics() {
    Display defaultDisplay = mWindowManager.getDefaultDisplay();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        defaultDisplay.getRealMetrics(mBaseMetrics);
    } else {
        defaultDisplay.getMetrics(mBaseMetrics);
    }
    return mBaseMetrics;
}
 
Example 12
Source File: PLManager.java    From panoramagl with Apache License 2.0 5 votes vote down vote up
@Override
public CGSize getSize() {
    Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    final DisplayMetrics displayMetrics = new DisplayMetrics();
    display.getMetrics(displayMetrics);
    return mTempSize.setValues(displayMetrics.widthPixels, displayMetrics.heightPixels);
}
 
Example 13
Source File: Helper.java    From Android-RTEditor with Apache License 2.0 4 votes vote down vote up
private static DisplayMetrics getDisplayMetrics() {
    Display display = ((WindowManager) RTApi.getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    return metrics;
}
 
Example 14
Source File: DisplayManager.java    From TigerVideo with Apache License 2.0 4 votes vote down vote up
public static float screenHeightPhysical(Context context) {
    Display display = getDisplay(context);
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    return (float)display.getHeight() / metrics.densityDpi;
}
 
Example 15
Source File: DisplayManager.java    From TigerVideo with Apache License 2.0 4 votes vote down vote up
public static float screenWidthPhysical(Context context) {
    Display display = getDisplay(context);
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    return (float)display.getWidth() / metrics.densityDpi;
}
 
Example 16
Source File: SmileyPickerUtility.java    From iBeebo with GNU General Public License v3.0 4 votes vote down vote up
public static int getScreenHeight(Activity paramActivity) {
    Display display = paramActivity.getWindowManager().getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    return metrics.heightPixels;
}
 
Example 17
Source File: Helper.java    From memoir with Apache License 2.0 4 votes vote down vote up
private static DisplayMetrics getDisplayMetrics() {
    Display display = ((WindowManager) RTApi.getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    return metrics;
}
 
Example 18
Source File: Helper.java    From memoir with Apache License 2.0 4 votes vote down vote up
private static DisplayMetrics getDisplayMetrics() {
    Display display = ((WindowManager) RTApi.getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    return metrics;
}
 
Example 19
Source File: KIKATSCUtil.java    From pc-android-controller-android with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
public static Bitmap acquireScreenshot(Context context) {
    WindowManager mWinManager = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics metrics = new DisplayMetrics();
    Display display = mWinManager.getDefaultDisplay();
    display.getMetrics(metrics);
    // 屏幕高
    int height = metrics.heightPixels;
    // 屏幕的宽
    int width = metrics.widthPixels;

    int pixelformat = display.getPixelFormat();
    PixelFormat localPixelFormat1 = new PixelFormat();
    PixelFormat.getPixelFormatInfo(pixelformat, localPixelFormat1);
    // 位深
    int deepth = localPixelFormat1.bytesPerPixel;

    byte[] arrayOfByte = new byte[height * width * deepth];
    try {
        // 读取设备缓存,获取屏幕图像流
        InputStream localInputStream = readAsRoot();
        DataInputStream localDataInputStream = new DataInputStream(
                localInputStream);
        localDataInputStream.readFully(arrayOfByte);
        localInputStream.close();

        int[] tmpColor = new int[width * height];
        int r, g, b;
        for (int j = 0; j < width * height * deepth; j += deepth) {
            b = arrayOfByte[j] & 0xff;
            g = arrayOfByte[j + 1] & 0xff;
            r = arrayOfByte[j + 2] & 0xff;
            tmpColor[j / deepth] = (r << 16) | (g << 8) | b | (0xff000000);
        }
        // 构建bitmap
        Bitmap scrBitmap = Bitmap.createBitmap(tmpColor, width, height,
                Bitmap.Config.ARGB_8888);
        return scrBitmap;

    } catch (Exception e) {
        L.d( "#### 读取屏幕截图失败");
        e.printStackTrace();
    }
    return null;

}
 
Example 20
Source File: SignalPinReminderDialog.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
public static void show(@NonNull Context context, @NonNull Launcher launcher, @NonNull Callback mainCallback) {
  if (!SignalStore.kbsValues().hasPin()) {
    throw new AssertionError("Must have a PIN!");
  }

  Log.i(TAG, "Showing PIN reminder dialog.");

  AlertDialog dialog = new AlertDialog.Builder(context, ThemeUtil.isDarkTheme(context) ? R.style.Theme_Signal_AlertDialog_Dark_Cornered_ColoredAccent : R.style.Theme_Signal_AlertDialog_Light_Cornered_ColoredAccent)
                                      .setView(R.layout.kbs_pin_reminder_view)
                                      .setCancelable(false)
                                      .setOnCancelListener(d -> RegistrationLockReminders.scheduleReminder(context, false))
                                      .create();

  WindowManager  windowManager = ServiceUtil.getWindowManager(context);
  Display        display       = windowManager.getDefaultDisplay();
  DisplayMetrics metrics       = new DisplayMetrics();
  display.getMetrics(metrics);

  dialog.show();
  dialog.getWindow().setLayout((int)(metrics.widthPixels * .80), ViewGroup.LayoutParams.WRAP_CONTENT);

  EditText pinEditText = (EditText) DialogCompat.requireViewById(dialog, R.id.pin);
  TextView pinStatus   = (TextView) DialogCompat.requireViewById(dialog, R.id.pin_status);
  TextView reminder    = (TextView) DialogCompat.requireViewById(dialog, R.id.reminder);
  View     skip        = DialogCompat.requireViewById(dialog, R.id.skip);
  View     submit      = DialogCompat.requireViewById(dialog, R.id.submit);

  SpannableString reminderText = new SpannableString(context.getString(R.string.KbsReminderDialog__to_help_you_memorize_your_pin));
  SpannableString forgotText   = new SpannableString(context.getString(R.string.KbsReminderDialog__forgot_pin));

  pinEditText.post(() -> {
    if (pinEditText.requestFocus()) {
      ServiceUtil.getInputMethodManager(pinEditText.getContext()).showSoftInput(pinEditText, 0);
    }
  });
  ViewCompat.setAutofillHints(pinEditText, HintConstants.AUTOFILL_HINT_PASSWORD);

  switch (SignalStore.pinValues().getKeyboardType()) {
    case NUMERIC:
      pinEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
      break;
    case ALPHA_NUMERIC:
      pinEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
      break;
  }

  ClickableSpan clickableSpan = new ClickableSpan() {
    @Override
    public void onClick(@NonNull View widget) {
      dialog.dismiss();
      launcher.launch(CreateKbsPinActivity.getIntentForPinChangeFromForgotPin(context), CreateKbsPinActivity.REQUEST_NEW_PIN);
    }
  };

  forgotText.setSpan(clickableSpan, 0, forgotText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

  reminder.setText(new SpannableStringBuilder(reminderText).append(" ").append(forgotText));
  reminder.setMovementMethod(LinkMovementMethod.getInstance());

  PinVerifier.Callback callback = getPinWatcherCallback(context, dialog, pinEditText, pinStatus, mainCallback);
  PinVerifier          verifier = new V2PinVerifier();

  skip.setOnClickListener(v -> {
    dialog.dismiss();
    mainCallback.onReminderDismissed(callback.hadWrongGuess());
  });

  submit.setEnabled(false);
  submit.setOnClickListener(v -> {
    Editable pinEditable = pinEditText.getText();

    verifier.verifyPin(pinEditable == null ? null : pinEditable.toString(), callback);
  });

  pinEditText.addTextChangedListener(new SimpleTextWatcher() {

    private final String localHash = Objects.requireNonNull(SignalStore.kbsValues().getLocalPinHash());

    @Override
    public void onTextChanged(String text) {
      if (text.length() >= KbsConstants.MINIMUM_PIN_LENGTH) {
        submit.setEnabled(true);

        if (PinHashing.verifyLocalPinHash(localHash, text)) {
          dialog.dismiss();
          mainCallback.onReminderCompleted(text, callback.hadWrongGuess());
        }
      } else {
        submit.setEnabled(false);
      }
    }
  });
}