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

APP 開發實戰 146:減少預置圖片資源的兩種方案

編輯:關於 Android 程式設計

在優化 App 體積時,減少預置的圖片資源是一個很有效的手段。以下介紹兩種常用方案。

方案一:用程式碼旋轉代替多張方向圖片

App 中有時會用到上下左右箭頭這類內容相同、方向不同的圖片。這時只需預置一張「上」箭頭圖標,其餘下、左、右箭頭都可以透過程式碼旋轉這張圖來實現,既減少了圖片數量,也縮小了 App 體積。

在 Google 官方文檔中就有如下說明:

You can also omit resources that are only a rotated equivalent of another resource. The following code snippet provides an example of turning an "expand" arrow into a "collapse" arrow icon by simply rotating the original image 180 degrees:

(來源:https://developer.android.com/topic/performance/reduce-apk-size.html)

如下兩個圖標箭頭都朝右:

對應的 XML 程式碼:

android:layout_width="80dp"
android:layout_height="wrap_content"
app:srcCompat="@drawable/arrow_right_red"
android:id="@+id/imageView" />

android:layout_width="80dp"
android:layout_height="wrap_content"
app:srcCompat="@drawable/arrow_right_red"
android:id="@+id/imageViewLeft" />

如果想把第二個改成朝左的箭頭,可以這樣實現:

先定義旋轉動畫的 XML:

android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="180" />

具體實現程式碼:

Animation rotateAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate);
ImageView imageViewLeft = (ImageView) findViewById(R.id.imageViewLeft);
imageViewLeft.startAnimation(rotateAnimation);
// 圖片旋轉後不恢復原狀
rotateAnimation.setFillAfter(true);

如此一來,只用一張圖、透過程式碼,就實現了兩種顯示效果。

方案二:使用 tint 與 tintMode 屬性動態改色

當你只想改變圖片內容的顏色、而不改變圖形本身時,以往的做法是預置好幾張不同顏色的圖片。使用 tint 和 tintMode 這兩個屬性後,只需預置一張圖,就能在程式運行時動態改變圖片顏色。

Google 官方文檔同樣有說明:

You can include a separate resource for variations of an image, such as tinted, shaded, or rotated versions of the same image. We recommend, however, that you reuse the same set of resources, customizing them as needed at runtime.

Android provides several utilities to change the color of an asset, either using the android:tint and tintMode attributes on Android 5.0 (API level 21) and higher. For lower versions of the platform, use the ColorFilter class.

(來源:https://developer.android.com/topic/performance/reduce-apk-size.html)

對應的 XML 程式碼:

android:id="@+id/login_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="8dp"
android:src="@drawable/login" />

加上 tint 屬性後,圖片內容不變但顏色改變:

android:id="@+id/login_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="8dp"
android:src="@drawable/login"
android:tint="#EEEEEE" />

也可以透過程式碼動態設定:

ImageView imageView = (ImageView) findViewById(R.id.login_image);
imageView.setColorFilter(Color.GRAY);

許多 App 在密碼輸入框右側都有一個圖標,反覆點擊時會顯示不同顏色,同時密碼在明文與密文之間切換。傳統做法要預置兩張不同顏色的圖片,改用 tint 與 tintMode 後只需一張即可:

android:id="@+id/login_eye_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_marginRight="12dp"
android:background="@drawable/login_eye_first"
android:gravity="center"
android:backgroundTint="@color/colorAccent"
android:backgroundTintMode="screen" />

程式碼動態設定:

private void setDisplayPassword() {
    mIsDisplayPassword = !mIsDisplayPassword;
    if (mIsDisplayPassword) {
        ViewCompat.setBackgroundTintList(mLoginEyeEt, ColorStateList.valueOf(Color.parseColor("#FF4081")));
        ViewCompat.setBackgroundTintMode(mLoginEyeEt, PorterDuff.Mode.SCREEN);
        mEditPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
    } else {
        ViewCompat.setBackgroundTintList(mLoginEyeEt, ColorStateList.valueOf(Color.parseColor("#CCCCCC")));
        ViewCompat.setBackgroundTintMode(mLoginEyeEt, PorterDuff.Mode.SCREEN);
        mEditPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
    }
}

之所以使用 AppCompatTextView 這類控件,是為了讓低於 Android 5.0(API level 21)的系統也能使用 tint 與 tintMode 屬性。

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