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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android12?SD如何動(dòng)態(tài)申請讀寫權(quán)限
這篇文章主要給大家介紹了關(guān)于android12?SD如何動(dòng)態(tài)申請讀寫權(quán)限的相關(guān)資料,從Android?6.0開始,權(quán)限不再是在manifest?件中粘貼?下即可,這時(shí)候權(quán)限也正式?進(jìn)?家的視野,需要的朋友可以參考下2023-07-07Android Studio default not found錯(cuò)誤解決辦法
這篇文章主要介紹了Android Studio gradle 編譯提示‘default not found’ 解決辦法的相關(guān)資料,需要的朋友可以參考下2017-01-01Android中fragment+viewpager實(shí)現(xiàn)布局
這篇文章主要為大家詳細(xì)介紹了Android中fragment+viewpager實(shí)現(xiàn)布局效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android 實(shí)現(xiàn)永久性開啟adb 的root權(quán)限
這篇文章主要介紹了Android 實(shí)現(xiàn)永久性開啟adb 的root權(quán)限,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android ListView 實(shí)例講解清晰易懂
這篇文章主要通過實(shí)例介紹了Android ListView,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09RxJava2.x實(shí)現(xiàn)定時(shí)器的實(shí)例代碼
本篇文章主要介紹了RxJava2.x實(shí)現(xiàn)定時(shí)器,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06Android實(shí)現(xiàn)仿淘寶購物車增加和減少商品數(shù)量功能demo示例
這篇文章主要介紹了Android實(shí)現(xiàn)仿淘寶購物車增加和減少商品數(shù)量功能,結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)的淘寶購物車商品數(shù)量變換與計(jì)算相關(guān)技巧,需要的朋友可以參考下2016-07-07Android實(shí)現(xiàn)圓圈倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圓圈倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08Android 類似UC瀏覽器的效果:向上滑動(dòng)地址欄隱藏功能
這篇文章主要介紹了Android 類似UC瀏覽器的效果:向上滑動(dòng)地址欄隱藏功能,需要的朋友可以參考下2017-12-12Android應(yīng)用開發(fā)中自定義ViewGroup的究極攻略
這里我們要演示的自定義ViewGroup中將實(shí)現(xiàn)多種方式排列和滑動(dòng)等效果,并且涵蓋子View之間Touch Event的攔截與處理等問題,完全干貨,下面就為大家送上Android應(yīng)用開發(fā)中自定義ViewGroup的究極實(shí)例攻略2016-05-05