Android - ImageView с закругленным только одним углом - PullRequest
3 голосов
/ 16 октября 2019

Я хочу создать ImageView следующим образом (правая сторона изображения):

enter image description here

У меня есть его в макете CardView, поэтому у меня закругленные углыкарты, но мне нужно создать закругленный левый нижний угол изображения отдельно (или с верхним правым углом).

Я пробовал несколько вариантов, но ничего не работает правильно.

Как я могу это сделать? У вас есть совет?

Ответы [ 3 ]

0 голосов
/ 16 октября 2019

попробуйте это:

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    app:cardCornerRadius="20dp">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_gravity="top|right"
        android:scaleType="fitXY"
        android:src="@drawable/person" />
</androidx.cardview.widget.CardView>

и в вашей деятельности:

 ImageView image = findViewById(R.id.image_view);
        Bitmap bitImg = BitmapFactory.decodeResource(getResources(),
                R.drawable.person);
        image.setImageBitmap(createRoundedRectBitmap(bitImg, 0, 20, 0, 20));
    }


    private static Bitmap createRoundedRectBitmap(@NonNull Bitmap bitmap, float topLeftCorner, float topRightCorner, float bottomRightCorner,
                                                  float bottomLeftCorner) {

        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);


        final int color = Color.WHITE;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        Path path = new Path();
        float[] radii = new float[]{
                topLeftCorner, bottomLeftCorner,
                topRightCorner, topRightCorner,
                bottomRightCorner, bottomRightCorner,
                bottomLeftCorner, bottomLeftCorner
        };

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        path.addRoundRect(rectF, radii, Path.Direction.CW);
        canvas.drawPath(path, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }
0 голосов
/ 16 октября 2019

Вы можете использовать эту библиотеку и поместить свой ImageView в макет

https://github.com/JcMinarro/RoundKornerLayouts

и можете установить радиус для определенных углов, как это

containerLayout.setCornerRadius(2f, CornerType.ALL);
containerLayout.setCornerRadius(2f, CornerType.BOTTOM_LEFT);

Другоевыбор

public final enum class CornerType private constructor() : kotlin.Enum<com.jcminarro.roundkornerlayout.CornerType> {
    ALL,

    TOP_LEFT,

    TOP_RIGHT,

    BOTTOM_RIGHT,

    BOTTOM_LEFT;
}
0 голосов
/ 16 октября 2019

Это просто сделать файл ресурсов для рисования text_logo_fix.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#fff"/>
    <corners android:bottomLeftRadius="30dp" android:topRightRadius="30dp"/>
</shape>

и установить этот файл в свойстве ImageView android:background

<ImageView
            android:id="@+id/imageView"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/herenamedrawableresourcefile"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/text_logo_fix" />

илиВы можете выглядеть так

<LinearLayout
            android:padding="10dp"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/herenamedrawableresourcefile">
            <ImageView
                android:id="@+id/imageView"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/text_logo_fix" />
        </LinearLayout>
...