Refine search
Point displaySize = new Point(); activity.getWindowManager().getDefaultDisplay().getRealSize(displaySize); Rect windowSize = new Rect(); ctivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(windowSize); int width = displaySize.x - Math.abs(windowSize.width()); int height = displaySize.y - Math.abs(windowSize.height()); return new Point(width, height);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG); paint.setStyle(Paint.Style.FILL); paint.setColor(color); paint.setTextAlign(Paint.Align.CENTER); paint.setTextSize(textSize); Rect bounds = new Rect(); paint.getTextBounds("a", 0, 1, bounds); buffer.drawText(this.myText, canvasWidht >> 1, (canvasHeight + bounds.height()) >> 1, paint); // remember x >> 1 is equivalent to x / 2, but works much much faster
private Rect r = new Rect(); private void drawCenter(Canvas canvas, Paint paint, String text) { canvas.getClipBounds(r); int cHeight = r.height(); int cWidth = r.width(); paint.setTextAlign(Paint.Align.LEFT); paint.getTextBounds(text, 0, text.length(), r); float x = cWidth / 2f - r.width() / 2f - r.left; float y = cHeight / 2f + r.height() / 2f - r.bottom; canvas.drawText(text, x, y, paint); }
/** * calculates the approximate size of a text, depending on a demo text * avoid repeated calls (e.g. inside drawing methods) * * @param paint * @param demoText * @param outputFSize An output variable, modified by the function. */ public static void calcTextSize(Paint paint, String demoText, FSize outputFSize) { Rect r = mCalcTextSizeRect; r.set(0,0,0,0); paint.getTextBounds(demoText, 0, demoText.length(), r); outputFSize.width = r.width(); outputFSize.height = r.height(); }
Rect rectf = new Rect(); <imageView>or<view>.getLocalVisibleRect(rectf); Log.d("WIDTH :", String.valueOf(rectf.width())); Log.d("HEIGHT :", String.valueOf(rectf.height())); Log.d("left :", String.valueOf(rectf.left)); Log.d("right :", String.valueOf(rectf.right)); Log.d("top :", String.valueOf(rectf.top)); Log.d("bottom :", String.valueOf(rectf.bottom));
/** * Fix the given rectangle if it doesn't confirm to aspect ration rule.<br> * Make sure that width and height are equal if 1:1 fixed aspect ratio is requested. */ private static void fixRectForAspectRatio(Rect rect, int aspectRatioX, int aspectRatioY) { if (aspectRatioX == aspectRatioY && rect.width() != rect.height()) { if (rect.height() > rect.width()) { rect.bottom -= rect.height() - rect.width(); } else { rect.right -= rect.width() - rect.height(); } } }
Rect bounds = new Rect(); void drawString(Canvas canvas, Paint paint, String str, int x, int y) { String[] lines = str.split("\n"); int yoff = 0; for (int i = 0; i < lines.length; ++i) { canvas.drawText(lines[i], x, y + yoff, paint); paint.getTextBounds(lines[i], 0, lines[i].length(), bounds); yoff += bounds.height(); } }
Paint paint = new Paint(); Rect bounds = new Rect(); int text_height = 0; int text_width = 0; paint.setTypeface(Typeface.DEFAULT);// your preference here paint.setTextSize(25);// have this the same as your text size String text = "Some random text"; paint.getTextBounds(text, 0, text.length(), bounds); text_height = bounds.height(); text_width = bounds.width();
private void measureTimeText(){ if(!mLocationDirty) return; mPaint.setTextSize(mTextTimeSize); Rect bounds = new Rect(); mPaint.getTextBounds(BASE_TEXT, 0, BASE_TEXT.length(), bounds); mBaseHeight = bounds.height(); mBaseY = (mHeaderRealHeight + mBaseHeight) / 2f; float dividerWidth = mPaint.measureText(TIME_DIVIDER, 0, TIME_DIVIDER.length()); mHourWidth = mPaint.measureText(mHour, 0, mHour.length()); mMinuteWidth = mPaint.measureText(mMinute, 0, mMinute.length()); mDividerX = (mHeaderRealWidth - dividerWidth) / 2f; mHourX = mDividerX - mHourWidth; mMinuteX = mDividerX + dividerWidth; mMiddayX = mMinuteX + mMinuteWidth; mLocationDirty = false; }
private BitmapDrawable createFloatingBitmap(View v) { floatingItemStatingBounds = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom()); floatingItemBounds = new Rect(floatingItemStatingBounds); Bitmap bitmap = Bitmap.createBitmap(floatingItemStatingBounds.width(), floatingItemStatingBounds.height(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); v.draw(canvas); BitmapDrawable retDrawable = new BitmapDrawable(v.getResources(), bitmap); retDrawable.setBounds(floatingItemBounds); return retDrawable; }