欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)設(shè)置APP灰白模式效果

 更新時(shí)間:2021年12月15日 14:43:08   作者:龍旋  
大家好,本篇文章主要講的是Android實(shí)現(xiàn)設(shè)置APP灰白模式效果,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽

細(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)文章

最新評(píng)論