直播视频app源码使用ColorMatrix 对图片进行风格处理
直播视频app源码使用ColorMatrix 对图片进行风格处理的相关代码
界面xml布局
1 | <?xml version= "1.0" encoding= "utf-8" ?><br><RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android" <br> android:layout_width= "match_parent" <br> android:layout_height= "match_parent" ><br> <ImageView<br> android:id= "@+id/iv_img" <br> android:layout_width= "wrap_content" <br> android:layout_height= "wrap_content" <br> android:layout_centerHorizontal= "true" <br> android:layout_margin= "25dp" <br> android:src= "@drawable/ncxy" /><br> <Spinner<br> android:id= "@+id/sp_colorFilter_item" <br> android:layout_width= "match_parent" <br> android:layout_height= "45dp" <br> android:layout_below= "@id/iv_img" <br> android:layout_centerHorizontal= "true" <br> android:layout_marginLeft= "45dp" <br> android:layout_marginRight= "45dp" <br> android:entries= "@array/colorMatrix" <br> android:gravity= "center_horizontal" /><br></RelativeLayout> |
spinner 监听代码
1 | spinner.setSelection(0, false );<br> spinner.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener() {<br> @Override<br> public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {<br> imageView.setImageResource(R.drawable.ncxy);<br> Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();<br> switch (position) {<br> case 0:<br> break ;<br> case 1:<br> imageView.setImageBitmap(getBitmap(bitmap, getColorMatrixGrayscale()));<br> break ;<br> case 2:<br> imageView.setImageBitmap(getBitmap(bitmap, getColorMatrixSepia()));<br> break ;<br> case 3:<br> imageView.setImageBitmap(getBitmap(bitmap, getColorMatrixBinary()));<br> break ;<br> case 4:<br> imageView.setImageBitmap(getBitmap(bitmap, getColorMatrixInvert()));<br> break ;<br> case 5:<br> imageView.setImageBitmap(getBitmap(bitmap, getColorMatrixAlphaBlue()));<br> break ;<br> case 6:<br> imageView.setImageBitmap(getBitmap(bitmap, getColorMatrixAlphaPink()));<br> break ;<br> }<br> }<br> @Override<br> public void onNothingSelected(AdapterView<?> parent) {<br> }<br> }); |
第一行是为了解决程序界面打开的时候自动调用监听;在监听里,每选择一个 item,就会调用一个新的方法返回一个 colorMatrix 对象,再通过 getBitmap 方法返回一个新的 bitmap ,设置给 imageview.下面是 getBitmap 方法代码:
1 | /**<br> * 根据colorMaxtrix获取对应图片<br> *<br> * @param colorMatrix<br> * @return<br> */ <br> private Bitmap getBitmap(Bitmap original, ColorMatrix colorMatrix) {<br> Bitmap bitmap = Bitmap.createBitmap(original.getWidth(), original.getHeight(),<br> Bitmap.Config.ARGB_8888);<br> Canvas canvas = new Canvas(bitmap);<br> Paint paint = new Paint();<br> paint.setColorFilter( new ColorMatrixColorFilter(colorMatrix));<br> canvas.drawBitmap(original, 0, 0, paint);<br> return bitmap;<br> } |
每个 item 对应的方法如下:
1 | private ColorMatrix getColorMatrixGrayscale() {<br> ColorMatrix colorMatrix = new ColorMatrix();<br> colorMatrix.setSaturation(0);<br> return colorMatrix;<br> }<br> private ColorMatrix getColorMatrixSepia() {<br> ColorMatrix colorMatrix = new ColorMatrix();<br> colorMatrix.setSaturation(0);<br> ColorMatrix colorScale = new ColorMatrix();<br> colorScale.setScale(1, 1, 0.8f, 1);<br> // Convert to grayscale, then apply brown color<br> colorMatrix.postConcat(colorScale);<br> return colorMatrix;<br> }<br> private ColorMatrix getColorMatrixBinary() {<br> ColorMatrix colorMatrix = new ColorMatrix();<br> colorMatrix.setSaturation(0);<br> float m = 255f;<br> float t = -255 * 128f;<br> ColorMatrix threshold = new ColorMatrix(new float[]{<br> m, 0, 0, 1, t,<br> 0, m, 0, 1, t,<br> 0, 0, m, 1, t,<br> 0, 0, 0, 1, 0<br> });<br> // Convert to grayscale, then scale and clamp<br> colorMatrix.postConcat(threshold);<br> return colorMatrix;<br> }<br> private ColorMatrix getColorMatrixInvert() {<br> return new ColorMatrix(new float[]{<br> -1, 0, 0, 0, 255,<br> 0, -1, 0, 0, 255,<br> 0, 0, -1, 0, 255,<br> 0, 0, 0, 1, 0<br> });<br> }<br> private ColorMatrix getColorMatrixAlphaBlue() {<br> return new ColorMatrix(new float[]{<br> 0, 0, 0, 0, 0,<br> 0.3f, 0, 0, 0, 50,<br> 0, 0, 0, 0, 255,<br> 0.2f, 0.4f, 0.4f, 0, -30<br> });<br> }<br> private ColorMatrix getColorMatrixAlphaPink() {<br> return new ColorMatrix(new float[]{<br> 0, 0, 0, 0, 255,<br> 0, 0, 0, 0, 0,<br> 0.2f, 0, 0, 0, 50,<br> 0.2f, 0.2f, 0.2f, 0, -20<br> });<br> } |
以上就是直播视频app源码使用ColorMatrix 对图片进行风格处理的相关代码, 更多内容欢迎关注之后的文章
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现