AppCompat
1、把androidx.appcompat升級到1.1.0以後,出現端內語言切換在Android 5~7 失效的問題。
Kotlin解決方案:
override fun applyOverrideConfiguration(overrideConfiguration: Configuration?) {
if (overrideConfiguration != null) {
val uiMode = overrideConfiguration.uiMode
overrideConfiguration.setTo(baseContext.resources.configuration)
overrideConfiguration.uiMode = uiMode
}
super.applyOverrideConfiguration(overrideConfiguration)
}
Java解決方案:
@Override
public void applyOverrideConfiguration(Configuration overrideConfiguration) {
if (overrideConfiguration != null) {
int uiMode = overrideConfiguration.uiMode;
overrideConfiguration.setTo(getBaseContext().getResources().getConfiguration());
overrideConfiguration.uiMode = uiMode;
}
super.applyOverrideConfiguration(overrideConfiguration);
}
RecyclerView
1、RecyclerView的Holder的itemView設置透明度不生效
由於Recycylerview默認會帶有動畫DefaultItemAnimator,所以就會導致setAlpha不生效,解決方法是取消ItemAnimatior。
RecyclerView.setItemAnimator(null);
消息隊列
1、能否catch一個在子線程執行的Runnable?
如下代碼所示,當處於A線程,然後在B線程post一個runnable,runnable裏面的代碼有可能會拋出異常,那麼我們不能夠catch這個runanble的異常,因爲我們都知道,當我們往線程裏面post一個runnable的時候,它會放到這個線程的消息隊列裏面,然後在未來某一個時間點出隊執行,但是我們try catch是在A線程中順序執行的,所以有可能當我們的try catch執行完畢,但是runnable裏面的代碼還未開始執行,所以這樣寫是錯誤的。
// 當前線程處於非UI線程
try {
ThreadUtils.runOnUiThread(() -> {
// 切換到UI線程
if (bitmap != null && !bitmap.isRecycled()) {
Bitmap copyedBitmap = bitmap.copy(bitmap.getConfig(), true);
}
});
} catch (Exception e) {
e.printStackTrace();
}
要改成正確寫法,還是要把try catch放到具體執行線程的代碼中,如下所示:
ThreadUtils.runOnUiThread(() -> {
try {
if (bitmap != null && !bitmap.isRecycled()) {
Log.i(TAG, "bindView(). fetch bg success");
Bitmap copyedBitmap = bitmap.copy(bitmap.getConfig(), true);
}
} catch (Exception e) {
e.printStackTrace();
}
});
Camera2
1、判斷Camera2是否兼容設備
Camera2雖然是支持API21以上的設備,但是它同時對設備有不同的兼容程度,目前劃分爲以下四個等級:
CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY,
CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED,
CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL,
CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_3
它們的可支持程度爲 LEGACY < LIMITED < FULL < LEVEL_3。我們可以通過api來獲取Camera2對當前設備的支持程度:
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
int deviceLevel = characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);
當我們發現設備的level很低時,建議還是放棄camera2,替換回camera1,例如如果設備的level爲INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY,那麼使用Camera2對每一幀進行操作,都會非常卡頓。
A LEGACY device does not support per-frame control, manual sensor control, manual post-processing, arbitrary cropping regions, and has relaxed performance constraints.
資源名稱
1、invalid symbol錯誤
在自定義屬性,如果自定義屬性值爲final,default,continue等值,如下所示,就會導致編譯時報invalid symbol的錯誤。同樣地,如果在給資源id或者資源名字設爲這些值也會報錯invalid symbol。
<declare-styleable name="ResultListLayout">
<attr name="result_type" format="enum">
<enum name="win" value="1" />
<enum name="lose" value="2" />
<enum name="default" value="3" />
</attr>
</declare-styleable>