Подтвердить что ты не робот

Tint bitmap в списке слоев

Я пытаюсь применить оттенок к растровому изображению внутри < layer-list >

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape  xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
            <solid android:color="@color/grey" />
            <size android:width="45dp" android:height="45dp"/>
        </shape>
    </item>
    <item android:left="5dp" android:right="5dp" android:top="5dp" android:bottom="5dp">
        <bitmap android:src="@drawable/ic_action" android:tint="#FF000000"   />
    </item>
</layer-list>

Предварительный просмотр показывает, что он должен работать, FROM ANDROID-STUDIO: enter image description here

Но при развертывании на устройстве оно не окрашивается: enter image description here

Он корректно оттеняет, если я использую ImageView в своем макете, но не с помощью списка слоев. Я считаю, что я пробовал каждый tintMode без каких-либо результатов.

4b9b3361

Ответ 1

Для кого-то еще, кто может столкнуться с этим вопросом. Это то, что я сделал.

Настройка

  • Android Studio 2.3.2
  • Windows 10

    Gradle

  • minSdkVersion 15

  • targetSdkVersion 25
  • compile 'com.android.support:appcompat-v7:25.3.1'

Устройства тестирования

  • Asus Padfone X Android v4.4.2
  • Samsung Galaxy S3 Android v6.0
  • Samsung Galaxy S7 Android v7.0

Layer Drawable Я добавил android:[email protected]+id/bitmapID к элементу, содержащему растровое изображение

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="oval">
            <solid android:color="#9e9e9e" />
            <size android:width="45dp" android:height="45dp"/>
        </shape>
    </item>
    <item
        android:id="@+id/tintDrawableImg"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp"
        android:bottom="5dp">
        <bitmap android:src="@mipmap/ic_pencil" android:tint="#860000"   />
    </item>
</layer-list>

Макет действий Я добавил слой, пригодный для рисования в ImageView

<ImageView
    android:id="@+id/tintLayerTest"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/test_layer"/>

MainActivity В методе onCreate() мы можем найти растровое изображение из слоя drawable используя findViewByID

public class MainActivity extends AppCompatActivity {

    ImageView iv;
    LayerDrawable ld;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Declare ImageView containing LayerDrawable
        iv = (ImageView)findViewById(R.id.tintLayerTest);
        //Get LayerDrawable from ImageView
        ld = (LayerDrawable)iv.getDrawable();
        //Get specific Drawable/Bitmap from within LayerDrawable
        //by ID and pass it as an independent Drawable
        Drawable ldDrawable = ld.findDrawableByLayerId(R.id.tintDrawableImg);

        //Pass your new Drawable to DrawableCompat.setTint
        //and define your color int
        DrawableCompat.setTint(ldDrawable, ContextCompat.getColor(this, R.color.colorAccent));

    }
}

Я надеюсь, что это поможет другим, которые сталкиваются с этим вопросом.

Ответ 2

Попробуйте использовать xml drawable с помощью растрового тега

Сохраните следующий файл xml custom_image.xml & замените свою иконку & оттенок

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