Я хочу добавить закругленные углы в свой пользовательский просмотр изображений в kotlin - PullRequest
0 голосов
/ 28 февраля 2020

Я хочу добавить закругленные углы к своему пользовательскому представлению изображения в kotlin и создал файл ресурсов для рисования, готовый и не работающий, и не способный выяснить способ использования представления. SetToOutline.plz поможет мне получить закругленные углы

kotlin код

import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.appcompat.widget.AppCompatImageView

class DynamicHeightImageView : AppCompatImageView {
    private var whRatio = 0f

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    fun setRatio(ratio: Float) {
        whRatio = ratio
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        if (whRatio != 0f) {
            val width = measuredWidth
            val height = (whRatio * width).toInt()
            setMeasuredDimension(width, height)
        }
    }

}


xml файл

<RelativeLayout 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">

        <com.georgcantor.wallpaperapp.util.DynamicHeightImageView
            android:id="@+id/pictureImageView"
            android:layout_width="match_parent"
            android:layout_margin="5dp"
            android:background="@drawable/rounded_corner"
            android:layout_height="match_parent"
            android:contentDescription="@string/image_item"
            android:foreground="?android:attr/selectableItemBackground"
            android:scaleType="fitXY" />

</RelativeLayout>

отрисовка файл

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners
        android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp" />

    <stroke
        android:width="1dp"
        android:color="@color/off_white" />

    <solid android:color="@color/colorDetailPrimaryDark" />

</shape>```

Ответы [ 2 ]

0 голосов
/ 28 февраля 2020

Также вы можете сделать это с помощью библиотеки загрузчиков изображений Glide

fun loadImage (imageUrl:String, myImageView:ImageView) {
   val options:RequestOptions = RequestOptions().transform(RoundedCorners(10))
   Glide.with(context)
    .load(imageUrl)
    .apply(requestOptions)
    .into(myImageView)
}
0 голосов
/ 28 февраля 2020

Пожалуйста, не изобретайте велосипед заново. Есть красивая библиотека .. https://github.com/vinc3m1/RoundedImageView

вы можете использовать это

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...