List of usage examples for android.graphics Rect width
public final int width()
From source file:com.mediatek.galleryfeature.stereo.fancycolor.FancyColorActivity.java
private Point calcClickPosition(int viewW, int viewH, float x, float y) { int thumbW = mThumbBitmapWidth; int thumbH = mThumbBitmapHeight; int gap;//from w w w .ja v a2s . co m int mapX; int mapY; Rect validRange = new Rect(); if (thumbH >= thumbW) { // bitmap height fulfills view height gap = (viewW - viewH * thumbW / thumbH) / 2; validRange.left = gap; validRange.right = viewW - gap; validRange.top = 0; validRange.bottom = viewH; mapX = thumbW * ((int) x - gap) / validRange.width(); mapY = thumbH * (int) y / validRange.height(); } else { // bitmap width fullfills view width gap = (viewH - viewW * thumbH / thumbW) / 2; validRange.left = 0; validRange.right = viewW; validRange.top = gap; validRange.bottom = viewH - gap; mapX = thumbW * (int) x / validRange.width(); mapY = thumbH * ((int) y - gap) / validRange.height(); } MtkLog.d(TAG, "<calcClickPosition> thumbW " + thumbW + ", thumbH " + thumbH + ", gap " + gap + ", rect lrtb " + validRange.left + ", " + validRange.right + ", " + validRange.top + ", " + validRange.bottom + ", mapX " + mapX + ", mapY " + mapY); if (!validRange.contains((int) x, (int) y)) { MtkLog.d(TAG, "<calcClickPosition> invalid click"); return null; } return new Point(mapX, mapY); }
From source file:com.tbse.mywearapplication.WatchFaceDrawer.java
void onDraw(Context context, IWatchFaceConfig config, Canvas canvas, Rect bounds) { final Calendar calendar = config.getCalendar(); final boolean isAmbient = config.isAmbient(); final boolean isRound = config.isRound(); final boolean useLightTheme = !isAmbient && config.isLightTheme(); mBackgroundPaint.setColor(ContextCompat.getColor(context, useLightTheme ? R.color.watchface_background_light : R.color.watchface_background)); ///////////////////////////////////////////////////////////////////// // Draw your watch face here, using the provided canvas and bounds // ///////////////////////////////////////////////////////////////////// final int width = bounds.width(); final int height = bounds.height(); // Find the center. Ignore the window insets so that, on round // watches with a "chin", the watch face is centered on the entire // screen, not just the usable portion. final float centerX = width / 2f; final float centerY = height / 2f; // Draw the background. if (mIsMobilePreview) { if (isRound) { canvas.drawCircle(centerX, centerY, centerX, mPreviewBorderPaint); } else {/*w w w.ja va2 s. com*/ final float radius = mPreviewSquareRadius; final RectF rectF = new RectF(0, 0, canvas.getWidth(), canvas.getHeight()); canvas.drawRoundRect(rectF, radius, radius, mPreviewBorderPaint); } final float translateXY = width * 0.05f; canvas.translate(translateXY, translateXY); canvas.scale(0.9f, 0.9f); if (isRound) { canvas.drawCircle(centerX, centerY, centerX, mBackgroundPaint); } else { canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mBackgroundPaint); } } else { canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mBackgroundPaint); } // Draw weather icon if (image != null) { Matrix matrix = new Matrix(); matrix.setTranslate(50, 90); matrix.preScale(0.2f, 0.2f); canvas.drawBitmap(image, matrix, null); } final float secRot = calendar.get(Calendar.SECOND) / 30f * (float) Math.PI; final int minutes = calendar.get(Calendar.MINUTE); final float minRot = minutes / 30f * (float) Math.PI; final float hrRot = ((calendar.get(Calendar.HOUR) + (minutes / 60f)) / 6f) * (float) Math.PI; final float secLength = centerX - mSecondOuterOffset; final float minLength = centerX - mMinuteOuterOffset; final float hrLength = centerX - mHourOuterOffset; if (!isAmbient) { final float secX = (float) Math.sin(secRot) * secLength; final float secY = (float) -Math.cos(secRot) * secLength; canvas.drawLine(centerX, centerY, centerX + secX, centerY + secY, mSecondHandPaint); } final float minX = (float) Math.sin(minRot) * minLength; final float minY = (float) -Math.cos(minRot) * minLength; canvas.drawLine(centerX, centerY, centerX + minX, centerY + minY, mMinuteHandPaint); final float hrX = (float) Math.sin(hrRot) * hrLength; final float hrY = (float) -Math.cos(hrRot) * hrLength; canvas.drawLine(centerX, centerY, centerX + hrX, centerY + hrY, mHourHandPaint); // Draw weather text canvas.drawText(day, 50, 50, isAmbient ? ambientTextPaint : textPaint); canvas.drawText(low, 50, 65, isAmbient ? ambientTextPaint : textPaint); canvas.drawText(high, 80, 65, isAmbient ? ambientTextPaint : textPaint); canvas.drawText(weatherDescription, 50, 80, isAmbient ? ambientTextPaint : textPaint); }
From source file:com.hezaijin.advance.widgets.view.progress.DiscreteSeekBar.java
private void updateDragging(MotionEvent ev) { setHotspot(ev.getX(), ev.getY());// w w w . ja v a2 s . c o m int x = (int) ev.getX(); Rect oldBounds = mThumb.getBounds(); int halfThumb = oldBounds.width() / 2; int addedThumb = mAddedTouchBounds; int newX = x - mDraggOffset + halfThumb; int left = getPaddingLeft() + halfThumb + addedThumb; int right = getWidth() - (getPaddingRight() + halfThumb + addedThumb); if (newX < left) { newX = left; } else if (newX > right) { newX = right; } int available = right - left; float scale = (float) (newX - left) / (float) available; if (isRtl()) { scale = 1f - scale; } int progress = Math.round((scale * (mMax - mMin)) + mMin); setProgress(progress, true); }
From source file:com.android.utils.traversal.DirectionalTraversalStrategy.java
public AccessibilityNodeInfoCompat findFocus(AccessibilityNodeInfoCompat focused, Rect focusedRect, int direction) { // Using roughly the same algorithm as // frameworks/base/core/java/android/view/FocusFinder.java#findNextFocusInAbsoluteDirection Rect bestCandidateRect = new Rect(focusedRect); switch (direction) { case TraversalStrategy.SEARCH_FOCUS_LEFT: bestCandidateRect.offset(focusedRect.width() + 1, 0); break;//from w w w . ja v a 2 s . c om case TraversalStrategy.SEARCH_FOCUS_RIGHT: bestCandidateRect.offset(-(focusedRect.width() + 1), 0); break; case TraversalStrategy.SEARCH_FOCUS_UP: bestCandidateRect.offset(0, focusedRect.height() + 1); break; case TraversalStrategy.SEARCH_FOCUS_DOWN: bestCandidateRect.offset(0, -(focusedRect.height() + 1)); break; } AccessibilityNodeInfoCompat closest = null; for (AccessibilityNodeInfoCompat focusable : mFocusables) { // Skip the currently-focused view. if (focusable.equals(focused) || focusable.equals(mRoot)) { continue; } Rect otherRect = new Rect(); getAssumedRectInScreen(focusable, otherRect); if (isBetterCandidate(direction, focusedRect, otherRect, bestCandidateRect)) { bestCandidateRect.set(otherRect); closest = focusable; } } if (closest != null) { return AccessibilityNodeInfoCompat.obtain(closest); } return null; }
From source file:net.gsantner.opoc.util.ContextUtils.java
/** * Draw text in the center of the given {@link DrawableRes} * This may be useful for e.g. badge counts *///from w w w .j a va 2 s.c o m public Bitmap drawTextOnDrawable(@DrawableRes int drawableRes, String text, int textSize) { Resources resources = _context.getResources(); float scale = resources.getDisplayMetrics().density; Bitmap bitmap = drawableToBitmap(drawableRes); bitmap = bitmap.copy(bitmap.getConfig(), true); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.rgb(61, 61, 61)); paint.setTextSize((int) (textSize * scale)); paint.setShadowLayer(1f, 0f, 1f, Color.WHITE); Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); int x = (bitmap.getWidth() - bounds.width()) / 2; int y = (bitmap.getHeight() + bounds.height()) / 2; canvas.drawText(text, x, y, paint); return bitmap; }
From source file:com.googlecode.eyesfree.example.bargraph.BarGraphView.java
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { final int heightMode = MeasureSpec.getMode(heightMeasureSpec); final int heightSize = MeasureSpec.getSize(heightMeasureSpec); final int widthMode = MeasureSpec.getMode(widthMeasureSpec); final int widthSize = MeasureSpec.getSize(widthMeasureSpec); int measuredHeight = heightSize; int measuredWidth = widthSize; final Rect contentBounds = mTempRect; getContentBounds(contentBounds);/*from w ww . ja v a 2 s.c o m*/ if (heightMode != MeasureSpec.EXACTLY) { measuredHeight = contentBounds.height(); if (heightMode == MeasureSpec.AT_MOST) { if (measuredHeight > heightSize) { measuredHeight = heightSize; } } } if (widthMode == MeasureSpec.UNSPECIFIED) { measuredWidth = contentBounds.width(); } setMeasuredDimension(measuredWidth, measuredHeight); }
From source file:com.chauthai.overscroll.BouncyAdapter.java
/** * Try to estimate the content size of the adapter inside the RecyclerView. * @return The average size of {@link BouncyConfig#viewCountEstimateSize} views in the adapter. *//* w ww .j a va 2 s . c o m*/ private int estimateContentSize() { int total = 0; int count = 0; for (int i = mAdapter.getItemCount() - 1; i >= 0 && count < mConfig.viewCountEstimateSize; i--) { View view = mLayoutManager.findViewByPosition(i + 1); if (view != null) { Rect rect = new Rect(); mLayoutManager.getDecoratedBoundsWithMargins(view, rect); int itemHeight = Math.abs(directionVertical() ? rect.height() : rect.width()); count++; total += itemHeight; } } if (count > 0) { double average = (double) total / count; return (int) (average * mAdapter.getItemCount()); } return 0; }
From source file:com.facebook.litho.MountState.java
private static int computeRectArea(Rect rect) { return rect.isEmpty() ? 0 : (rect.width() * rect.height()); }
From source file:com.dynamixsoftware.printingsample.IntentApiFragment.java
@Override public void onClick(View v) { switch (v.getId()) { case R.id.check_premium: new AlertDialog.Builder(requireContext()).setTitle(R.string.check_premium) .setMessage("" + intentApi.checkPremium()).setPositiveButton(R.string.ok, null).show(); break;/*from w ww . j a va 2 s . c o m*/ case R.id.activate_online: final Context appContext = requireContext().getApplicationContext(); intentApi.setLicense("YOUR_ACTIVATION_KEY", new ISetLicenseCallback.Stub() { @Override public void start() { toastInMainThread(appContext, "activate start"); } @Override public void serverCheck() { toastInMainThread(appContext, "activate check server"); } @Override public void finish(final Result arg0) { toastInMainThread(appContext, "activate finish " + (arg0 == Result.OK ? "ok" : "error")); } }); break; case R.id.setup_printer: intentApi.setupCurrentPrinter(); break; case R.id.change_options: intentApi.changePrinterOptions(); break; case R.id.get_current_printer: try { IPrinterInfo printer = intentApi.getCurrentPrinter(); Toast.makeText(requireContext().getApplicationContext(), "current printer " + (printer != null ? printer.getName() : "null"), Toast.LENGTH_LONG) .show(); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.print_image: intentApi.print(Uri.parse("file://" + FilesUtils.getFilePath(requireContext(), FilesUtils.FILE_PNG)), "image/png", "from printing sample"); break; case R.id.print_file: intentApi.print(Uri.parse("file://" + FilesUtils.getFilePath(requireContext(), FilesUtils.FILE_DOC)), "application/msword", "from printing sample"); break; case R.id.show_file_preview: intentApi.showFilePreview( Uri.parse("file://" + FilesUtils.getFilePath(requireContext(), FilesUtils.FILE_DOC)), "application/msword", 0); break; case R.id.print_with_your_rendering: try { IDocument.Stub document = new IDocument.Stub() { private int thumbnailWidth; private int thumbnailHeight; @Override public Bitmap renderPageFragment(int arg0, Rect fragment) throws RemoteException { IPrinterInfo printer = intentApi.getCurrentPrinter(); if (printer != null) { Bitmap bitmap = Bitmap.createBitmap(fragment.width(), fragment.height(), Bitmap.Config.ARGB_8888); for (int i = 0; i < 3; i++) try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; options.inDither = false; if (i > 0) { options.inSampleSize = 1 << i; } Bitmap imageBMP = BitmapFactory.decodeStream( new FileInputStream( FilesUtils.getFilePath(requireContext(), FilesUtils.FILE_PNG)), null, options); Paint p = new Paint(); int imageWidth = 0; int imageHeight = 0; if (imageBMP != null) { imageWidth = imageBMP.getWidth(); imageHeight = imageBMP.getHeight(); } int xDpi = printer.getPrinterContext().getHResolution(); int yDpi = printer.getPrinterContext().getVResolution(); // in dots int paperWidth = printer.getPrinterContext().getPaperWidth() * xDpi / 72; int paperHeight = printer.getPrinterContext().getPaperHeight() * yDpi / 72; float aspectH = (float) imageHeight / (float) paperHeight; float aspectW = (float) imageWidth / (float) paperWidth; aspectH = aspectH > 1 ? 1 / aspectH : aspectH; aspectW = aspectW > 1 ? 1 / aspectW : aspectW; RectF dst = new RectF(0, 0, fragment.width() * aspectW, fragment.height() * aspectH); float sLeft = 0; float sTop = fragment.top * aspectH; float sRight = imageWidth; float sBottom = fragment.top * aspectH + fragment.bottom * aspectH; RectF source = new RectF(sLeft, sTop, sRight, sBottom); Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.WHITE); // move image to actual printing area dst.offsetTo(dst.left - fragment.left, dst.top - fragment.top); Matrix matrix = new Matrix(); matrix.setRectToRect(source, dst, Matrix.ScaleToFit.FILL); canvas.drawBitmap(imageBMP, matrix, p); break; } catch (IOException ex) { ex.printStackTrace(); break; } catch (OutOfMemoryError ex) { if (bitmap != null) { bitmap.recycle(); bitmap = null; } continue; } return bitmap; } else { return null; } } @Override public void initDeviceContext(IPrinterContext printerContext, int thumbnailWidth, int thumbnailHeight) { this.thumbnailWidth = thumbnailWidth; this.thumbnailHeight = thumbnailHeight; } @Override public int getTotalPages() { return 1; } @Override public String getDescription() { return "PrintHand test page"; } @Override public Bitmap getPageThumbnail(int arg0) throws RemoteException { Bitmap bitmap = Bitmap.createBitmap(thumbnailWidth, thumbnailHeight, Bitmap.Config.ARGB_8888); for (int i = 0; i < 3; i++) try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; options.inDither = false; if (i > 0) { options.inSampleSize = 1 << i; } Bitmap imageBMP = BitmapFactory.decodeStream( new FileInputStream( FilesUtils.getFilePath(requireContext(), FilesUtils.FILE_PNG)), null, options); Paint p = new Paint(); int imageWidth = 0; int imageHeight = 0; if (imageBMP != null) { imageWidth = imageBMP.getWidth(); imageHeight = imageBMP.getHeight(); } // default int paperWidth = 2481; int paperHeight = 3507; IPrinterInfo printer = intentApi.getCurrentPrinter(); if (printer != null) { int xDpi = printer.getPrinterContext().getHResolution(); int yDpi = printer.getPrinterContext().getVResolution(); // in dots paperWidth = printer.getPrinterContext().getPaperWidth() * xDpi / 72; paperHeight = printer.getPrinterContext().getPaperHeight() * yDpi / 72; } float aspectW = (float) imageWidth / (float) paperWidth; float aspectH = (float) imageHeight / (float) paperHeight; RectF dst = new RectF(0, 0, thumbnailWidth * aspectW, thumbnailHeight * aspectH); float sLeft = 0; float sTop = 0; float sRight = imageWidth; float sBottom = imageHeight; RectF source = new RectF(sLeft, sTop, sRight, sBottom); Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.WHITE); Matrix matrix = new Matrix(); matrix.setRectToRect(source, dst, Matrix.ScaleToFit.FILL); canvas.drawBitmap(imageBMP, matrix, p); break; } catch (IOException ex) { ex.printStackTrace(); break; } catch (OutOfMemoryError ex) { if (bitmap != null) { bitmap.recycle(); bitmap = null; } continue; } return bitmap; } }; intentApi.print(document); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.print_with_your_rendering_without_ui: try { IJob.Stub job = new IJob.Stub() { @Override public Bitmap renderPageFragment(int num, Rect fragment) throws RemoteException { IPrinterInfo printer = intentApi.getCurrentPrinter(); if (printer != null) { Bitmap bitmap = Bitmap.createBitmap(fragment.width(), fragment.height(), Bitmap.Config.ARGB_8888); for (int i = 0; i < 3; i++) try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; options.inDither = false; if (i > 0) { options.inSampleSize = 1 << i; } Bitmap imageBMP = BitmapFactory.decodeStream( new FileInputStream( FilesUtils.getFilePath(requireContext(), FilesUtils.FILE_PNG)), null, options); Paint p = new Paint(); int imageWidth = 0; int imageHeight = 0; if (imageBMP != null) { imageWidth = imageBMP.getWidth(); imageHeight = imageBMP.getHeight(); } int xDpi = printer.getPrinterContext().getHResolution(); int yDpi = printer.getPrinterContext().getVResolution(); // in dots int paperWidth = printer.getPrinterContext().getPaperWidth() * xDpi / 72; int paperHeight = printer.getPrinterContext().getPaperHeight() * yDpi / 72; float aspectH = (float) imageHeight / (float) paperHeight; float aspectW = (float) imageWidth / (float) paperWidth; RectF dst = new RectF(0, 0, fragment.width() * aspectW, fragment.height() * aspectH); float sLeft = 0; float sTop = fragment.top * aspectH; float sRight = imageWidth; float sBottom = fragment.top * aspectH + fragment.bottom * aspectH; RectF source = new RectF(sLeft, sTop, sRight, sBottom); Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.WHITE); // move image to actual printing area dst.offsetTo(dst.left - fragment.left, dst.top - fragment.top); Matrix matrix = new Matrix(); matrix.setRectToRect(source, dst, Matrix.ScaleToFit.FILL); canvas.drawBitmap(imageBMP, matrix, p); break; } catch (IOException ex) { ex.printStackTrace(); break; } catch (OutOfMemoryError ex) { if (bitmap != null) { bitmap.recycle(); bitmap = null; } continue; } return bitmap; } else { return null; } } @Override public int getTotalPages() { return 1; } }; intentApi.print(job, 1); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.print_image_with_print_hand_rendering_without_ui: try { intentApi.print("PrintingSample", "image/png", Uri.parse("file://" + FilesUtils.getFilePath(requireContext(), FilesUtils.FILE_PNG))); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.change_image_options: try { List<PrintHandOption> imageOptions = intentApi.getImagesOptions(); changeRandomOption(imageOptions); intentApi.setImagesOptions(imageOptions); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.print_file_with_print_hand_rendering_without_ui: try { intentApi.print("PrintingSample", "application/ms-word", Uri.parse("file://" + FilesUtils.getFilePath(requireContext(), FilesUtils.FILE_DOC))); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.print_protected_file_with_print_hand_rendering_without_ui: try { intentApi.print("PrintingSample", "application/pdf", Uri.parse("file://" + FilesUtils.getFilePath(requireContext(), FilesUtils.FILE_PDF))); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.change_files_options: try { List<PrintHandOption> fileOptions = intentApi.getFilesOptions(); changeRandomOption(fileOptions); intentApi.setFilesOptions(fileOptions); } catch (RemoteException e) { e.printStackTrace(); } break; } }
From source file:com.astir_trotter.atcustom.ui.iconics.core.IconicsDrawable.java
/** * Update the Padding Bounds//from www .j a va 2s. co m * * @param viewBounds */ private void updatePaddingBounds(Rect viewBounds) { if (mIconPadding >= 0 && !(mIconPadding * 2 > viewBounds.width()) && !(mIconPadding * 2 > viewBounds.height())) { mPaddingBounds.set(viewBounds.left + mIconPadding, viewBounds.top + mIconPadding, viewBounds.right - mIconPadding, viewBounds.bottom - mIconPadding); } }