Ivan Gerken
Ivan Gerken

Reputation: 914

Implementing a custom Drawable the contains another Drawable

I decided to create the look for an ImageButton using Drawables. Those Drawables aren't primitive, so I have to create them in code. The simplified version of the custom Drawable implementation is below.

For now I cannot manage it to get visible, so I expect some stupid newbie error there.

public class PlayButtonDrawable extends Drawable {

    private Drawable _defaultLook;

    public PlayButtonDrawable() {

        _defaultLook = createDefaultLook();
    }

    @Override
    public void draw(Canvas canvas) {
        _defaultLook.draw(canvas);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSPARENT;
    }

    @Override
    public void setAlpha(int alpha) {
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
    }

    private Drawable createDefaultLook() {
        GradientDrawable oval = new GradientDrawable(Orientation.BOTTOM_TOP, 
                new int[]{0xCDFFFFFF, 0xCDCCCCCC});
        oval.setGradientType(GradientDrawable.RADIAL_GRADIENT);
        oval.setShape(GradientDrawable.OVAL);
        oval.setBounds(0, 0, 100, 100);
        oval.setStroke(4, 0xFF4CFF00);
        oval.setGradientRadius(100);
        oval.setGradientCenter(25, 25);

        return oval;
    }
}

This PlayButtonDrawable is added to an activity in onCreate.

_playButton = (ImageButton)findViewById(R.id.playButton);
_playButton.setImageDrawable(new PlayButtonDrawable());
_playButton.setOnClickListener(_playButtonClickListener);
_playButton.invalidate();

Upvotes: 0

Views: 164

Answers (1)

Minsky
Minsky

Reputation: 2146

How did you setup your ImageButton(R.id.playButton) in the layout file? Important values are the layout_width and the layout_height parameter. For instance:

<ImageButton
      android:id="@+id/playButton"                    
      android:layout_width="100dp"
      android:layout_height="100dp"
 />

If you set layout_width and layout_height just to "wrap_content" you will see only some pixels.

Upvotes: 1

Related Questions