xamarin.android - 如何在 xamarin android 中使用 45 度的渐变着色?
问题描述
我想通过将图像分割 45 度来为图像着色,下面是我实现的代码,但我得到的是附加图像。谁能告诉我如何准确划分它?
public class MultiView : View
{
Bitmap bitmap;
int[] colorCodes;
float[] positions;
public MultiView(Context context, Bitmap _bitmap,int[]colors,float[] _positions) : base(context)
{
colorCodes = new int[] { Color.Red,Color.Purple};
bitmap = _bitmap;
positions = _positions ;
this.LayoutParameters = new ViewGroup.LayoutParams(bitmap.Width, bitmap.Height);
}
public override void Draw(Canvas canvas).
{
base.Draw(canvas);
Paint paint = new Paint();
//Shader shader = new LinearGradient(startX, startY, EndX, EndY, colorCodes, positions, Shader.TileMode.Mirror);
Shader shader = new LinearGradient(0, bitmap.Width,0,bitmap.Height,colorCodes,positions,Shader.TileMode.Mirror);
Matrix matrix = new Matrix();
matrix.SetRotate(45);
shader.SetLocalMatrix(matrix);
paint.SetShader(shader);
canvas.DrawRect(0, 0, bitmap.Width, bitmap.Height, paint);
}
}
解决方案
您需要将枢轴设置在要旋转的位置。在这种情况下:
matrix.SetRotate(45, bitmap.Width / 2, bitmap.Height / 2);
编辑:如果你想通过角落均匀地划分它:
var degreesRot = Math.Atan2(bitmap.Width, bitmap.Height) / Math.PI * 180;
matrix.SetRotate(degreesRot, bitmap.Width / 2, bitmap.Height / 2);
推荐阅读
- c# - Polly 强制超时后任务仍在运行
- google-bigquery - 有没有办法仅在 Bigquery SQL 中区分或删除在某些条件下重复的记录?
- python-3.x - 如何在 Python 中编写一个简单的基于 http 请求(方法)的 AWS lambda 函数?
- gstreamer - gstreamer:将录制从一个文件切换到另一个文件
- python - Y_Test的python机器学习预测中的关键错误
- java - 如何限制 Java 中的“新 Scanner(System.in)”系统,不进一步处理用户输入数据,除非它是正确的输入值?
- swift - “非持久性”S4TF 安装
- json - 如何使用 ELK(Eclipse 布局内核)图将节点定位在同一级别
- maven - 配置文件中的依赖版本
- qt5 - 如何将 QGraphicsScene 上的绘画限制在特定区域?