首页 > 解决方案 > 如何在 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);                                          
    }  
}

标签: xamarin.android

解决方案


您需要将枢轴设置在要旋转的位置。在这种情况下:

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);

推荐阅读