Java Code Examples for android.graphics.drawable.Drawable#getIntrinsicHeight()

The following examples show how to use android.graphics.drawable.Drawable#getIntrinsicHeight() . 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: MatrixDrawable.java    From FanXin-based-HuanXin with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Determines bounds for the underlying drawable and a matrix that should be applied on it.
 */
private void configureBounds() {
  Drawable underlyingDrawable = getCurrent();
  Rect bounds = getBounds();
  int underlyingWidth = mUnderlyingWidth = underlyingDrawable.getIntrinsicWidth();
  int underlyingHeight = mUnderlyingHeight = underlyingDrawable.getIntrinsicHeight();

  // In case underlying drawable doesn't have intrinsic dimensions, we cannot set its bounds to
  // -1 so we use our bounds and discard specified matrix. In normal case we use drawable's
  // intrinsic dimensions for its bounds and apply specified matrix to it.
  if (underlyingWidth <= 0 || underlyingHeight <= 0) {
    underlyingDrawable.setBounds(bounds);
    mDrawMatrix = null;
  } else {
    underlyingDrawable.setBounds(0, 0, underlyingWidth, underlyingHeight);
    mDrawMatrix = mMatrix;
  }
}
 
Example 2
Source File: ConvertUtils.java    From AndroidWallet with GNU General Public License v3.0 6 votes vote down vote up
/**
 * drawable转bitmap
 *
 * @param drawable drawable对象
 * @return bitmap
 */
public static Bitmap drawable2Bitmap(final Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        if (bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }
    Bitmap bitmap;
    if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        bitmap = Bitmap.createBitmap(1, 1,
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
    }
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}
 
Example 3
Source File: Thumbnail.java    From UMS-Interface with GNU General Public License v3.0 6 votes vote down vote up
/**
 * get a bitmap from a drawable
 * */
public static Bitmap drawableToBitamp(Drawable drawable)
{
    Bitmap bitmap;
    int w = drawable.getIntrinsicWidth();
    int h = drawable.getIntrinsicHeight();
    Bitmap.Config config =
            drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                    : Bitmap.Config.RGB_565;
    bitmap = Bitmap.createBitmap(w,h,config);
    //注意,下面三行代码要用到,否在在View或者surfaceview里的canvas.drawBitmap会看不到图
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, w, h);
    drawable.draw(canvas);
    return bitmap;
}
 
Example 4
Source File: FlipLoadingLayout.java    From Roid-Library with Apache License 2.0 6 votes vote down vote up
@Override
protected void onLoadingDrawableSet(Drawable imageDrawable) {
    if (null != imageDrawable) {
        final int dHeight = imageDrawable.getIntrinsicHeight();
        final int dWidth = imageDrawable.getIntrinsicWidth();

        /**
         * We need to set the width/height of the ImageView so that it is
         * square with each side the size of the largest drawable
         * dimension. This is so that it doesn't clip when rotated.
         */
        ViewGroup.LayoutParams lp = mHeaderImage.getLayoutParams();
        lp.width = lp.height = Math.max(dHeight, dWidth);
        mHeaderImage.requestLayout();

        /**
         * We now rotate the Drawable so that is at the correct rotation,
         * and is centered.
         */
        mHeaderImage.setScaleType(ScaleType.MATRIX);
        Matrix matrix = new Matrix();
        matrix.postTranslate((lp.width - dWidth) / 2f, (lp.height - dHeight) / 2f);
        matrix.postRotate(getDrawableRotationAngle(), lp.width / 2f, lp.height / 2f);
        mHeaderImage.setImageMatrix(matrix);
    }
}
 
Example 5
Source File: ScrollableNumberPicker.java    From ScrollableNumberPicker with MIT License 6 votes vote down vote up
private void scaleImageViewDrawable(ImageView view, float scaleFactor) {
    Drawable drawable = view.getDrawable();
    int currentWidth = drawable.getIntrinsicWidth();
    int currentHeight = drawable.getIntrinsicHeight();
    int newWidth = (int) (currentWidth * scaleFactor);
    int newHeight = (int) (currentHeight * scaleFactor);

    if (newWidth < currentWidth && newHeight < currentHeight) {
        int marginWidth = (currentWidth - newWidth) / 2;
        int marginHeight = (currentHeight - newHeight) / 2;

        //setBounds is not working on FireTV, that's why we use a workaround with margins
        LayoutParams params = (LayoutParams) view.getLayoutParams();
        params.width = newWidth;
        params.height = newHeight;
        params.setMargins(marginWidth, marginHeight, marginWidth, marginHeight);
        view.setLayoutParams(params);
    }
}
 
