【发布时间】:2017-03-27 14:14:12
【问题描述】:
我正在尝试在 android 上制作颜色选择器,但我无法弄清楚我做错了什么,我得到了错误的颜色。我有一个 ImageView,我正在将此图像解码为位图。在位图中,我使用了GetPixel() 方法,但我认为此时我做错了什么。有人能告诉我我做错了什么吗?这是我正在使用的图像:
我在 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);
}
}
}
【问题讨论】:
-
你期待哪种颜色,你会得到哪种颜色?请数字!从 X,Y 开始。
标签: c# android xamarin bitmap xamarin.android