android灰色滤镜布局
创始人
2024-03-14 18:55:53
0

android灰色滤镜布局

h5网页灰色滤镜

只要给 html 加下列css 样式就可以了

html {filter: progid:DXImageTransform.Microsoft.BasicImage(grayscale=1);-webkit-filter: grayscale(100%);
}

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

Android组件灰色滤镜

ColorMartrix类,这个类对外提供了很多 API,大家直接调用 API 就能得到大部分想要的效果了,除非你有特别特殊的操作,那么可以自己通过矩阵去运算。

像灰度这样的效果,我们可以通过饱和度 API来操作:

 //设置 1:彩色  0:黑白
setSaturation(float sat)

传入 0 就可以了,底层传入了一个特定的矩阵去做的运算。

自定义ImagView灰色滤镜

自定义ImagView

/*** 灰色滤镜 ImageView*/
public class GrayImageView extends AppCompatImageView {private Paint mPaint = new Paint();public GrayImageView(Context context, AttributeSet attrs) {super(context, attrs);ColorMatrix cm = new ColorMatrix();//设置 1:彩色  0:黑白cm.setSaturation(0);mPaint.setColorFilter(new ColorMatrixColorFilter(cm));}@Overridepublic void draw(Canvas canvas) {canvas.saveLayer(null, mPaint, Canvas.ALL_SAVE_FLAG);super.draw(canvas);canvas.restore();}/*** @desc 设置灰色页面*/public void setGray(boolean isGray) {ColorMatrix cm = new ColorMatrix();//设置 1:彩色  0:黑白cm.setSaturation(isGray ? 0 : 1);mPaint.setColorFilter(new ColorMatrixColorFilter(cm));//重新绘制画布invalidate();}
}

layout

使用

  GrayImageView givImg= findViewById(R.id.giv_main_img);//false 彩色  true灰色givImg.setGray(false);

效果
在这里插入图片描述
在这里插入图片描述

自定义LinearLayout灰色滤镜

自定义LinearLayout

/*** 灰色滤镜 线性布局*/
public class GrayLinearLayout extends LinearLayout {private Paint mPaint = new Paint();public GrayLinearLayout(Context context, AttributeSet attrs) {super(context, attrs);ColorMatrix cm = new ColorMatrix();//设置 1:彩色  0:黑白cm.setSaturation(1);mPaint.setColorFilter(new ColorMatrixColorFilter(cm));}@Overridepublic void draw(Canvas canvas) {canvas.saveLayer(null, mPaint, Canvas.ALL_SAVE_FLAG);super.draw(canvas);canvas.restore();}@Overrideprotected void dispatchDraw(Canvas canvas) {canvas.saveLayer(null, mPaint, Canvas.ALL_SAVE_FLAG);super.dispatchDraw(canvas);canvas.restore();}/*** @desc 设置灰色页面*/public void setGray(boolean isGray) {ColorMatrix cm = new ColorMatrix();//设置 1:彩色  0:黑白cm.setSaturation(isGray ? 0 : 1);mPaint.setColorFilter(new ColorMatrixColorFilter(cm));//重新绘制画布invalidate();}
}

layout


使用

   GrayLinearLayout gllLayout = findViewById(R.id.gll_main_layout);//false 彩色  true灰色gllLayout.setGray(false);

效果
在这里插入图片描述
在这里插入图片描述
其他布局类似

相关内容

热门资讯

汽车油箱结构是什么(汽车油箱结... 本篇文章极速百科给大家谈谈汽车油箱结构是什么,以及汽车油箱结构原理图解对应的知识点,希望对各位有所帮...
美国2年期国债收益率上涨15个... 原标题:美国2年期国债收益率上涨15个基点 美国2年期国债收益率上涨15个基...
嵌入式 ADC使用手册完整版 ... 嵌入式 ADC使用手册完整版 (188977万字)💜&#...
重大消息战皇大厅开挂是真的吗... 您好:战皇大厅这款游戏可以开挂,确实是有挂的,需要了解加客服微信【8435338】很多玩家在这款游戏...
盘点十款牵手跑胡子为什么一直... 您好:牵手跑胡子这款游戏可以开挂,确实是有挂的,需要了解加客服微信【8435338】很多玩家在这款游...
senator香烟多少一盒(s... 今天给各位分享senator香烟多少一盒的知识,其中也会对sevebstars香烟进行解释,如果能碰...
终于懂了新荣耀斗牛真的有挂吗... 您好:新荣耀斗牛这款游戏可以开挂,确实是有挂的,需要了解加客服微信8435338】很多玩家在这款游戏...
盘点十款明星麻将到底有没有挂... 您好:明星麻将这款游戏可以开挂,确实是有挂的,需要了解加客服微信【5848499】很多玩家在这款游戏...
总结文章“新道游棋牌有透视挂吗... 您好:新道游棋牌这款游戏可以开挂,确实是有挂的,需要了解加客服微信【7682267】很多玩家在这款游...
终于懂了手机麻将到底有没有挂... 您好:手机麻将这款游戏可以开挂,确实是有挂的,需要了解加客服微信【8435338】很多玩家在这款游戏...