Example 6
Source File: ACache.java    From FanXin-based-HuanXin with GNU General Public License v2.0 6 votes vote down vote up
private static Bitmap drawable2Bitmap(Drawable drawable) {
	if (drawable == null) {
		return null;
	}
	// 取 drawable 的长宽
	int w = drawable.getIntrinsicWidth();
	int h = drawable.getIntrinsicHeight();
	// 取 drawable 的颜色格式
	Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
	// 建立对应 bitmap
	Bitmap bitmap = Bitmap.createBitmap(w, h, config);
	// 建立对应 bitmap 的画布
	Canvas canvas = new Canvas(bitmap);
	drawable.setBounds(0, 0, w, h);
	// 把 drawable 内容画到画布中
	drawable.draw(canvas);
	return bitmap;
}
 
Example 7
Source File: ScaleImageView.java    From umeng_community_android with MIT License 6 votes vote down vote up
private void initialize() {
    this.setScaleType(ScaleType.MATRIX);
    this.mMatrix = new Matrix();
    Drawable d = getDrawable();
    if (d != null) {
        mIntrinsicWidth = d.getIntrinsicWidth();
        mIntrinsicHeight = d.getIntrinsicHeight();
    }
    // 双击的情况。首先放大到图片的最大比例8,再双击则显示原图大小
    mDetector = new GestureDetector(mContext, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            maxZoomTo((int) e.getX(), (int) e.getY());
            cutting();
            return super.onDoubleTap(e);
        }
    });
    mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
    Log.e(VIEW_LOG_TAG, "### 图片宽度 : " + mIntrinsicWidth + ", height : " + mIntrinsicHeight);
}
 
Example 8
Source File: VerticalSeekBar.java    From oneHookLibraryAndroid with Apache License 2.0 5 votes vote down vote up
private void drawThumb(Canvas canvas) {
    Drawable customThumb = getCustomThumb();

    if (customThumb != null) {

        int available = getHeight() - getPaddingTop() - getPaddingBottom();
        final int thumbHeight = customThumb.getIntrinsicHeight();
        available -= thumbHeight;
        // The extra space for the thumb to move on the track
        available += getThumbOffset() * 2;

        int left = (int) (getScale() * available + 0.5f);

        final int top, bottom;
        top = 0;
        bottom = customThumb.getIntrinsicWidth();

        final int right = left + customThumb.getIntrinsicHeight();

        customThumb.setBounds(left, top, right, bottom);

        canvas.save();
        canvas.translate(getPaddingBottom() - getThumbOffset(), getPaddingTop());
        customThumb.draw(canvas);
        canvas.restore();

    }
}
 
Example 9
Source File: TouchImageView.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    Drawable drawable = getDrawable();
    if (drawable == null || drawable.getIntrinsicWidth() == 0 || drawable.getIntrinsicHeight() == 0) {
        setMeasuredDimension(0, 0);
        return;
    }

    int drawableWidth = drawable.getIntrinsicWidth();
    int drawableHeight = drawable.getIntrinsicHeight();
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    viewWidth = setViewSize(widthMode, widthSize, drawableWidth);
    viewHeight = setViewSize(heightMode, heightSize, drawableHeight);

    //
    // Set view dimensions
    //
    setMeasuredDimension(viewWidth, viewHeight);

    //
    // Fit content within view
    //
    fitImageToView();
}
 
Example 10
Source File: DividerHelper.java    From LRecyclerView with Apache License 2.0 5 votes vote down vote up
public static void drawTopAlignItem(Canvas canvas, Drawable drawable, View child, ViewGroup.MarginLayoutParams params) {

        final int left = child.getLeft() - params.leftMargin;
        final int right = child.getRight() + params.rightMargin;
        final int top = child.getTop() - params.topMargin - drawable.getIntrinsicHeight();
        final int bottom = top + drawable.getIntrinsicHeight();

        drawable.setBounds(left, top, right, bottom);
        drawable.draw(canvas);
    }
 
Example 11
Source File: TrashView.java    From FloatingView with Apache License 2.0 5 votes vote down vote up
/**
 * アクション用削除アイコンを設定します。<br/>
 * この画像はフローティング表示が重なった際に大きさが変化します。
 *
 * @param drawable Drawable
 */
