[mono-android] SurfaceView keeps disappearing when I try to get the colour that is on it (creating a colorpicker)
Paul Johnson
paul at all-the-johnsons.co.uk
Wed Jun 6 15:44:46 UTC 2012
Hi,
I'm creating a colorpicker for an art app but have hit a problem. I've
created a view that is consists of a linearview (horizontal) with a
pile of surfaceviews. Each surfaceview has an gradient fill set as the
background. Under that is an imageview that displays the colour I've
clicked on.
The problem is this. If I click on the first SurfaceView, the colour
appears in the imageview. If I click on any other SurfaceView it will
either switch places with the first SurfaceView *or* vanish
completely. The ImageView turns black.
The code I have looks like this
8-->
public class colorPicker : Activity, ISurfaceHolderCallback,
View.IOnTouchListener
{
ImageView imageView;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.colorPicker);
SurfaceView surfaceCP1 =
FindViewById<SurfaceView>(Resource.Id.colorPick1);
var holderCP1 = surfaceCP1.Holder;
holderCP1.AddCallback(this);
surfaceCP1.SetOnTouchListener(this);
SurfaceView surfaceCP2 =
FindViewById<SurfaceView>(Resource.Id.colorPick2);
var holderCP2 = surfaceCP2.Holder;
holderCP2.AddCallback(this);
surfaceCP2.SetOnTouchListener(this);
SurfaceView surfaceCP3 =
FindViewById<SurfaceView>(Resource.Id.colorPick3);
var holderCP3 = surfaceCP3.Holder;
holderCP3.AddCallback(this);
surfaceCP3.SetOnTouchListener(this);
SurfaceView surfaceCP4 =
FindViewById<SurfaceView>(Resource.Id.colorPick4);
var holderCP4 = surfaceCP4.Holder;
holderCP4.AddCallback(this);
surfaceCP4.SetOnTouchListener(this);
SurfaceView surfaceCP5 =
FindViewById<SurfaceView>(Resource.Id.colorPick5);
var holderCP5 = surfaceCP5.Holder;
holderCP5.AddCallback(this);
surfaceCP5.SetOnTouchListener(this);
SurfaceView surfaceCP6 =
FindViewById<SurfaceView>(Resource.Id.colorPick6);
var holderCP6 = surfaceCP6.Holder;
holderCP6.AddCallback(this);
surfaceCP6.SetOnTouchListener(this);
SurfaceView surfaceCP7 =
FindViewById<SurfaceView>(Resource.Id.colorPick7);
var holderCP7 = surfaceCP7.Holder;
holderCP7.AddCallback(this);
surfaceCP7.SetOnTouchListener(this);
SurfaceView surfaceCP8 =
FindViewById<SurfaceView>(Resource.Id.colorPick8);
var holderCP8 = surfaceCP8.Holder;
holderCP8.AddCallback(this);
surfaceCP8.SetOnTouchListener(this);
SurfaceView surfaceCP9 =
FindViewById<SurfaceView>(Resource.Id.colorPick9);
var holderCP9 = surfaceCP9.Holder;
holderCP9.AddCallback(this);
surfaceCP9.SetOnTouchListener(this);
SurfaceView surfaceCP10 =
FindViewById<SurfaceView>(Resource.Id.colorPick10);
var holderCP10 = surfaceCP10.Holder;
holderCP10.AddCallback(this);
surfaceCP10.SetOnTouchListener(this);
SurfaceView surfaceCP11 =
FindViewById<SurfaceView>(Resource.Id.colorPick11);
var holderCP11 = surfaceCP11.Holder;
holderCP11.AddCallback(this);
surfaceCP11.SetOnTouchListener(this);
SurfaceView surfaceCP12 =
FindViewById<SurfaceView>(Resource.Id.colorPick12);
var holderCP12 = surfaceCP12.Holder;
holderCP12.AddCallback(this);
surfaceCP12.SetOnTouchListener(this);
imageView = FindViewById<ImageView>(Resource.Id.colorDisplay);
imageView.SetBackgroundColor(Android.Graphics.Color.Red);
}
public bool OnTouch(View v, MotionEvent e)
{
int sv = v.Id;
int x = (int) e.GetX();
int y = (int) e.GetY();
double xbuf = 0;
double SizeX = (WindowManager.DefaultDisplay.Width / 12);
switch (sv)
{
case Resource.Id.colorPick1: xbuf = 0;
break;
case Resource.Id.colorPick2: xbuf = SizeX;
break;
case Resource.Id.colorPick3: xbuf = SizeX * 2;
break;
case Resource.Id.colorPick4: xbuf = SizeX * 3;
break;
case Resource.Id.colorPick5: xbuf = SizeX * 4;
break;
case Resource.Id.colorPick6: xbuf = SizeX * 5;
break;
case Resource.Id.colorPick7: xbuf = SizeX * 6;
break;
case Resource.Id.colorPick8: xbuf = SizeX * 7;
break;
case Resource.Id.colorPick9: xbuf = SizeX * 8;
break;
case Resource.Id.colorPick10: xbuf = SizeX * 9;
break;
case Resource.Id.colorPick11: xbuf = SizeX * 10;
break;
case Resource.Id.colorPick12: xbuf = SizeX * 11;
break;
}
#if DEBUG
Console.WriteLine("X = {0}, Y = {1}, sv = {2}, xbuf =
{3}", x, y, sv, xbuf);
#endif
Bitmap b = Bitmap.CreateBitmap(v.Width, v.Height,
Bitmap.Config.Argb8888);
Canvas c = new Canvas(b);
v.Layout(0, 0, v.Width, v.Height);
v.Draw(c);
int color = b.GetPixel(x, y);
#if DEBUG
Console.WriteLine("color = {0}", color);
#endif
imageView.SetBackgroundColor(Color.Argb(255,
Color.GetRedComponent(color), Color.GetGreenComponent(color),
Color.GetBlueComponent(color)));
return false;
}
<--8
The switch is in the second method as an attempt to position the
canvas correctly ie v.Layout((int)xbuf, 0, v.Width, v.Height).
Additional to this, is there a way to display the colour under the
pointer as the pointer moves?
Thanks
Paul
More information about the Monodroid
mailing list