c# - Xamarin 从位图中获取错误的颜色

标签 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);
            }
        }
    }

最佳答案

您可以根据 ImageView 的大小和您的原始可绘制对象/位图(固有值)缩放触摸 X/Y 坐标,将 X/Y 偏移当前边界和 应该给你翻译成你的位图的触摸坐标(除非你已经将某种类型的其他翻译或效果应用到你的位图)。

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

关于c# - Xamarin 从位图中获取错误的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43048834/

相关文章:

c# - 使用opencv c#检测图像中的文本 block

c# - SSL 握手超时

c# - 使用 JQuery 选择自动生成的 WebForm 控件 ID

java - LibGdx : Using setInputProcessor(stage) in different classes

java - 在没有 Android Studio 的情况下使用 gradle 构建 Android multidex 应用程序(在构建服务器上)

c# - UIViewController 没有被处置

xaml - 如何使用 Xamarin.Forms xaml 编辑器预览 ListView

c# - 在 VS2015 社区中使用 ASP.NET 5 时的 "Cannot resolve symbol ' 系统 '"和其他命名空间

c# - Universal App中的NavigationSevice

android调试桥兼容性问题