void setActionTrashIconImage(Drawable drawable) {
    mActionTrashIconView.setImageDrawable(drawable);
    if (drawable != null) {
        mActionTrashIconBaseWidth = drawable.getIntrinsicWidth();
        mActionTrashIconBaseHeight = drawable.getIntrinsicHeight();
    }
}
 
Example 12
Source File: UEImage.java    From Auie with GNU General Public License v2.0 5 votes vote down vote up
private void drawableToBitmap(Drawable drawable) {
	final int width = drawable.getIntrinsicWidth();
	final int height = drawable.getIntrinsicHeight();
	Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Config.ARGB_8888 : Config.RGB_565);
	Canvas canvas = new Canvas(bitmap);
	drawable.setBounds(0, 0, width, height);
	drawable.draw(canvas);
	this.bitmap = bitmap;
}
 
Example 13
Source File: PhotoView.java    From SprintNBA with Apache License 2.0 5 votes vote down vote up
private boolean hasSize(Drawable d) {
    if ((d.getIntrinsicHeight() <= 0 || d.getIntrinsicWidth() <= 0)
            && (d.getMinimumWidth() <= 0 || d.getMinimumHeight() <= 0)
            && (d.getBounds().width() <= 0 || d.getBounds().height() <= 0)) {
        return false;
    }
    return true;
}
 
Example 14
Source File: PLA_ListView.java    From EverMemo with MIT License 5 votes vote down vote up
/**
 * Sets the drawable that will be drawn between each item in the list. If the drawable does
 * not have an intrinsic height, you should also call {@link #setDividerHeight(int)}
 *
 * @param divider The drawable to use.
 */
public void setDivider(Drawable divider) {
	if (divider != null) {
		mDividerHeight = divider.getIntrinsicHeight();
		mClipDivider = divider instanceof ColorDrawable;
	} else {
		mDividerHeight = 0;
		mClipDivider = false;
	}
	mDivider = divider;
	mDividerIsOpaque = divider == null || divider.getOpacity() == PixelFormat.OPAQUE;
	requestLayoutIfNecessary();
}
 
Example 15
Source File: TouchImageView.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Return the point at the center of the zoomed image. The PointF coordinates range
 * in value between 0 and 1 and the focus point is denoted as a fraction from the left
 * and top of the view. For example, the top left corner of the image would be (0, 0).
 * And the bottom right corner would be (1, 1).
 *
 * @return PointF representing the scroll position of the zoomed image.
 */
public PointF getScrollPosition() {
    Drawable drawable = getDrawable();
    if (drawable == null) {
        return null;
    }
    int drawableWidth = drawable.getIntrinsicWidth();
    int drawableHeight = drawable.getIntrinsicHeight();

    PointF point = transformCoordTouchToBitmap(viewWidth / 2, viewHeight / 2, true);
    point.x /= drawableWidth;
    point.y /= drawableHeight;
    return point;
}
 
Example 16
Source File: TouchImageView.java    From GankGirl with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Return the point at the center of the zoomed image. The PointF coordinates range
 * in value between 0 and 1 and the focus point is denoted as a fraction from the left
 * and top of the view. For example, the top left corner of the image would be (0, 0).
 * And the bottom right corner would be (1, 1).
 *
 * @return PointF representing the scroll position of the zoomed image.
 */
public PointF getScrollPosition() {
    Drawable drawable = getDrawable();
    if (drawable == null) {
        return null;
    }
    int drawableWidth = drawable.getIntrinsicWidth();
    int drawableHeight = drawable.getIntrinsicHeight();

    PointF point = transformCoordTouchToBitmap(viewWidth / 2, viewHeight / 2, true);
    point.x /= drawableWidth;
    point.y /= drawableHeight;
    return point;
}
 
