2017-03-27 1 views
1

저는 Android에서 색상 선택 도구를 만들려고 노력하고 있습니다. 그러나 내가 잘못하고있는 것을 잘못 이해하고 있습니다. ImageView가 있고이 이미지를 비트 맵으로 디코딩합니다. 비트 맵에서 GetPixel() 메서드를 사용하고 있지만이 시점에서 뭔가 잘못하고 있다고 생각합니다. 누군가 내가 뭘 잘못하고 있는지 말할 수 있습니까? 그래서 아마 해상도가 문제가,Xamarin이 비트 맵에서 잘못된 색상을 얻었습니다.

enter image description here

내가 Z1 컴팩트에서 테스트하고 있습니다 (720x1280 해상도, 안드로이드 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); 
      } 
     } 
    } 
+0

어떤 색상이 좋으며 어떤 색상을 얻나요? 수치주세요! X, Y로 시작합니다. – greenapps

답변

0

당신은 확장 할 수있는 터치 X/Y는 상기 ImageView의 크기와 원래 드로어 블/사진 비트 맵 (고유 값), 현재의 경계에 의해 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)}"); 
}; 
+0

고마워요! 그것은 작동합니다 :) 나는 "x, y"좌표의 크기를 조정해야한다는 것을 전혀 모릅니다. 이미지 크기와 관련이 있다는 것이 확실했습니다. – Salin

관련 문제