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

The following examples show how to use android.graphics.drawable.Drawable#getIntrinsicWidth() . 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: FlipLoadingLayout.java    From PullToRefresh-PinnedSection-ListView with MIT License 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 2
Source File: IconButton.java    From IconButton with Apache License 2.0 6 votes vote down vote up
@Override
public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) {
    super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

    if (left != null && right != null) {
        drawableWidth = left.getIntrinsicWidth() + right.getIntrinsicWidth();
        drawablePosition = DrawablePositions.LEFT_AND_RIGHT;
    } else if (left != null) {
        drawableWidth = left.getIntrinsicWidth();
        drawablePosition = DrawablePositions.LEFT;
    } else if (right != null) {
        drawableWidth = right.getIntrinsicWidth();
        drawablePosition = DrawablePositions.RIGHT;
    } else {
        drawablePosition = DrawablePositions.NONE;
    }

    requestLayout();
}
 
Example 3
Source File: CacheUtils.java    From Android-UtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * drawable转bitmap
 *
 * @param drawable drawable对象
 * @return bitmap
 */
private static Bitmap drawable2Bitmap(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, Bitmap.Config.ARGB_8888);
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}
 
Example 4
Source File: PreciselyClipDrawable.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
public PreciselyClipDrawable(Drawable drawable, int offsetX, int offsetY, int width, int height) {
    super(drawable);
    float originWidth = drawable.getIntrinsicWidth();
    float originHeight = drawable.getIntrinsicHeight();

    if (originWidth <= 0 || originHeight <= 0) {
        // Can not clip
        mClip = false;
    } else {
        mClip = true;
        mScale = new RectF();
        mScale.set(MathUtils.clamp(offsetX / originWidth, 0.0f, 1.0f),
                MathUtils.clamp(offsetY / originHeight, 0.0f, 1.0f),
                MathUtils.clamp((offsetX + width) / originWidth, 0.0f, 1.0f),
                MathUtils.clamp((offsetY + height) / originHeight, 0.0f, 1.0f));
        mTemp = new Rect();
    }
}
 
Example 5
Source File: ACache.java    From fangzhuishushenqi with Apache License 2.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 6
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 7
Source File: TouchImageView.java    From Ecommerce-Retronight-Android with Creative Commons Zero v1.0 Universal 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 8
Source File: PolygonImageView.java    From PolygonImageView with Apache License 2.0 5 votes vote down vote up
/**
 * Transforms a drawable into a bitmap.
 *
 * @param drawable incoming drawable
 * @return new bitmap
 */
private static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable == null) {
        return null;
    } else if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }

    //Avoid Color Drawable special case
    int width = drawable.getIntrinsicWidth();
    width = width > 0 ? width : 1;
    int height = drawable.getIntrinsicHeight();
    height = height > 0 ? height : 1;

    Bitmap bitmap;
    try {
        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    } catch (OutOfMemoryError e) {
        Log.e("PolygonImageView", "OutOfMemory during bitmap creation");
        return null;
    }

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}
 
Example 9
Source File: AttachImage.java    From Ruisi with Apache License 2.0 5 votes vote down vote up
public AttachImage(String aid, Drawable drawable, int maxWidth) {
    super(drawable);
    this.aid = aid;
    int width = (int) (drawable.getIntrinsicWidth() * 1.2f);
    int height = (int) (drawable.getIntrinsicHeight() * 1.2f);

    if (width > maxWidth) {
        height = (int) (height * maxWidth * 1.0f / width);
        width = maxWidth;
    }
    drawable.setBounds(0, 0, width, height);
}
 
Example 10
Source File: GridDrawable.java    From ProjectX with Apache License 2.0 5 votes vote down vote up
@Override
public int getIntrinsicWidth() {
    if (mColumnCount <= 0)
        return -1;
    final Drawable drawable = getWrappedDrawable();
    if (drawable == null)
        return -1;
    final int itemWidth = drawable.getIntrinsicWidth();
    return Math.round(mColumnCount * itemWidth + mHorizontalSpacing * (mColumnCount - 1));
}
 
Example 11
Source File: DualButton.java    From DualButton with Apache License 2.0 5 votes vote down vote up
public void setCompoundDrawables(Drawable d,Button btn, int side) {
    int h = d.getIntrinsicHeight();
    int w = d.getIntrinsicWidth();
    d.setBounds(0, 0, w, h);
    btn.setCompoundDrawables(
            side==0?d:btn.getCompoundDrawables()[0],
            side==1?d:btn.getCompoundDrawables()[1],
            side==2?d:btn.getCompoundDrawables()[2],
            side==3?d:btn.getCompoundDrawables()[3]
    );
}
 
