Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 5.x 特性概覽(五):著色 Tinting 與裁剪 Clipping

編輯:Android 程式設計入門

上一節介紹了 Material Design 對陰影效果的實現,本節則聚焦 Android 5.x 在圖像處理上新增的兩個特性——Tinting(著色)與 Clipping(裁剪)。

一、Tinting(著色)

下面透過對圖片著色,幫助大家理解這一特性:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ImageView android:layout_width="100dp" android:layout_height="100dp"
        android:layout_gravity="center" android:elevation="5dip"
        android:src="@drawable/ic_launcher" />

    <ImageView android:layout_width="100dp" android:layout_height="100dp"
        android:layout_gravity="center" android:elevation="5dip"
        android:src="@drawable/ic_launcher"
        android:tint="@android:color/holo_blue_bright" />

    <ImageView android:layout_width="100dp" android:layout_height="100dp"
        android:layout_gravity="center" android:elevation="5dip"
        android:src="@drawable/ic_launcher"
        android:tint="@android:color/holo_blue_bright" android:tintMode="add" />

    <ImageView android:layout_width="100dp" android:layout_height="100dp"
        android:layout_gravity="center" android:elevation="5dip"
        android:src="@drawable/ic_launcher"
        android:tint="@android:color/holo_blue_bright" android:tintMode="multiply" />
</LinearLayout>

運行效果圖如下:

關於著色可以總結為:Tinting 的使用非常簡單,只要在 XML 中配置好 tint 和 tintMode 即可。如上例,不同的 tint 與 tintMode 組合會讓圖片呈現不同效果。此外,Tint 還能透過修改圖像的 Alpha 遮罩來改變顏色,達到重新著色的目的,這在一些圖像處理 App 中會非常方便。

二、Clipping(裁剪)

使用裁剪的基本步驟是:先用 ViewOutlineProvider 修改 outline,再通過 setOutlineProvider 把 outline 作用給視圖。下面以一個例子說明——把一個正方形的 TextView 裁剪成一個圓角正方形和一個圓。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView android:id="@+id/tv_rect" android:layout_width="100dp"
        android:layout_height="100dp" android:layout_gravity="center"
        android:layout_marginTop="20dp" android:elevation="1dip" />

    <TextView android:id="@+id/tv_circle" android:layout_width="100dp"
        android:layout_height="100dp" android:layout_gravity="center"
        android:elevation="1dip" />
</LinearLayout>

設置為圓形還是矩形的 Java 代碼如下:

public class MainActivity extends Activity {
    private TextView tv_rect;
    private TextView tv_circle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        tv_rect = (TextView) findViewById(R.id.tv_rect);
        tv_circle = (TextView) findViewById(R.id.tv_circle);

        // 矩形 outline
        ViewOutlineProvider viewOutlineProvider1 = new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRect(0, 0, view.getWidth(), view.getHeight());
            }
        };
        // 圓形 outline
        ViewOutlineProvider viewOutlineProvider2 = new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setOval(0, 0, view.getWidth(), view.getHeight());
            }
        };
        tv_circle.setOutlineProvider(viewOutlineProvider2);
        tv_rect.setOutlineProvider(viewOutlineProvider1);
    }
}

最終運行效果如下:

以上就是 Android 5.x 著色與裁剪特性的介紹。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved