-1

Topics checked before creating topic

  1. Mask layout with rounded corner xml shape
  2. Mask ImageView with round corner background
  3. How can I make rounded (circled) ImageView box on Android Studio?
  4. ImageView in circular through xml
  5. https://inducesmile.com/android/how-to-make-circular-imageview-and-rounded-corner-imageview-in-android/

I want to apply mask to ImageView like from pic1 to pic2 but it is formed of multiple gradients via .

At first I was using

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners android:radius="36dp" />
</shape>

Then added solid then tried FrameLayout then tried adding library from link 5. None of proposed methods did apply mask. Library was causing errors on

getMenuInflater().inflate(R.menu
if (id == R.id.action_settings

Cannot resolve menu and action_settings.

How come answers are being accepted but I cannot reproduce it? What am I missing in xml files? Even copy-paste whole ImageView + shape did not help

JayJayAbrams
  • 195
  • 1
  • 16
  • Possible duplicate of [How to make an ImageView with rounded corners?](https://stackoverflow.com/questions/2459916/how-to-make-an-imageview-with-rounded-corners) – Bertram Gilfoyle Dec 10 '17 at 11:43
  • no need for any third-party external library: all you need is [RoundedBitmapDrawable](https://developer.android.com/reference/android/support/v4/graphics/drawable/RoundedBitmapDrawable.html) – pskink Dec 10 '17 at 11:50
  • @pskink how do I use it in xml? – JayJayAbrams Dec 10 '17 at 12:12
  • you cannot - you need to setup RoundedBitmapDrawable in java – pskink Dec 10 '17 at 12:13

1 Answers1

0

Final solution via java only (original idea proposed by @pskink)

    ImageView img = (ImageView) findViewById(R.id.img);
    Bitmap src1 = BitmapFactory.decodeResource(getResources(), R.drawable.img_port);
    RoundedBitmapDrawable dr1 = RoundedBitmapDrawableFactory.create(getResources(), src1);
    dr1.setCornerRadius(Math.max(src1.getWidth(), src1.getHeight()) / 10f);
    img.setImageDrawable(dr1);

Another simple solution via xml enter link description here i.e

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="15dp"  />
    <solid/>
</shape>

and ImageView.setClipToOutline(true) in activity. Additional note: that does not work with

JayJayAbrams
  • 195
  • 1
  • 16