your programing

API <21의 드로어 블 틴팅

lovepro 2020. 10. 13. 08:22
반응형

API <21의 드로어 블 틴팅


api <21에 대해 드로어 블 틴팅 작업을 할 수 있습니까?

<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ic_calendar"
    android:tint="@color/primary" />

잘 작동하지만 API21이있는 장치에서만 작동합니다. 하위 API 장치 또는 AppCompat 지원에 대한 해결 방법이 있습니까? 아무것도 찾을 수 없습니다.


다음과 AppCompatImageView같이 사용하십시오 .

<android.support.v7.widget.AppCompatImageView
        android:id="@+id/my_appcompat_imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/my_image"
        android:tint="#636363"
    />

확인 최신이 appcompat-v7앱의의를 build.gradle.

예 : compile 'com.android.support:appcompat-v7:25.0.0'앱의 build.gradle.


소스 코드를 사용하여이를 달성 할 수 있습니다. 이전에는에서 틴팅이 지원되지 않았습니다 DrawableCompat. 지원 라이브러리 22.1부터는 그렇게 할 수 있지만 다음과 같이해야합니다.

Drawable normalDrawable = getResources().getDrawable(R.drawable.drawable_to_tint);
Drawable wrapDrawable = DrawableCompat.wrap(normalDrawable);
DrawableCompat.setTint(wrapDrawable, getResources().getColor(R.color.colorPrimaryLight));

단순히 ImageView를 사용하여 Drawable을 표시 할 수 없습니까? android:tint이전 API 수준에서 잘 작동합니다.

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_calendar"
    android:tint="@color/primary"
    />

여기에도 비슷한 질문이 있습니다 : https://stackoverflow.com/a/26533340/950427

Android Drawable Tinting은 Android 5.0 이상 (API 21 이상)에서만 지원됩니다. ( " At the moment this is limited to coloring the action bar and some widgets."이라고 표시되어 있습니다.)

테마

...

이러한 속성을 설정하면 AppCompat는 해당 값을 API 21+의 프레임 워크 속성에 자동으로 전파합니다. 이렇게하면 상태 표시 줄과 개요 (최근) 작업 항목의 색상이 자동으로 지정됩니다.

이전 플랫폼에서 AppCompat는 가능한 경우 색상 테마를 에뮬레이트합니다. 현재 이것은 작업 표시 줄과 일부 위젯의 색상 지정으로 제한됩니다.

위젯 색조

Android 5.0이 설치된 기기에서 실행할 때 모든 위젯은 방금 언급 한 색상 테마 속성을 사용하여 착색됩니다. Lollipop에서이를 허용하는 두 가지 주요 기능이 있습니다 : 드로어 블 틴팅과 드로어 블에서 테마 속성 (? attr / foo 형식) 참조.

AppCompat는 UI 위젯의 하위 집합에 대해 이전 버전의 Android에서 유사한 동작을 제공합니다.

AppCompat의 툴바에서 제공하는 모든 것 (액션 모드 등) EditText Spinner CheckBox RadioButton Switch (새로운 android.support.v7.widget.SwitchCompat 사용) CheckedTextView 이러한 작업을 수행하기 위해 특별한 작업을 수행 할 필요가 없습니다. 당신의 레이아웃은 평소와 같이 나머지는 AppCompat이 알아서 할 것입니다 (주의 사항이 있습니다. 아래 FAQ 참조).

출처 :

http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html

https://chris.banes.me/2014/10/17/appcompat-v21/


이제 AppCompatImageView, AppCompatButton이 ImageView, Button을 대체하여 API가 낮은 장치에서 색조를 지원합니다. 자세한 내용은 링크를 확인하십시오. AppCompatImageView , AppCompatButton


이미지를 착색하려면 imageView.setColorFilter(int color). 이것은 API 8에서 작동하며 검정색 이미지를 원하는 색상으로 착색하는 데 사용되었습니다. 이것은 대체 할 수 setImageTintList()있지만 사용만으로도 android:tint작동합니다.


이 네임 스페이스
xmlns : app = "http://schemas.android.com/apk/res-auto"사용

그리고 모든 android : tint를 app : tint로 바꿀 수 있습니다. 이것은 나를 위해 문제를 해결합니다.


조금 늦었지만 방법은 다음과 같습니다.

val textInput = EditText(context)

val drawable = ContextCompat.getDrawable(context, R.drawable.your_drawable)
drawable?.let {
    myDrawable -> DrawableCompat.setTint(myDrawable, ContextCompat.getColor(context, R.color.your_color))
    textInput.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, myDrawable, null)
}

이 작업은 원하는대로 수행되며 모든 Android 버전의 지원 라이브러리에서 작동합니다.

@JvmStatic
fun getTintedDrawable(inputDrawable: Drawable, @ColorInt color: Int): Drawable {
    val wrapDrawable = DrawableCompat.wrap(inputDrawable.mutate())
    DrawableCompat.setTint(wrapDrawable, color)
    DrawableCompat.setTintMode(wrapDrawable, Mode.SRC_IN)
    return wrapDrawable
}

누군가 새로운 드로어 블 (tin1, tint2 ..)을 만들고 싶다면 이것을 시도하십시오

<?xml version="1.0" encoding="utf-8"?>
<bitmap
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:src="@drawable/your_image"
  android:tint="@color/tint_color">
   </bitmap>

참고 URL : https://stackoverflow.com/questions/29155463/drawable-tinting-for-api-21

반응형