Java Code Examples for org.telegram.messenger.AndroidUtilities#dp()

The following examples show how to use org.telegram.messenger.AndroidUtilities#dp() . 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: FilterTabsView.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
public int getWidth(boolean store) {
    int width = titleWidth = (int) Math.ceil(textPaint.measureText(title));
    int c;
    if (store) {
        c = delegate.getTabCounter(id);
        if (c < 0) {
            c = 0;
        }
        if (store) {
            counter = c;
        }
    } else {
        c = counter;
    }
    if (c > 0) {
        String counterText = String.format("%d", c);
        int counterWidth = (int) Math.ceil(textCounterPaint.measureText(counterText));
        int countWidth = Math.max(AndroidUtilities.dp(10), counterWidth) + AndroidUtilities.dp(10);
        width += countWidth + AndroidUtilities.dp(6);
    }
    return Math.max(AndroidUtilities.dp(40), width);
}
 
Example 2
Source File: FilterTabsView.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (!tabs.isEmpty()) {
        int width = MeasureSpec.getSize(widthMeasureSpec) - AndroidUtilities.dp(7) - AndroidUtilities.dp(7);
        Tab firstTab = tabs.get(0);
        firstTab.setTitle(LocaleController.getString("FilterAllChats", R.string.FilterAllChats));
        int tabWith = firstTab.getWidth(false);
        firstTab.setTitle(allTabsWidth > width ? LocaleController.getString("FilterAllChatsShort", R.string.FilterAllChatsShort) : LocaleController.getString("FilterAllChats", R.string.FilterAllChats));
        int trueTabsWidth = allTabsWidth - tabWith;
        trueTabsWidth += firstTab.getWidth(false);
        int prevWidth = additionalTabWidth;
        additionalTabWidth = trueTabsWidth < width ? (width - trueTabsWidth) / tabs.size() : 0;
        if (prevWidth != additionalTabWidth) {
            ignoreLayout = true;
            adapter.notifyDataSetChanged();
            ignoreLayout = false;
        }
        updateTabsWidths();
        invalidated = false;
    }
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
 
Example 3
Source File: BotKeyboardView.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
public void setPanelHeight(int height) {
    panelHeight = height;
    if (isFullSize && botButtons != null && botButtons.rows.size() != 0) {
        buttonHeight = !isFullSize ? 42 : (int) Math.max(42, (panelHeight - AndroidUtilities.dp(30) - (botButtons.rows.size() - 1) * AndroidUtilities.dp(10)) / botButtons.rows.size() / AndroidUtilities.density);
        int count = container.getChildCount();
        int newHeight = AndroidUtilities.dp(buttonHeight);
        for (int a = 0; a < count; a++) {
            View v = container.getChildAt(a);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) v.getLayoutParams();
            if (layoutParams.height != newHeight) {
                layoutParams.height = newHeight;
                v.setLayoutParams(layoutParams);
            }
        }
    }
}
 
Example 4
Source File: PullForegroundDrawable.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private void updatePath() {
    int h = AndroidUtilities.dp(18);
    path.reset();
    path.moveTo(h >> 1, AndroidUtilities.dpf2(4.98f));
    path.lineTo(AndroidUtilities.dpf2(4.95f), AndroidUtilities.dpf2(9f));
    path.lineTo(h - AndroidUtilities.dpf2(4.95f), AndroidUtilities.dpf2(9f));
    path.lineTo(h >> 1, AndroidUtilities.dpf2(4.98f));

    paint.setStyle(Paint.Style.FILL_AND_STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(AndroidUtilities.dpf2(1f));
    lastDensity = AndroidUtilities.density;
}
 
Example 5
Source File: TextCell.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = AndroidUtilities.dp(48);

    valueTextView.measure(MeasureSpec.makeMeasureSpec(width - AndroidUtilities.dp(leftPadding), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(20), MeasureSpec.EXACTLY));
    textView.measure(MeasureSpec.makeMeasureSpec(width - AndroidUtilities.dp(71 + leftPadding) - valueTextView.getTextWidth(), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(20), MeasureSpec.EXACTLY));
    if (imageView.getVisibility() == VISIBLE) {
        imageView.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST));
    }
    if (valueImageView.getVisibility() == VISIBLE) {
        valueImageView.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST));
    }
    setMeasuredDimension(width, AndroidUtilities.dp(50) + (needDivider ? 1 : 0));
}
 
