Jared
Jared

Reputation: 4983

Android: Perform actions on a ShapeDrawable within an ImageView?

I have created a ShapeDrawable and placed it within an imageview that is dynamically created within my layout. Here's the code I use to assign the drawable to my ImageView. Notice that I call on a method to set a random color, I also set the height and width using user filled text fields, but that's irrelevant here.

ShapeDrawable shape;

public void makeShape(){

    shape = new ShapeDrawable(new RectShape());
    shape.setIntrinsicHeight(Integer.parseInt(ET_Height.getText().toString()));
    shape.setIntrinsicWidth(Integer.parseInt(ET_Width.getText().toString()));
    shape.getPaint().setColor(chooseColor());
    iView.setImageDrawable(shape);
}

This part of the code works wonderfully, it's when I later try and access the Drawable to change it's color on a user click, utilizing the same color method from before.

public void onClick(View v){

    iView.getDrawable().getPaint().setColor(chooseColor());
}

Obviously this code doesn't work, but it's the closest I have come to being able to complete it. I can't access the drawable via "shape" either, and I don't understand why.

So what's the best way to gain access to the drawable to change it's color, alpha, rotation, etc in the onClick method?

Thanks!

Upvotes: 1

Views: 677

Answers (2)

Fritz Buehler
Fritz Buehler

Reputation: 36

I have used Abhinav's solution to this same problem and it worked for me Basicly useing the same parameters I Used to create the shape then just created the same shape again and set color and bounds to the existing shape

Upvotes: 0

Abhinav
Abhinav

Reputation: 39904

Try iView.setImageDrawable(shape) again after changing the shape parameters.

Upvotes: 1

Related Questions