Example 17
Source File: DrawableUtils.java    From BigApp_Discuz_Android with Apache License 2.0 4 votes vote down vote up
/**
     * 缩放Drawable
     *
     * @param drawable
     * @param w
     * @param h
     * @param isConstrain 是否等比缩放
     * @return
     */
    public static Drawable zoomDrawable(Drawable drawable, int w, int h, boolean isConstrain) {

        float scale = 1;

        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap oldbmp = BitmapUtils.drawable2Bitmap(drawable); // drawable 转换成 bitmap
        Matrix matrix = new Matrix();   // 创建操作图片用的 Matrix 对象

        float scaleWidth = 1;
        float scaleHeight = 1;
        if (w > 0) {
            scaleWidth = ((float) MathExtend.divide(w, width));   // 计算缩放比例
        }
        if (h > 0) {
            scaleHeight = ((float) MathExtend.divide(h, height));
        }

        if (!isConstrain)
            matrix.postScale(scaleWidth, scaleHeight);         // 设置缩放比例
        else {
            if (scaleHeight >= 1 && scaleWidth >= 1) {//如果放大,那就取缩放最大的那个
                scale = (scaleHeight > scaleWidth ? scaleHeight : scaleWidth);
            } else if (scaleHeight <= 1 && scaleWidth <= 1) {//如果缩小,那就取缩放最小的那个
                scale = (scaleHeight < scaleWidth ? scaleHeight : scaleWidth);
            } else {//这就是异常情况了 那就取两个之中较小的那个

//                LogUtils.printLog(DrawableUtils.class, "in here!!!!!!!!!!");
                scale = (scaleHeight < scaleWidth ? scaleHeight : scaleWidth);
            }
            matrix.postScale(scale, scale);         // 设置缩放比例
        }
        Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);       // 建立新的 bitmap ,其内容是对原 bitmap 的缩放后的图
        if (!oldbmp.isRecycled()) {
            oldbmp.recycle();
        }


//        LogUtils.printLog(DrawableUtils.class, "newbmp:" + newbmp + " scale:" + scale);
        return new BitmapDrawable(newbmp);       // 把 bitmap 转换成 drawable 并返回

//        return drawable;
    }
 
Example 18
Source File: StickerImageView.java    From PLDroidShortVideo with Apache License 2.0 4 votes vote down vote up
public void setControlDrawable(Drawable drawable) {
    this.mControlDrawable = drawable;
    mControlDrawableWidth = drawable.getIntrinsicWidth();
    mControlDrawableHeight = drawable.getIntrinsicHeight();
    transformDraw();
}
 
Example 19
Source File: ViewPosition.java    From GestureViews with Apache License 2.0 4 votes vote down vote up
/**
 * @param targetView View for which we want to get on-screen location
 * @return true if view position is changed, false otherwise
 */
private boolean init(@NonNull View targetView) {
    // If view is not attached then we can't get it's position
    if (targetView.getWindowToken() == null) {
        return false;
    }

    tmpViewRect.set(view);

    targetView.getLocationOnScreen(tmpLocation);

    view.set(0, 0, targetView.getWidth(), targetView.getHeight());
    view.offset(tmpLocation[0], tmpLocation[1]);

    viewport.set(targetView.getPaddingLeft(),
            targetView.getPaddingTop(),
            targetView.getWidth() - targetView.getPaddingRight(),
            targetView.getHeight() - targetView.getPaddingBottom());
    viewport.offset(tmpLocation[0], tmpLocation[1]);

    boolean isVisible = targetView.getGlobalVisibleRect(visible);
    if (!isVisible) {
        // Assuming we are starting from center of invisible view
        visible.set(view.centerX(), view.centerY(), view.centerX() + 1, view.centerY() + 1);
    }

    if (targetView instanceof ImageView) {
        ImageView imageView = (ImageView) targetView;
        Drawable drawable = imageView.getDrawable();

        if (drawable == null) {
            image.set(viewport);
        } else {
            final int drawableWidth = drawable.getIntrinsicWidth();
            final int drawableHeight = drawable.getIntrinsicHeight();

            // Getting image position within the view
            ImageViewHelper.applyScaleType(imageView.getScaleType(),
                    drawableWidth, drawableHeight, viewport.width(), viewport.height(),
                    imageView.getImageMatrix(), tmpMatrix);

            tmpSrc.set(0f, 0f, drawableWidth, drawableHeight);
            tmpMatrix.mapRect(tmpDst, tmpSrc);

            // Calculating image position on screen
            image.left = viewport.left + (int) tmpDst.left;
            image.top = viewport.top + (int) tmpDst.top;
            image.right = viewport.left + (int) tmpDst.right;
            image.bottom = viewport.top + (int) tmpDst.bottom;
        }
    } else {
        image.set(viewport);
    }

    return !tmpViewRect.equals(view);
}
 
Example 20
Source File: DrawableUtils.java    From Markwon with Apache License 2.0 4 votes vote down vote up
@NonNull
@CheckResult
public static Rect intrinsicBounds(@NonNull Drawable drawable) {
    return new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
}