Example 6
Source File: CheckBoxCell.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), AndroidUtilities.dp(48) + (needDivider ? 1 : 0));

    int availableWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - AndroidUtilities.dp(34);

    valueTextView.measure(MeasureSpec.makeMeasureSpec(availableWidth / 2, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
    textView.measure(MeasureSpec.makeMeasureSpec(availableWidth - valueTextView.getMeasuredWidth() - AndroidUtilities.dp(8), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
    checkBox.measure(MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(18), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(18), MeasureSpec.EXACTLY));
}
 
Example 7
Source File: BotHelpCell.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    int x = (getWidth() - width) / 2;
    int y = AndroidUtilities.dp(2);
    Drawable shadowDrawable = Theme.chat_msgInMediaDrawable.getShadowDrawable();
    if (shadowDrawable != null) {
        shadowDrawable.setBounds(x, y, width + x, height + y);
        shadowDrawable.draw(canvas);
    }
    int h = AndroidUtilities.displaySize.y;
    if (getParent() instanceof View) {
        View view = (View) getParent();
        h = view.getMeasuredHeight();
    }
    Theme.chat_msgInMediaDrawable.setTop((int) getY(), h, false, false);
    Theme.chat_msgInMediaDrawable.setBounds(x, y, width + x, height + y);
    Theme.chat_msgInMediaDrawable.draw(canvas);
    Theme.chat_msgTextPaint.setColor(Theme.getColor(Theme.key_chat_messageTextIn));
    Theme.chat_msgTextPaint.linkColor = Theme.getColor(Theme.key_chat_messageLinkIn);
    canvas.save();
    canvas.translate(textX = AndroidUtilities.dp(2 + 9) + x, textY = AndroidUtilities.dp(2 + 9) + y);
    if (pressedLink != null) {
        canvas.drawPath(urlPath, Theme.chat_urlPaint);
    }
    if (textLayout != null) {
        textLayout.draw(canvas);
    }
    canvas.restore();
    wasDraw = true;
}
 
Example 8
Source File: LayoutHelper.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
public static ScrollView.LayoutParams createScroll(int width, int height, int gravity, float leftMargin, float topMargin, float rightMargin, float bottomMargin) {
    ScrollView.LayoutParams layoutParams = new ScrollView.LayoutParams(getSize(width), getSize(height), gravity);
    layoutParams.leftMargin = AndroidUtilities.dp(leftMargin);
    layoutParams.topMargin = AndroidUtilities.dp(topMargin);
    layoutParams.rightMargin = AndroidUtilities.dp(rightMargin);
    layoutParams.bottomMargin = AndroidUtilities.dp(bottomMargin);
    return layoutParams;
}
 
Example 9
Source File: WebPlayerView.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    int x = ((r - l) - aspectRatioFrameLayout.getMeasuredWidth()) / 2;
    int y = ((b - t - AndroidUtilities.dp(10)) - aspectRatioFrameLayout.getMeasuredHeight()) / 2;
    aspectRatioFrameLayout.layout(x, y, x + aspectRatioFrameLayout.getMeasuredWidth(), y + aspectRatioFrameLayout.getMeasuredHeight());
    if (controlsView.getParent() == this) {
        controlsView.layout(0, 0, controlsView.getMeasuredWidth(), controlsView.getMeasuredHeight());
    }
    x = ((r - l) - progressView.getMeasuredWidth()) / 2;
    y = ((b - t) - progressView.getMeasuredHeight()) / 2;
    progressView.layout(x, y, x + progressView.getMeasuredWidth(), y + progressView.getMeasuredHeight());
    controlsView.imageReceiver.setImageCoords(0, 0, getMeasuredWidth(), getMeasuredHeight() - AndroidUtilities.dp(10));
}
 
Example 10
Source File: PullForegroundDrawable.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
public static int getMaxOverscroll() {
    return AndroidUtilities.dp(72);
}
 
Example 11
Source File: FeaturedStickerSetInfoCell.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int getIntrinsicWidth() {
    return AndroidUtilities.dp(12);
}
 
Example 12
Source File: MediaActionDrawable.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int getMinimumWidth() {
    return AndroidUtilities.dp(48);
}
 
