Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách thay đổi màu sắc của Drawable trong Android

Giới thiệu

Ví dụ này minh họa cách tạo hiệu ứng chuyển đổi màu nền (background animation) bằng AnimationDrawable và các file XML drawable (gradient, shape) trong Android.

Các bước thực hiện

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project.

Bước 2: Thiết kế giao diện chính (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/background"
    tools:context=".MainActivity"
    tools:ignore="InvalidId">
</LinearLayout>

Bước 3: Viết code logic (src/MainActivity.java)

package com.app.sample;

import androidx.appcompat.app.AppCompatActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout yourLayout = findViewById(R.id.activity_main);
        AnimationDrawable animationDrawable = (AnimationDrawable) yourLayout.getBackground();

        animationDrawable.setEnterFadeDuration(4000);
        animationDrawable.setExitFadeDuration(4000);
        animationDrawable.start();
    }
}

Bước 4: Tạo animation list (res/drawable/background.xml)

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="https://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/gradient1"
        android:duration="4000" />
    <item
        android:drawable="@drawable/gradient2"
        android:duration="4000" />
</animation-list>

Bước 5: Tạo gradient 1 (res/drawable/gradient1.xml)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <gradient
        android:angle="225"
        android:endColor="#044fab"
        android:startColor="#21d6d3" />
</shape>

Bước 6: Tạo gradient 2 (res/drawable/gradient2.xml)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <gradient
        android:angle="45"
        android:endColor="#933c94"
        android:startColor="#517f95" />
</shape>

Bước 7: Cấu hình Manifest (Manifests/AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.app.sample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

Chạy ứng dụng

Kết nối thiết bị, nhấn Run. Nền sẽ chuyển đổi mượt mà giữa hai gradient.

Cách thay đổi màu sắc của Drawable trong Android

Mở rộng: Thay đổi màu Drawable theo code (Runtime)

Ngoài animation XML, có thể thay đổi màu Drawable động bằng code:

1. Dùng ColorFilter (cho ImageView/Drawable đơn giản)

Drawable drawable = ContextCompat.getDrawable(context, R.drawable.ic_icon);
drawable.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
imageView.setImageDrawable(drawable);

2. Dùng DrawableCompat (API 19+ - khuyến nghị)

Drawable wrapped = DrawableCompat.wrap(drawable.mutate());
DrawableCompat.setTint(wrapped, Color.BLUE);
DrawableCompat.setTintMode(wrapped, PorterDuff.Mode.SRC_IN);

3. Dùng ColorStateList cho StateListDrawable (selector)

ColorStateList csl = new ColorStateList(
    new int[][] { new int[] {android.R.attr.state_pressed}, new int[] {} },
    new int[] { Color.RED, Color.BLUE }
);
DrawableCompat.setTintList(wrapped, csl);