Example 12
Source File: IcsLinearLayout.java    From AndroidCacheFoundation with Apache License 2.0 5 votes vote down vote up
public void setDividerDrawable(Drawable divider) {
    if (divider == mDivider) {
        return;
    }
    mDivider = divider;
    if (divider != null) {
        mDividerWidth = divider.getIntrinsicWidth();
        mDividerHeight = divider.getIntrinsicHeight();
    } else {
        mDividerWidth = 0;
        mDividerHeight = 0;
    }
    setWillNotDraw(divider == null);
    requestLayout();
}
 
Example 13
Source File: TouchImageView.java    From social-app-android 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 14
Source File: CheckBox.java    From Carbon with Apache License 2.0 5 votes vote down vote up
@Override
public int getCompoundPaddingRight() {
    int padding = super.getCompoundPaddingRight();
    if (!isButtonOnTheLeft()) {
        final Drawable buttonDrawable = drawable;
        if (buttonDrawable != null) {
            padding += buttonDrawable.getIntrinsicWidth() + drawablePadding;
        }
    }
    return padding;
}
 
Example 15
Source File: PhotoImageView.java    From MoeQuest 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 16
Source File: FitWidthImageView.java    From UILibrary with MIT License 5 votes vote down vote up
@Override
public void setImageResource(int resourceId) {
    free();
    super.setImageResource(resourceId);

    Drawable d = this.getDrawable();

    if (d != null) {
        intrinsicWidth = d.getIntrinsicWidth();
        intrinsicHeight = d.getIntrinsicHeight();
        scaleToFit();
    }
}
 
Example 17
Source File: BusLineView.java    From BusLine with Apache License 2.0 4 votes vote down vote up
/**
 * 设置公交图标
 * @param d Drawable
 */
public void setBusDrawable(Drawable d) {
    mBus = d;
    mBusStationWidth = d.getIntrinsicWidth() << 1;
}
 
Example 18
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
        Rect badgeLocation, boolean tryBadgeInPlace) {
    final int badgedWidth = drawable.getIntrinsicWidth();
    final int badgedHeight = drawable.getIntrinsicHeight();
    final boolean canBadgeInPlace = tryBadgeInPlace
            && (drawable instanceof BitmapDrawable)
            && ((BitmapDrawable) drawable).getBitmap().isMutable();

    final Bitmap bitmap;
    if (canBadgeInPlace) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    } else {
        bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(bitmap);

    if (!canBadgeInPlace) {
        drawable.setBounds(0, 0, badgedWidth, badgedHeight);
        drawable.draw(canvas);
    }

    if (badgeLocation != null) {
        if (badgeLocation.left < 0 || badgeLocation.top < 0
                || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
            throw new IllegalArgumentException("Badge location " + badgeLocation
                    + " not in badged drawable bounds "
                    + new Rect(0, 0, badgedWidth, badgedHeight));
        }
        badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());

        canvas.save();
        canvas.translate(badgeLocation.left, badgeLocation.top);
        badgeDrawable.draw(canvas);
        canvas.restore();
    } else {
        badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
        badgeDrawable.draw(canvas);
    }

    if (!canBadgeInPlace) {
        BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
        }

        return mergedDrawable;
    }

    return drawable;
}
 
Example 19
Source File: ToggleButton.java    From ToggleButtons with MIT License 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
	// If there's text of any sort resort to CompoundButton#onDraw
	if (getText() != null && getText().length() > 0 ||
		getTextOff() != null && getTextOff().length() > 0 ||
		getTextOff() != null && getTextOn().length() > 0) {
		super.onDraw(canvas);
	}
	// Otherwise override CompoundButton#onDraw entirely to allow properly aligned image toggles
	else {
		final Drawable buttonDrawable = CompoundButtonCompat.getButtonDrawable(this);
		if (buttonDrawable != null) {
			final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
			final int horizontalGravity = getGravity() & Gravity.HORIZONTAL_GRAVITY_MASK;
			final int drawableHeight = buttonDrawable.getIntrinsicHeight();
			final int drawableWidth = buttonDrawable.getIntrinsicWidth();

			final int top;
			switch (verticalGravity) {
				case Gravity.BOTTOM:
					top = getHeight() - drawableHeight;
					break;
				case Gravity.CENTER_VERTICAL:
					top = (getHeight() - drawableHeight) / 2;
					break;
				default:
					top = 0;
			}

			final int left;
			switch (horizontalGravity) {
				case Gravity.RIGHT:
				case Gravity.END:
					left = getWidth() - drawableWidth;
					break;
				case Gravity.CENTER_HORIZONTAL:
					left = (getWidth() - drawableWidth) / 2;
					break;
				default:
					left = 0;
			}

			final int bottom = top + drawableHeight;
			final int right = left + drawableWidth;

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

			final Drawable background = getBackground();
			if (Build.VERSION.SDK_INT > 21 && background != null) {
				background.setHotspotBounds(left, top, right, bottom);
			}

			buttonDrawable.draw(canvas);
		}
	}
}
 
