Other Snippet DrawableToBitmap.

jahswant

Well-Known Member
Licensed User
Longtime User
B4X:
private Bitmap drawableToBitmap (Drawable drawable) {

        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }

        Bitmap bitmap;
        if (mRadius == 0) {
            // Default dot radius size
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        } else {
            // Specify dot radius size
            bitmap = Bitmap.createBitmap(mRadius*2, mRadius*2, Bitmap.Config.ARGB_8888);
        }
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }
 

leitor79

Active Member
Licensed User
Longtime User
Hello jashwani, thank you for sharing!

Where does mRadius came from?

Thank you very much!
 

leitor79

Active Member
Licensed User
Longtime User
Hi, thank you for your answer!

The question I meant to ask was where the value of mRadius came from... it's like it should be a parameter or set somewhere else...

Regards!
 

DonManfred

Expert
Licensed User
Longtime User
The question I meant to ask was where the value of mRadius came from... it's like it should be a parameter or set somewhere else...
Based on the code he posted the mRadius is not set. So his code is partially wrong.

Extend the method to expect a radius as parameter to use it inside the inline java.
B4X:
private Bitmap drawableToBitmap (Drawable drawable, int mRadius) {

        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }

        Bitmap bitmap;
        if (mRadius == 0) {
            // Default dot radius size
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        } else {
            // Specify dot radius size
            bitmap = Bitmap.createBitmap(mRadius*2, mRadius*2, Bitmap.Config.ARGB_8888);
        }
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }
 

jahswant

Well-Known Member
Licensed User
Longtime User
The question I meant to ask was where the value of mRadius came from... it's like it should be a parameter or set somewhere else...
You are right. Take example of this simple dot view where mRaduis is got from styles Attributs.

B4X:
package me.cafecode.lib.dotindicatorlib;
//from   ww  w .  j av a  2  s .co  m
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
* Created by Natthawut Hemathulin on 11/20/14 AD.
* Email: [email protected]
*/
public class DotIndicator extends View {

    private static final String TAG = "DotIndicator";

    /**
     * This class work with custom attributes(attrs.xml).
     */
    private Bitmap mActiveDotBitmap;
    private Bitmap mNormalDotBitmap;

    private Context mContext;
    private Paint mPaint;

    // Attributes
    private int mSize;
    private int mSpace;
    private int mRadius;
    private Drawable mNormalDotDrawable;
    private Drawable mActivedDotDrawable;

    private int mActivedPosition;
    private Canvas canvas;

    // Work with ViewPager
//    private ViewPager mPager;

    public DotIndicator(Context context) {
        super(context);
        mContext = context;
        initUI();
    }

