티스토리 뷰

Android/Kotlin

[Kotlin]  ImageView  타원화

혀가 길지 않은 개발자 2020. 8. 4. 18:21

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:background="#FFFFFF"
    tools:context=".MainActivity">
    
    <ImageView
        android:id="@+id/ivProfile"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/iv_profile"/>

</LinearLayout>

activity_main.xml

 

 

 

 

 

MainActivity.kt

package com.jwsoft.kotlinproject

import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.shapes.OvalShape
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        ivProfile.background = ShapeDrawable(OvalShape())
        if (Build.VERSION.SDK_INT >= 21) {
            ivProfile.clipToOutline = true  // 모양에 맞게 이미지 출력
        }
    }

}

실행 결과

 

 

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함