Example 20
Source File: ActivityBase.java    From XPrivacy with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
private void buildCheckBoxes() {
	mCheck = new Bitmap[4];

	int userId = Util.getUserId(Process.myUid());
	String themeName = PrivacyManager.getSetting(userId, PrivacyManager.cSettingTheme, "");
	int colorAccent = getResources().getColor(
			themeName.equals("Dark") ? R.color.color_accent_dark : R.color.color_accent_light);

	// Get off check box
	TypedArray ta2 = getTheme().obtainStyledAttributes(new int[] { android.R.attr.listChoiceIndicatorMultiple });
	Drawable off = ta2.getDrawable(0);
	ta2.recycle();
	off.setBounds(0, 0, off.getIntrinsicWidth(), off.getIntrinsicHeight());

	// Get check mark
	Drawable checkmark = getResources().getDrawable(R.drawable.checkmark);
	checkmark.setBounds(0, 0, off.getIntrinsicWidth(), off.getIntrinsicHeight());
	checkmark.setColorFilter(colorAccent, Mode.SRC_ATOP);

	// Get check mark outline
	Drawable checkmarkOutline = getResources().getDrawable(R.drawable.checkmark_outline);
	checkmarkOutline.setBounds(0, 0, off.getIntrinsicWidth(), off.getIntrinsicHeight());

	// Create off check box
	mCheck[0] = Bitmap.createBitmap(off.getIntrinsicWidth(), off.getIntrinsicHeight(), Config.ARGB_8888);
	Canvas canvas0 = new Canvas(mCheck[0]);
	off.draw(canvas0);

	// Create half check box
	mCheck[1] = Bitmap.createBitmap(off.getIntrinsicWidth(), off.getIntrinsicHeight(), Config.ARGB_8888);
	Canvas canvas1 = new Canvas(mCheck[1]);
	off.draw(canvas1);
	Paint paint1 = new Paint();
	paint1.setStyle(Paint.Style.FILL);
	paint1.setColor(colorAccent);
	float wborder = off.getIntrinsicWidth() / 3f;
	float hborder = off.getIntrinsicHeight() / 3f;
	canvas1.drawRect(wborder, hborder, off.getIntrinsicWidth() - wborder, off.getIntrinsicHeight() - hborder,
			paint1);

	// Create full check box
	mCheck[2] = Bitmap.createBitmap(off.getIntrinsicWidth(), off.getIntrinsicHeight(), Config.ARGB_8888);
	Canvas canvas2 = new Canvas(mCheck[2]);
	off.draw(canvas2);
	checkmark.draw(canvas2);
	checkmarkOutline.draw(canvas2);

	// Get question mark
	Drawable questionmark = getResources().getDrawable(R.drawable.ondemand);
	questionmark.setBounds(0, 0, off.getIntrinsicWidth(), off.getIntrinsicHeight());
	questionmark.setColorFilter(colorAccent, Mode.SRC_ATOP);

	// Get question mark outline
	Drawable questionmarkOutline = getResources().getDrawable(R.drawable.questionmark_outline);
	questionmarkOutline.setBounds(0, 0, off.getIntrinsicWidth(), off.getIntrinsicHeight());

	// Create question check box
	mCheck[3] = Bitmap.createBitmap(off.getIntrinsicWidth(), off.getIntrinsicHeight(), Config.ARGB_8888);
	Canvas canvas3 = new Canvas(mCheck[3]);
	off.draw(canvas3);
	questionmark.draw(canvas3);
	questionmarkOutline.draw(canvas3);
}