編輯:關於 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 這兩個屬性後,只需預置一張圖,就能在程式運行時動態改變圖片顏色。
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 屬性。
深入認識 Android 的樣式與主題(Styles and Themes)
只要寫過 Android 應用,你一定見過畫面上那些密密麻麻的 XML 屬性。這篇文章要談的,是這些屬性背後的機制:屬性從哪裡來、為什麼有些屬性用不了、自定義屬性怎麼定義
LinearLayout 與 RelativeLayout 組合完成精細版面配置
padding 與 margin 的差異padding 是控制項內容與控制項邊緣之間的距離;margin 則是控制項邊緣與父容器之間的距離。兩者都用來處理間距,但參照對象
Android 自定義圖片選擇器完整實戰教程
本示例可以實現以下效果: 第一格放照相機圖標,點擊即可打開相機拍照; 其餘格子展示設備中已存儲的所有圖片,單擊可查看大圖,長按則會為每張圖片顯示一個 CheckBox
Android 除錯利器 DDMS 完全解析:從截圖到模擬來電
DDMS 在 IDE 與模擬器/真機之間搭起一座橋。透過它,開發者可以查看目標機器上的程序與執行緒狀態、把 Android 螢幕擷取到開發機、檢視 heap 與記憶體配置