Xamarin从位图获取错误的颜色

时间:2017-03-27 14:14:13

标签: c# android xamarin bitmap xamarin.android

我正在尝试在Android上制作一个颜色选择器,但我无法弄清楚我做错了什么我得到了错误的颜色。我有一个ImageView,我正在将这个图像解码为位图。在位图中我使用的是GetPixel()方法,但我认为此时我做错了。有人能说出我做错了吗?这是我正在使用的图像:

enter image description here

我在Z1 Compact上测试它(720x1280分辨率,android 5.1.1),图片是750x300,所以分辨率可能是问题吗?

 namespace ImageViewTest
    {
        [Activity(Label = "ImageView", MainLauncher = true, Icon = "@drawable/icon")]
        public class MainActivity : Activity
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);

                int colorPickerWidth = 750, colorPickerHeight = 300, colorBaseHeight = 1184, colorBaseWidth = 720;
                float colorPickerScaleWidth = 720 / colorBaseWidth, colorPickerScaleHeight = 1184 / colorBaseHeight;

                // Set our view from the "main" layout resource
                SetContentView(Resource.Layout.Main);
                RelativeLayout rl = FindViewById<RelativeLayout>(Resource.Id.relative);
                ImageView colorPanel = new ImageView(this);

                colorPanel.SetImageResource(Resource.Drawable.colorPicker);
                colorPanel.LayoutParameters = new ViewGroup.LayoutParams(colorPickerWidth, colorPickerHeight);
                Bitmap bitmap = BitmapFactory.DecodeResource(this.Resources, Resource.Drawable.colorPicker);
                bitmap = Bitmap.CreateBitmap(bitmap);

                colorPanel.SetImageBitmap(bitmap);
                colorPanel.Touch += (object sender, TouchEventArgs e) =>
                {
                    Console.WriteLine("X:" + (int)e.Event.GetX());
                    Console.WriteLine("Y:" + (int)e.Event.GetY());
                    try
                    {
                        Color pixel = new Color(bitmap.GetPixel((int)e.Event.GetX(), (int)e.Event.GetY()));
                        int red = Color.GetRedComponent(pixel), green = Color.GetGreenComponent(pixel), blue = Color.GetBlueComponent(pixel);
                        Console.WriteLine("R:" + red + " G:" + green + " B:" + blue);
                    }
                    catch
                    {

                    }
                };

                rl.AddView(colorPanel);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以根据ImageView和原始绘图/位图(内在值)的大小缩放触摸X / Y坐标,将X / Y偏移当前边界,应该< / em>为您提供翻译到位图的触摸坐标(除非您已将某些类型的其他翻译或效果应用于您的位图)。

var drawable = (BitmapDrawable)colorPanel.Drawable;
colorPanel.Touch += (object sender, Android.Views.View.TouchEventArgs e) =>
{
    var intrinsicScaleX = (float)drawable.IntrinsicWidth / (float)drawable.Bounds.Width();
    var intrinsicScaleY = (float)drawable.IntrinsicHeight / (float)drawable.Bounds.Height();
    var viewScaleX = (float)drawable.IntrinsicWidth / (float)colorPanel.Width;
    var viewScaleY = (float)drawable.IntrinsicHeight / (float)colorPanel.Height;
    var x = (e.Event.GetX() - drawable.Bounds.Left) * intrinsicScaleX * viewScaleX;
    var y = (e.Event.GetY() - drawable.Bounds.Top) * intrinsicScaleY * viewScaleY;
    var pixel = drawable.Bitmap.GetPixel((int)x, (int)y);
    Console.WriteLine($"{viewScaleX}:{viewScaleY}::{e.Event.GetX()}:{e.Event.GetY()}:{x}:{y}:{Color.GetRedComponent(pixel)}:{Color.GetGreenComponent(pixel)}:{Color.GetBlueComponent(pixel)}");
};