Drawable draw - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Drawable draw method.

This code example shows how to use the following methods:draw, getBounds, getIntrinsicHeight, getIntrinsicWidth, setBounds
            Bitmap bitmap = bitmapDrawable.getBitmap();
            if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
                bitmapDrawable.setTargetDensity(mDisplayMetrics);
            } 
        } 
        int sourceWidth = icon.getIntrinsicWidth();
        int sourceHeight = icon.getIntrinsicHeight();
 
        if (sourceWidth > 0 && sourceHeight > 0) {
            // There are intrinsic sizes. 
            if (width < sourceWidth || height < sourceHeight) {
                // It's too big, scale it down. 
                final float ratio = (float) sourceWidth / sourceHeight;
                if (sourceWidth > sourceHeight) {
                    height = (int) (width / ratio);
                } else if (sourceHeight > sourceWidth) {
                    width = (int) (height * ratio);
                } 
            } else if (sourceWidth < width && sourceHeight < height) {
                // It's small, use the size they gave us. 

9
CodeRank
This code example shows how to use the following methods:draw, setBounds
            drawVerticalDivider(canvas, right);
        } 
    } 
 
    private void drawHorizontalDivider(Canvas canvas, int top) {
        mDivider.setBounds(getPaddingLeft() + mDividerPadding, top,
                getWidth() - getPaddingRight() - mDividerPadding, top + mDividerHeight);
        mDivider.draw(canvas);
    } 
 
    private void drawVerticalDivider(Canvas canvas, int left) {
        mDivider.setBounds(left, getPaddingTop() + mDividerPadding,
                left + mDividerWidth, getHeight() - getPaddingBottom() - mDividerPadding);
        mDivider.draw(canvas);
    } 
 
    private boolean hasDividerBeforeChildAt(int childIndex) {
        if (childIndex == 0 || childIndex == getChildCount()) {
          return false; 
        } 
9
CodeRank
This code example shows how to use the following methods:draw, setAlpha, setBounds
 
	@Override 
	public void draw(Canvas canvas)
	{ 
		if (background != null) {
			background.setBounds((int) innerRect.left, (int) innerRect.top, (int) innerRect.right, (int) innerRect.bottom);
		} 
 
		canvas.save();
 
		if (background != null) {
			if (alpha > NOT_SET) {
				background.setAlpha(alpha);
			} 
			background.draw(canvas);
		} 
 
		canvas.restore();
	} 
 
8
CodeRank
This code example shows how to use the following methods:draw, getIntrinsicHeight, getIntrinsicWidth, setBounds, setState
	@Override 
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
 
		if (image != null) {
			image.setState(getDrawableState());
 
			// scale image to fit inside button 
 
			int imgHeight = image.getIntrinsicHeight();
			int imgWidth = image.getIntrinsicWidth();
			int btnWidth = getWidth();
			int btnHeight = getHeight();
			float scale;
 
			if (imgWidth <= btnWidth && imgHeight <= btnHeight) {
                scale = 1.0f;
            } else { 
                scale = Math.min((float) btnWidth / (float) imgWidth,
                        (float) btnHeight / (float) imgHeight);

8
CodeRank
This code example shows how to use the following methods:draw, getBounds, getIntrinsicHeight, getIntrinsicWidth, getOpacity
            Bitmap bitmap = bitmapDrawable.getBitmap();
            if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
                bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
            } 
        } 
        int iconWidth = icon.getIntrinsicWidth();
        int iconHeight = icon.getIntrinsicHeight();
 
        if (width > 0 && height > 0) {
            if (width < iconWidth || height < iconHeight || scale != 1.0f) {
                final float ratio = (float) iconWidth / iconHeight;
 
                if (iconWidth > iconHeight) {
                    height = (int) (width / ratio);
                } else if (iconHeight > iconWidth) {
                    width = (int) (height * ratio);
                } 
 
                final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ?
                            Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;

8
CodeRank
This code example shows how to use the following methods:draw, getIntrinsicHeight, getIntrinsicWidth, setBounds
            final int h = getHeight();
 
            frameBounds.set(0, 0, w, h);
            mFrame.setBounds(frameBounds);
            if (mOverlay != null) {
                mOverlay.setBounds(w - mOverlay.getIntrinsicWidth(),
                        h - mOverlay.getIntrinsicHeight(), w, h);
            } 
        } 
 
        mFrame.draw(canvas);
        if (mOverlay != null) {
            mOverlay.draw(canvas);
        } 
    } 
 
 
    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
8
CodeRank
This code example shows how to use the following methods:restore, restoreToCount, save, scale, translate
8
CodeRank
This code example shows how to use the following methods:draw, getIntrinsicHeight, getIntrinsicWidth, getOpacity, setBounds
     * 将Drawable转化为Bitmap 
     * @param drawable 
     * @return 
     */ 
    public static Bitmap drawableToBitmap(Drawable drawable) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
                .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);
        return bitmap;
 
    } 
 
    /** 
     * 获得圆角图片的方法 
     * @param bitmap 
8
CodeRank
This code example shows how to use the following methods:draw, setBounds
This code example shows how to use the following methods:draw, getIntrinsicHeight, setBounds
                    childRect.bottom);
            mShadowRight.draw(canvas);
        } 
 
        if ((mEdgeFlag & EDGE_BOTTOM) != 0) {
            mShadowBottom.setBounds(childRect.left, childRect.bottom, childRect.right,
                    childRect.bottom + mShadowBottom.getIntrinsicHeight());
            mShadowBottom.draw(canvas);
        } 
    } 
 
    /** 
     * Set up contentView which will be moved by user gesture 
     *  
     * @param view 
     */ 
    private void setContentView(final View view) {
        mContentView = view;
    } 
 
8
CodeRank

Related Drawable draw Questions & Answers:

draw Method Overview
Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter).
See Code Examples for other Android Drawable Methods: