Android 黑白化界面

Activity处理 相关链接App 黑白化实现探索,有一行代码实现的方案吗?鸿洋的博客-CSDN博客

//根部局处理
public class GrayFrameLayout extends FrameLayout {
    private Paint mPaint = new Paint();

    public GrayFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        ColorMatrix cm = new ColorMatrix();
        //设置饱和度0
        cm.setSaturation(0);
        mPaint.setColorFilter(new ColorMatrixColorFilter(cm));
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        canvas.saveLayer(null, mPaint, Canvas.ALL_SAVE_FLAG);
        super.dispatchDraw(canvas);
        canvas.restore();
    }


    @Override
    public void draw(Canvas canvas) {
        canvas.saveLayer(null, mPaint, Canvas.ALL_SAVE_FLAG);
        super.draw(canvas);
        canvas.restore();
    }

}
 
//Activity处理  重写onCreateView  这是全局方案,当前activity对应的界面都会黑白
public class TestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        try {
            if ("FrameLayout".equals(name)) {
                int count = attrs.getAttributeCount();
                for (int i = 0; i < count; i++) {
                    String attributeName = attrs.getAttributeName(i);
                    String attributeValue = attrs.getAttributeValue(i);
                    if (attributeName.equals("id")) {
                        int id = Integer.parseInt(attributeValue.substring(1));
                        String idVal = getResources().getResourceName(id);
                        if ("android:id/content".equals(idVal)) {
                            GrayFrameLayout grayFrameLayout = new GrayFrameLayout(context, attrs);
    //                            grayFrameLayout.setWindow(getWindow());
                            return grayFrameLayout;
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.onCreateView(name, context, attrs);
    }
}

状态栏颜色处理 单个颜色设置饱和度Android 颜色处理_-SOLO-的博客-CSDN博客

    /**
     * 设置饱和度 重新计算原颜色 置灰
     * @param normalColor
     * @return
     */
    public int getGrayColor(int normalColor){
        if (!grayActivityFlag){
            return normalColor;
        }
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(0);
        float[] m = colorMatrix.getArray();
        int R = Color.red(normalColor);
        int G = Color.green(normalColor);
        int B = Color.blue(normalColor);
        int A = Color.alpha(normalColor);
        /**
         *  [ a, b, c, d, e,
         *    f, g, h, i, j,
         *    k, l, m, n, o,
         *    p, q, r, s, t ]
         *
         *    R = a*R + b*G + c*B + d*A + e;
         *   G = f*R + g*G + h*B + i*A + j;
         *   B= k*R + l*G + m*B + n*A + o;
         *   A = p*R + q*G + r*B + s*A + t
         */
        int nR = (int) (m[0] * R + m[1] * G + m[2] * B + m[3] * A + m[4]);
        int nG = (int) (m[5] * R + m[6] * G + m[7] * B + m[8] * A + m[9]);
        int nB = (int) (m[10] * R + m[11] * G + m[12] * B + m[13] * A + m[14]);
        int nA = (int) (m[15] * R + m[16] * G + m[17] * B + m[18] * A + m[19]);
        return Color.argb(nA,nR,nG,nB);
    }

plan B 单独处理某个界面,如一个activity下的多个fragment只处理homeFragment

    /**
     * 0-设置黑白模式  1-恢复
     * @param activity
     * @param saturation A value of 0 maps the color to gray-scale. 1 is identity.
     */
    public void setGrayActivity(Activity activity,int saturation){
        int grayActivity = tdxApp.getInstance().getIntFrame(FRAME_GRAY_ACTIVITY, 0);
        grayActivityFlag = (grayActivity != 0);
        if (grayActivity == 0){
            return;
        }
        View decorView = activity.getWindow().getDecorView();
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(saturation);
        paint.setColorFilter(new ColorMatrixColorFilter(cm));
        decorView.setLayerType(View.LAYER_TYPE_HARDWARE,paint);
    }

你可能感兴趣的:(Android 黑白化界面)