Graphics-----ColorFilter

 

1.使用ColorFilter

MaskFilter是对一个Paint的alpha通道的转换,而ColorFilter则是对每一个RGB通道应用转换 。所有由ColorFilter所派生的类在执行它们的转换时,都会忽略alpha通道。

Android包含三个ColorFilter:

ColorMatrixColorFilter  可以指定一个4×5的ColorMatrix并将其应用到一个Paint中。ColorMatrixes通常在程序中用于对图像进行处理 ,而且由于它们支持使用矩阵相乘的方法来执行链接转换,所以它们很有用。

LightingColorFilter  乘以第一个颜色的RGB通道,然后加上第二个颜色。每一次转换的结果都限制在0到255之间。

PorterDuffColorFilter  可以使用数字图像合成的16条Porter-Duff 规则中的任意一条来向Paint应用一个指定的颜色。

使用setColorFilter方法应用ColorFilter,如下所示:

Java代码 
  1. myPaint.setColorFilter(new LightingColorFilter(Color.BLUE, Color.RED));   

API中的ColorMatrixSample是说明如何使用ColorFilter和Color Matrix的非常好的例子。

 

2.

  1. private static void setContrast(ColorMatrix cm, float contrast) {  
  2.             float scale = contrast + 1.f;  
  3.                float translate = (-.5f * scale + .5f) * 255.f;  
  4.             cm.set(new float[] {  
  5.                    scale, 000, translate,  
  6.                    0, scale, 00, translate,  
  7.                    00, scale, 0, translate,  
  8.                    00010 });  
  9.         }  

 

Java代码 
  1. float contrast = mAngle / 180.f;  
  2.           
  3.         setContrast(cm, contrast);  
  4.         paint.setColorFilter(new ColorMatrixColorFilter(cm));  
  5.         canvas.drawBitmap(mBitmap, x + mBitmap.getWidth() + 10, y, paint);  
 


 

以下是在C#中生成带噪声的正弦曲线并使用Drawing绘制折线图,然后使用S-G滤波后绘制折线图的示例代码: ```csharp using System; using System.Drawing; using System.Windows.Forms; namespace SinWaveWithNoise { public partial class Form1 : Form { private const int Width = 600; private const int Height = 400; private const int Margin = 50; private const int NumPoints = 100; private readonly Random _random = new Random(); private readonly PointF[] _points = new PointF[NumPoints]; private readonly PointF[] _filteredPoints = new PointF[NumPoints]; private readonly Pen _sinPen = new Pen(Color.Red, 2f); private readonly Pen _noisePen = new Pen(Color.Blue, 1f); private readonly Pen _filteredPen = new Pen(Color.Green, 2f); public Form1() { InitializeComponent(); // Generate noisy sine wave points for (int i = 0; i < NumPoints; i++) { float x = (float) i / (NumPoints - 1) * (Width - 2 * Margin) + Margin; float y = (float) (Math.Sin(x * 2 * Math.PI / (Width - 2 * Margin)) + 0.5 * _random.NextDouble() - 0.25); _points[i] = new PointF(x, y * (Height - 2 * Margin) + Margin); } // Apply S-G filter float[] coefficients = { -3f / 35f, 12f / 35f, 17f / 35f, 12f / 35f, -3f / 35f }; for (int i = 2; i < NumPoints - 2; i++) { float filteredY = 0f; for (int j = 0; j < 5; j++) { filteredY += coefficients[j] * _points[i - 2 + j].Y; } _filteredPoints[i] = new PointF(_points[i].X, filteredY); } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; // Draw axes g.DrawLine(Pens.Black, Margin, Margin, Margin, Height - Margin); g.DrawLine(Pens.Black, Margin, Height - Margin, Width - Margin, Height - Margin); g.DrawString("0", Font, Brushes.Black, Margin - 10, Height - Margin + 5); g.DrawString("2π", Font, Brushes.Black, Width - Margin + 5, Height - Margin + 5); g.DrawString("y", Font, Brushes.Black, Margin - 20, Margin - 15); g.DrawString("x", Font, Brushes.Black, Width - Margin + 10, Height - Margin + 5); // Draw sin wave for (int i = 0; i < NumPoints - 1; i++) { g.DrawLine(_sinPen, _points[i], _points[i + 1]); } // Draw noise for (int i = 0; i < NumPoints - 1; i++) { g.DrawLine(_noisePen, _points[i].X, _points[i].Y, _points[i + 1].X, _points[i + 1].Y); } // Draw filtered wave for (int i = 0; i < NumPoints - 1; i++) { g.DrawLine(_filteredPen, _filteredPoints[i], _filteredPoints[i + 1]); } } } } ``` 该代码生成一个带有噪声的正弦曲线,并使用S-G滤波器对其进行平滑处理。然后,它使用Drawing将原始曲线、噪声曲线和平滑曲线绘制为折线图。您可以在窗体的OnPaint事件中使用此代码来显示绘图。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值