Android實(shí)現(xiàn)設(shè)置APP灰白模式效果
細(xì)心點(diǎn)的童鞋會(huì)發(fā)現(xiàn),到特殊節(jié)日比如清明節(jié)這天很多App都設(shè)置了符合主題的灰白模式,比如京東,如圖所示:

我們再來看看最終實(shí)現(xiàn)的效果圖:

那我們今天就介紹三種方案全局設(shè)置灰白模式:
方案一:
這也是我回復(fù)這位童鞋的方案:給Activity的頂層View設(shè)置置灰,實(shí)現(xiàn)全局置灰效果,下面我們來看看具體的實(shí)現(xiàn)過程。
可以在BaseActivity的onCreate方法中,使用ColorMatrix設(shè)置灰度
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//方案一
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);//灰度效果
paint.setColorFilter(new ColorMatrixColorFilter(cm));
getWindow().getDecorView().setLayerType(View.LAYER_TYPE_HARDWARE,paint);
}
這樣就可以實(shí)現(xiàn)啦,這種方式還是比較簡單的。
方案二:
該方法使用自定義layout,在dispatchdraw方法的時(shí)候,添加一層黑白色的bitmap,讓界面開起來成為黑白模式。但是缺點(diǎn)明顯,應(yīng)用比較卡頓。
1、首先需要先定義一個(gè)GrayFrameLayout布局
public class GrayFrameLayout extends FrameLayout {
private Paint mPaint = new Paint();
public GrayFrameLayout(@NonNull Context context) {
super(context);
}
public GrayFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
mPaint.setColorFilter(new ColorMatrixColorFilter(cm));
}
@Override
protected void onDraw(Canvas canvas) {
canvas.saveLayer(null, mPaint, Canvas.ALL_SAVE_FLAG);
super.onDraw(canvas);
}
@Override
protected void dispatchDraw(Canvas canvas) {
canvas.saveLayer(null, mPaint, Canvas.ALL_SAVE_FLAG);
super.dispatchDraw(canvas);
}
}
2、在BaseActivity的onCreateView方法中做如下處理
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
//方案二
if("FrameLayout".equals(name)){
int attributeCount = attrs.getAttributeCount();
for (int i = 0; i < attributeCount; i++) {
String attributeName = attrs.getAttributeName(i);
String attributeValue = attrs.getAttributeValue(i);
if(attributeName.equals("id")){
int id = Integer.parseInt(attributeValue.substring(1));
String resourceName = getResources().getResourceName(id);
if("android:id/content".equals(resourceName)){
GrayFrameLayout frameLayout = new GrayFrameLayout(this,attrs);
return frameLayout;
}
}
}
}
return super.onCreateView(parent, name, context, attrs);
}
方案三
有些特殊控件需要置灰,比如webview、H5頁面、視頻等
1、創(chuàng)建一個(gè)置灰的管理類
public class GrayManager {
private static GrayManager mInstance;
private Paint mGrayPaint;
private ColorMatrix mGrayMatrix;
public static GrayManager getInstance() {
if (mInstance == null) {
synchronized (GrayManager.class) {
if (mInstance == null) {
mInstance = new GrayManager();
}
}
}
return mInstance;
}
//初始化
public void init() {
mGrayMatrix = new ColorMatrix();
mGrayPaint = new Paint();
mGrayMatrix.setSaturation(0);
mGrayPaint.setColorFilter(new ColorMatrixColorFilter(mGrayMatrix));
}
//硬件加速置灰方法
public void setLayerGrayType(View view) {
if (mGrayMatrix == null || mGrayPaint == null) {
init();
}
view.setLayerType(View.LAYER_TYPE_HARDWARE, mGrayPaint);
}
}
2、特殊控件需要置灰的話直接調(diào)用setLayerGrayType()方法將view傳進(jìn)去,比如demo中讓某個(gè)Activity置灰,那就在Activity里面調(diào)用:
GrayManager.getInstance().setLayerGrayType(getWindow().getDecorView());
以上三種方案都可以實(shí)現(xiàn)灰白模式,也是經(jīng)過demo測試驗(yàn)證的,不過可能由于測試范圍比較狹隘,所以可能還有其它情況,那就后面遇到再補(bǔ)充吧,今天的內(nèi)容就到這里啦。
到此這篇關(guān)于Android實(shí)現(xiàn)設(shè)置APP灰白模式效果的文章就介紹到這了,更多相關(guān)Android APP灰白模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android中EditText屏蔽第三方輸入法表情的方法示例
最近在工作終于遇到一個(gè)問題,因?yàn)榈谌捷斎敕ū砬榈膯栴}導(dǎo)致Android中TextView的內(nèi)容顯示異常,只能想辦法解決了,下面這篇文章主要記錄了在處理Android中EditText屏蔽第三方輸入法表情的方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-01-01
Android 桌面快捷方式實(shí)現(xiàn)實(shí)例詳解
這篇文章主要為大家介紹了Android 桌面快捷方式實(shí)現(xiàn)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android實(shí)現(xiàn)判斷某個(gè)服務(wù)是否正在運(yùn)行的方法
這篇文章主要介紹了Android實(shí)現(xiàn)判斷某個(gè)服務(wù)是否正在運(yùn)行的方法,涉及Android針對(duì)系統(tǒng)服務(wù)運(yùn)行狀態(tài)的判斷技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android為TextView添加字體庫和設(shè)置描邊的方法
本篇文章主要介紹了Android為TextView添加字體庫和設(shè)置描邊的方法,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09
月下載量上千次Android實(shí)現(xiàn)二維碼生成器app源碼分享
既然是二維碼生成器那么我們?nèi)绾沃谱鞫S碼呢?這篇文章為大家分享了月下載量上千次Android實(shí)現(xiàn)二維碼生成器app源碼,希望大家喜歡2015-12-12
Android實(shí)現(xiàn)控件的縮放移動(dòng)功能
這篇文章主要介紹了android控件的縮放,移動(dòng)功能,本文圖文并茂給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-01-01