    /**
     *  Important. It minimum to provide a constructor.
     *
     * @param context
     * @param attrs
     */
    public DotIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DotIndicator, 0, 0);

        try {
            mSize = typedArray.getInteger(R.styleable.DotIndicator_size, 0);
            mSpace = typedArray.getDimensionPixelSize(R.styleable.DotIndicator_space, 0);
            mRadius = typedArray.getDimensionPixelSize(R.styleable.DotIndicator_radius, 0);
            mNormalDotDrawable = typedArray.getDrawable(R.styleable.DotIndicator_src_normal);
            mActivedDotDrawable = typedArray.getDrawable(R.styleable.DotIndicator_src_actived);
        } finally {
            typedArray.recycle();
        }
        mContext = context;
        initUI();
    }

    public DotIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initUI();
    }

    private void initUI() {

        mActivedPosition = 0;

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        if (mNormalDotDrawable == null) {
            mNormalDotBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.dot_normal));
        } else {
            mNormalDotBitmap = drawableToBitmap(mNormalDotDrawable);
        }

        if (mActivedDotDrawable == null) {
            mActiveDotBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.dot_actived));
        } else {
            mActiveDotBitmap = drawableToBitmap(mActivedDotDrawable);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Log.i(TAG, "onDraw()");
        super.onDraw(canvas);
        this.canvas = canvas;
        drawDot(canvas);

    }

    int widthMeasureSpec, heightMeasureSpec;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {


        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        Log.i(TAG, "onMeasure()");
        // Background image's size
//        int desiredWidth = getBackground().getIntrinsicWidth();
//        int desiredHeight = getBackground().getIntrinsicHeight();
//
//        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
//        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
//
//        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
//        this.widthMeasureSpec = widthMeasureSpec;
//        this.heightMeasureSpec = heightMeasureSpec;

        /*int width = ((mRadius*2)+mSpace)*mSize-mSpace;
        width = resolveSize(width, widthMeasureSpec);
//        width = MeasureSpec.getSize(widthMeasureSpec);

        int height = mNormalDotBitmap.getHeight();
        height = resolveSize(height, heightMeasureSpec);

        setMeasuredDimension(width, height);*/
        updateMeasuredDimension();
//        setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight());
    }

    private void updateMeasuredDimension() {
        if (mSize < 1) {
            setMeasuredDimension(0, 0);
            return;
        }

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        Log.i(TAG, "WM: "+widthMode+", HM: "+heightMode);

        int width = (mRadius*2+mSpace)*mSize-mSpace;
        width = resolveSizeAndState(width, widthMeasureSpec, 1);
        width = MeasureSpec.getSize(width);

        int height = mNormalDotBitmap.getHeight();
        height = resolveSizeAndState(height, heightMeasureSpec, 0);
        height = MeasureSpec.getSize(height);
        Log.i(TAG, "WF: "+width+", HF: "+height);

        setMeasuredDimension(width, height);
    }

    private void drawDot(Canvas canvas) {
        int axisX = 0;
        for (int i = 0; i < mSize; i++) {
            if (i == mActivedPosition) {
                canvas.drawBitmap(mActiveDotBitmap, axisX, 0, mPaint);
            } else {
                canvas.drawBitmap(mNormalDotBitmap, axisX, 0, mPaint);
            }
            axisX += mNormalDotBitmap.getWidth()+mSpace;
        }
    }

    public int getViewWidth() {
        if (mSpace == 0) {
            return mSize *mNormalDotBitmap.getWidth();
        }
        return mSize *(mNormalDotBitmap.getWidth()+mSpace)-mSpace;
    }

    public int getTotalDots() {
        return mSize;
    }

    public void setTotalDots(int mTotalDots) {
        this.mSize = mTotalDots;
    }

    public int getActivedPosition() {
        return mActivedPosition;
    }

    public void setActivedPosition(int activedPosition) {
        mActivedPosition = activedPosition;
        invalidate();
    }

    public void setPager(ViewPager viewPager) {
        mSize = viewPager.getAdapter().getCount();

        invalidate();

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {
                setActivedPosition(i);
            }

            @Override
            public void onPageSelected(int i) {

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    public void update() {
//        drawDot(canvas);
        invalidate();
    }

    /**
     * Convert drawable to bitmap
     * @param drawable is Drawable
     * @return Bitmap
     */
    private Bitmap drawableToBitmap (Drawable drawable) {

        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }

        Bitmap bitmap;
        if (mRadius == 0) {
            // Default dot radius size
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        } else {
            // Specify dot radius size
            bitmap = Bitmap.createBitmap(mRadius*2, mRadius*2, Bitmap.Config.ARGB_8888);
        }
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }

    public void setViewPager(ViewPager pager) {
        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setActivedPosition(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

}
 

leitor79

Active Member
Licensed User
Longtime User
Thank you both very much for your answer!

DonManfred, besides being a parameter, what I really really wanted to know is what mRadius is. If it's a parameter, its an arbitrary value for determining the width and height of the image? Or is not arbitrary, is should depend on some screen resolution (and how I get it in that case)

jahswani, thank you very much for your full example! but, this line makes me think:

B4X:
mRadius = typedArray.getDimensionPixelSize(R.styleable.DotIndicator_radius, 0);

what is R and where does it came from? (no, I'm not java nor android expert)

Regards!
 
Top