Example 13
Source File: BotHelpCell.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
public void setText(String text) {
    if (text == null || text.length() == 0) {
        setVisibility(GONE);
        return;
    }
    if (text != null && oldText != null && text.equals(oldText)) {
        return;
    }
    oldText = text;
    setVisibility(VISIBLE);
    int maxWidth;
    if (AndroidUtilities.isTablet()) {
        maxWidth = (int) (AndroidUtilities.getMinTabletSide() * 0.7f);
    } else {
        maxWidth = (int) (Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) * 0.7f);
    }
    String lines[] = text.split("\n");
    SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
    String help = LocaleController.getString("BotInfoTitle", R.string.BotInfoTitle);
    stringBuilder.append(help);
    stringBuilder.append("\n\n");
    for (int a = 0; a < lines.length; a++) {
        stringBuilder.append(lines[a].trim());
        if (a != lines.length - 1) {
            stringBuilder.append("\n");
        }
    }
    MessageObject.addLinks(false, stringBuilder);
    stringBuilder.setSpan(new TypefaceSpan(AndroidUtilities.getTypeface("fonts/rmedium.ttf")), 0, help.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    Emoji.replaceEmoji(stringBuilder, Theme.chat_msgTextPaint.getFontMetricsInt(), AndroidUtilities.dp(20), false);
    try {
        textLayout = new StaticLayout(stringBuilder, Theme.chat_msgTextPaint, maxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
        width = 0;
        height = textLayout.getHeight() + AndroidUtilities.dp(4 + 18);
        int count = textLayout.getLineCount();
        for (int a = 0; a < count; a++) {
            width = (int) Math.ceil(Math.max(width, textLayout.getLineWidth(a) + textLayout.getLineLeft(a)));
        }
        if (width > maxWidth) {
            width = maxWidth;
        }
    } catch (Exception e) {
        FileLog.e(e);
    }
    width += AndroidUtilities.dp(4 + 18);
}
 
Example 14
Source File: VideoTimelineView.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    int width = getMeasuredWidth() - AndroidUtilities.dp(36);
    int startX = (int) (width * progressLeft) + AndroidUtilities.dp(16);
    int endX = (int) (width * progressRight) + AndroidUtilities.dp(16);

    canvas.save();
    canvas.clipRect(AndroidUtilities.dp(16), 0, width + AndroidUtilities.dp(20), getMeasuredHeight());
    if (frames.isEmpty() && currentTask == null) {
        reloadFrames(0);
    } else {
        int offset = 0;
        for (int a = 0; a < frames.size(); a++) {
            Bitmap bitmap = frames.get(a);
            if (bitmap != null) {
                int x = AndroidUtilities.dp(16) + offset * (isRoundFrames ? frameWidth / 2 : frameWidth);
                int y = AndroidUtilities.dp(2);
                if (isRoundFrames) {
                    rect2.set(x, y, x + AndroidUtilities.dp(28), y + AndroidUtilities.dp(28));
                    canvas.drawBitmap(bitmap, rect1, rect2, null);
                } else {
                    canvas.drawBitmap(bitmap, x, y, null);
                }
            }
            offset++;
        }
    }

    int top = AndroidUtilities.dp(2);

    canvas.drawRect(AndroidUtilities.dp(16), top, startX, getMeasuredHeight() - top, paint2);
    canvas.drawRect(endX + AndroidUtilities.dp(4), top, AndroidUtilities.dp(16) + width + AndroidUtilities.dp(4), getMeasuredHeight() - top, paint2);

    canvas.drawRect(startX, 0, startX + AndroidUtilities.dp(2), getMeasuredHeight(), paint);
    canvas.drawRect(endX + AndroidUtilities.dp(2), 0, endX + AndroidUtilities.dp(4), getMeasuredHeight(), paint);
    canvas.drawRect(startX + AndroidUtilities.dp(2), 0, endX + AndroidUtilities.dp(4), top, paint);
    canvas.drawRect(startX + AndroidUtilities.dp(2), getMeasuredHeight() - top, endX + AndroidUtilities.dp(4), getMeasuredHeight(), paint);
    canvas.restore();

    canvas.drawCircle(startX, getMeasuredHeight() / 2, AndroidUtilities.dp(7), paint);
    canvas.drawCircle(endX + AndroidUtilities.dp(4), getMeasuredHeight() / 2, AndroidUtilities.dp(7), paint);
}
 
