Drawable Programmatically 하게 만들기 (seekbar background)

나고수·2022년 7월 4일
0

1일1공부

목록 보기
53/68
post-custom-banner

Android Seekbar progressDrawable 을 xml 상에서 말고 Programmatically하게 kotlin 코드로 만들어보기

//.xml로 만들기 

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- seekbar 배경 -->
    <item android:id="@android:id/background">
        <shape android:shape="rectangle">
            <stroke
                android:width="8dp"
                android:color="#00ffffff" />
            <solid android:color="#151B2E" />
            <corners android:radius="12dp" />
        </shape>
    </item>

    <!-- seekbar 프로그래스 -->
    <item android:id="@android:id/progress">
        <clip>
            <shape android:shape="rectangle">
                <stroke
                    android:width="8dp"
                    android:color="#00ffffff" />
                <gradient
                    android:angle="180"
                    android:endColor="#FFD5E4"
                    android:startColor="#E91E63"
                    android:type="linear" />
                <corners android:radius="12dp" />
            </shape>
        </clip>
    </item>

</layer-list>
//kotlin 코드로 Programmatically하게 만들기
   private fun createDrawable(context: Context): Drawable {

        //background drawable
        val gradientDrawable = GradientDrawable(
            GradientDrawable.Orientation.LEFT_RIGHT,
            intArrayOf(Color.parseColor("#E91E63"), Color.parseColor("#FFD5E4"))
        ) //왼쪽에서 오른쪽으로 그라데이션 되는 drawable

        gradientDrawable.gradientType = GradientDrawable.LINEAR_GRADIENT //그라데이션 종류 (직선 그라데이션)
        gradientDrawable.setStroke(8, Color.parseColor("#00ffffff")) //stroke
        gradientDrawable.cornerRadius = 12f //drawable radius
        gradientDrawable.shape = GradientDrawable.RECTANGLE //drawable shape

        //progress drawable
        val clipDrawable = ClipDrawable(
            gradientDrawable, Gravity.LEFT,
            ClipDrawable.HORIZONTAL
        )

        val layerDrawable = LayerDrawable(arrayOf(gradientDrawable,clipDrawable))
        layerDrawable.setId(0, android.R.id.background) //background drawable
        layerDrawable.setId(1, android.R.id.progress) //progress drawable

        return layerDrawable
    }
profile
되고싶다
post-custom-banner

0개의 댓글