0

I'm trying to show a certain part of an image.

For example:
70% of Android icon

I tried to set a ClipDrawable of an ImageView from a Bitmap using the code below, but it didn't show up.

BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
ClipDrawable clipDrawable = new ClipDrawable(bitmapDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
clipDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
clipDrawable.setLevel(5000);
imageView.setImageDrawable(clipDrawable);

I also tested to make sure the problem wasn't with the BitmapDrawable by seeing if imageView.setImageDrawable(bitmapDrawable) would show the full image and it did display it correctly.

How can I show a fraction of a Bitmap using ClipDrawable?

Why is the image not showing up?

I looked at this post, but the accepted answer assumes that the image can be retrieved from the resources whereas I'm loading my image dynamically.

The answers to the post android - How to cut some part of image and show it in imageview don't answer my question using ClipDrawable and just use magic numbers to create a new Bitmap.

The Android docs on ClipDrawable only explain how to solve this problem if you set the Drawable in XML.

Community
  • 1
  • 1
lschlessinger
  • 1,934
  • 3
  • 25
  • 47
  • Can't you just modify the `width` and `height` arguments of `setBounds`? I'm not an android dev, but seeing how you pass it into the `ClipDrawable`, which has a `setBounds` method, I would assume so. After reading your post for a second time, are you having problems displaying an image? If so, you should fit your post to match the problem – Vince Jun 27 '15 at 04:03
  • @VinceEmigh Yes, it's not showing up when I call `imageView.setImageDrawable(clipDrawable)`. I'll edit the post. How do you think I should change the arguments of the `setBounds` method? – lschlessinger Jun 27 '15 at 04:14
  • check my answer: http://stackoverflow.com/questions/30395855/using-clipdrawable-to-hide-a-part-of-and-imageview – ovluca Jul 28 '15 at 11:11

1 Answers1

3

It's either you need to call invalidateSelf on the clipdrawable after you set the level or you should try this:

imageView.setBackground(clipDrawable);
imageView.setImageResource(android.R.color.transparent);

Courtesy of: Using ClipDrawable to hide a part of and ImageView

Community
  • 1
  • 1
d0nut
  • 2,835
  • 1
  • 18
  • 23