How do you use ScaleDrawable?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

How do you use ScaleDrawable?

Postby androidian » Sun Dec 28, 2008 3:56 pm

Hi,

Can someone tell me how to use the ScaleDrawable class? In particular, instantiating it. There is only one constructor:

public ScaleDrawable(Drawable drawable, int gravity, float scaleWidth, float scaleHeight)

...but it has no supporting javadoc documentation. What is the gravity constant I must pass? I guess I want the drawable which is contained in the ScaleDrawable to appear pretty much to the left of the canvas, with perhaps a bit of padding. What should the scaleWidth and scaleHeight values be? I know from the description of the class that this is the right one for my purpose (to display a static, non-moving progress-bar-like image) but I can't work out how to do this. Let's say the view that contains this image is 150px x 30px and i want the progress to show 1/3, how do I use ScaleDrawable to do this?

cheers,
Ian
androidian
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sat Oct 25, 2008 3:51 pm

Top

Postby androidian » Tue Dec 30, 2008 12:59 am

Anyone?
androidian
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sat Oct 25, 2008 3:51 pm

Re: How do you use ScaleDrawable?

Postby Sparkletron » Thu Jan 08, 2009 4:58 pm

androidian wrote:I know from the description of the class that this is the right one for my purpose (to display a static, non-moving progress-bar-like image)


That doesn't sound like ScaleDrawable to me. ScaleDrawable is used to, um, scale drawables--to change the size of a drawable. And once you know that, all those attributes that you're puzzling about become quite clear.

Have you considered using a ProgressBar to display a "static, non-moving progress-bar-like image" ?

-S
Sparkletron
Developer
Developer
 
Posts: 26
Joined: Tue Nov 04, 2008 6:17 am

Re: How do you use ScaleDrawable?

Postby androidian » Fri Jan 09, 2009 12:57 am

Thanks for the reply.


Sparkletron wrote:That doesn't sound like ScaleDrawable to me.


From the two line description of ScaleDrawable: " Most often used to implement things like progress bars." That's why I was interested in it. The ProgressBar widget is definitely overkill for my purposes, as the values never change (the image never updates) and spawning threads for what is essentially a pre-renderable image is not good.

Anyway, in the end I was probably overthinking this. I ended up just using a ShapeDrawable and modifying its bounds to get the desired effect. Hence, my onDraw method of my custom View looks like this (assuming that percentage is the relative width of the progress bar I wish to display):

protected void onDraw(Canvas canvas) {

LayoutParams layoutParams = getLayoutParams();
maxHeight = layoutParams.height - (2 * padding);
maxWidth = layoutParams.width - (2 * padding);

width = StrictMath.round(percentage * maxWidth / 100);

mRect = new Rect(padding, padding, padding + width, padding + maxHeight);
mShapeDrawable.setBounds(mRect);

canvas.save();
mShapeDrawable.draw(canvas);
canvas.restore();
}

cheers,
Ian
androidian
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sat Oct 25, 2008 3:51 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests