Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ShapeableImageView with StrokeWidth, Stroke trimmed on all four side [duplicate]

Tags:

android

Today I just tried out ShapeableImageView from Material design,

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/GuidelineTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.15" />

    <com.google.android.material.imageview.ShapeableImageView
        android:id="@+id/imgProfile"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:scaleType="centerCrop"
        app:strokeColor="@color/grey400"
        app:strokeWidth="@dimen/dp3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:adjustViewBounds="true"
        app:layout_constraintTop_toBottomOf="@+id/GuidelineTop"
        app:shapeAppearanceOverlay="@style/circleImageView" />
</androidx.constraintlayout.widget.ConstraintLayout>

And my circleImageView

<style name="circleImageView" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">50%</item>
</style>

Here is the sample screenshot.

enter image description here

As you see, stroke is cut-off on left, top, right, bottom. Is there anything I missed?

like image 996
Gunaseelan Avatar asked Dec 17 '22 13:12

Gunaseelan


1 Answers

As stroke in android Paint is always drawn centered (half inside, half outside), you can add paddings as wide as half of the stroke width.

<com.google.android.material.imageview.ShapeableImageView
  ...
  app:strokeWidth="@dimen/dp3"
  android:padding="@dimen/dp4"
/>

(where dp4 is considered to be half of dp3)

like image 188
momt99 Avatar answered Dec 21 '22 09:12

momt99