Example 15
Source File: ScrollSlidingTextTabStrip.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
public void addTextTab(final int id, CharSequence text) {
    int position = tabCount++;
    if (position == 0 && selectedTabId == -1) {
        selectedTabId = id;
    }
    positionToId.put(position, id);
    idToPosition.put(id, position);
    if (selectedTabId != -1 && selectedTabId == id) {
        currentPosition = position;
        prevLayoutWidth = 0;
    }
    TextView tab = new TextView(getContext());
    tab.setGravity(Gravity.CENTER);
    tab.setText(text);
    tab.setBackgroundDrawable(Theme.createSelectorDrawable(Theme.getColor(Theme.key_actionBarDefaultSelector), 2));
    tab.setTag(selectedTabId == id ? Theme.key_actionBarDefaultTitle : Theme.key_actionBarDefaultSubtitle);
    tab.setTextColor(Theme.getColor(currentPosition == position ? Theme.key_actionBarDefaultTitle : Theme.key_actionBarDefaultSubtitle));
    tab.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
    tab.setSingleLine(true);
    tab.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
    tab.setPadding(AndroidUtilities.dp(8), 0, AndroidUtilities.dp(8), 0);
    tab.setOnClickListener(v -> {
        int position1 = tabsContainer.indexOfChild(v);
        if (position1 < 0 || position1 == currentPosition) {
            return;
        }
        boolean scrollingForward = currentPosition < position1;
        previousPosition = currentPosition;
        currentPosition = position1;
        selectedTabId = id;

        if (animatingIndicator) {
            AndroidUtilities.cancelRunOnUIThread(animationRunnable);
            animatingIndicator = false;
        }

        animationTime = 0;
        animatingIndicator = true;
        animateIndicatorStartX = indicatorX;
        animateIndicatorStartWidth = indicatorWidth;

        animateIndicatorToX = v.getLeft();
        animateIndicatorToWidth = v.getMeasuredWidth();
        setEnabled(false);

        AndroidUtilities.runOnUIThread(animationRunnable, 16);

        if (delegate != null) {
            delegate.onPageSelected(id, scrollingForward);
        }
        scrollToChild(position1);
    });
    int tabWidth = (int) Math.ceil(tab.getPaint().measureText(text, 0, text.length())) + AndroidUtilities.dp(16);
    allTextWidth += tabWidth;
    positionToWidth.put(position, tabWidth);
    tabsContainer.addView(tab, LayoutHelper.createLinear(0, LayoutHelper.MATCH_PARENT));
}
 
Example 16
Source File: BotKeyboardView.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
public int getKeyboardHeight() {
    return isFullSize ? panelHeight : botButtons.rows.size() * AndroidUtilities.dp(buttonHeight) + AndroidUtilities.dp(30) + (botButtons.rows.size() - 1) * AndroidUtilities.dp(10);
}
 
Example 17
Source File: VideoTimelinePlayView.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
private void reloadFrames(int frameNum) {
    if (mediaMetadataRetriever == null) {
        return;
    }
    if (frameNum == 0) {
        if (isRoundFrames) {
            frameHeight = frameWidth = AndroidUtilities.dp(56);
            framesToLoad = (int) Math.ceil((getMeasuredWidth() - AndroidUtilities.dp(16)) / (frameHeight / 2.0f));
        } else {
            frameHeight = AndroidUtilities.dp(40);
            framesToLoad = (getMeasuredWidth() - AndroidUtilities.dp(16)) / frameHeight;
            frameWidth = (int) Math.ceil((float) (getMeasuredWidth() - AndroidUtilities.dp(16)) / (float) framesToLoad);
        }
        frameTimeOffset = videoLength / framesToLoad;
    }
    currentTask = new AsyncTask<Integer, Integer, Bitmap>() {
        private int frameNum = 0;

        @Override
        protected Bitmap doInBackground(Integer... objects) {
            frameNum = objects[0];
            Bitmap bitmap = null;
            if (isCancelled()) {
                return null;
            }
            try {
                bitmap = mediaMetadataRetriever.getFrameAtTime(frameTimeOffset * frameNum * 1000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
                if (isCancelled()) {
                    return null;
                }
                if (bitmap != null) {
                    Bitmap result = Bitmap.createBitmap(frameWidth, frameHeight, bitmap.getConfig());
                    Canvas canvas = new Canvas(result);
                    float scaleX = (float) frameWidth / (float) bitmap.getWidth();
                    float scaleY = (float) frameHeight / (float) bitmap.getHeight();
                    float scale = Math.max(scaleX, scaleY);
                    int w = (int) (bitmap.getWidth() * scale);
                    int h = (int) (bitmap.getHeight() * scale);
                    Rect srcRect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
                    Rect destRect = new Rect((frameWidth - w) / 2, (frameHeight - h) / 2, w, h);
                    canvas.drawBitmap(bitmap, srcRect, destRect, null);
                    bitmap.recycle();
                    bitmap = result;
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (!isCancelled()) {
                frames.add(bitmap);
                invalidate();
                if (frameNum < framesToLoad) {
                    reloadFrames(frameNum + 1);
                }
            }
        }
    };
    currentTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, frameNum, null, null);
}
 
Example 18
Source File: BackDrawable.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int getIntrinsicWidth() {
    return AndroidUtilities.dp(24);
}
 
Example 19
Source File: VoIPActivity.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int getIntrinsicHeight() {
    return AndroidUtilities.dp(12);
}
 
Example 20
Source File: RadialProgress.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int getIntrinsicHeight() {
    return AndroidUtilities.dp(48);
}