How To Allow User To Drag And Drop An Image Within A Linearlayout
Solution 1:
Here is a colorpicker widget for your site. I dont know if this is exactly what your looking for but I definetly hope it helps!
<divstyle="font-family:Arial,Helvetica,sans-serif;border:solid 1px
#cccccc;position:absolute;width:240px;height:326px;background: #ffffff;-moz-box-shadow:0 0
6px rgba(0,0,0,.25);-webkit-box-shadow: 0 0 6px rgba(0,0,0,.25);box-shadow:0 0 6px
rgba(0,0,0,.25);-moz-border-radius: 5px;-webkit-border-radius:5px;border-radius:5px;"><divstyle="background-color:#2d6ab4;position:absolute;top:0px;left:0px; width:100%;
height:22px;text-align:center;padding-top:2px;font-weight:bold;border-top-right-
radius:5px;border-top-left-radius:5px;"><astyle="text-decoration:none;color:#ffffff;"target="_blank"href="">Color Picker</a></div><scriptsrc="http://widget.colorcodehex.com/color-picker/abcdef.html"type="text/javascript"></script></div>Solution 2:
How can I allow the user to drag and drop the small square image within the layout ONLY and also retrieve the X and Y values?
- Check out this tutorial for an idea on drag and drop of a ui element
Whenever, the user presses inside the layout, the square moves to that coordinate within the layout. How can I do that?
- Furthermore from the tutorial,
llColorSpectcan also implement aMotionEvent.ACTION_DOWNevent to moveivSquareto thex,yof theMotionEvent
I want to then use those X and Y values to get the HEX/RGB values. How would I be able to accomplish it?
- This is going to be more tricky. You will require some lookup that translates the
x,yvalues into the HEX or RGB values. You can getView.getMeasuredWidth()andView.getMeasuredHeight()ofllColorSpectto find out the dimensions that it appears on screen. Say for example the width of the spectrum range is from 0-1000 andllColorSpect'smeasured distance is from 0-3000, user clicks 300, then you'll know thatspectrumLookup(300/3) == 100so you'd use the spectrum color @ 100.
Good luck!
Solution 3:
I suggest using a Canvas for your image as this would allow you to draw the square to select the colour over the colour spectrum.
The code below uses a canvas that overrides onTouchEvent and will give you be able to give you the coordinates of where the user presses.
publicclassCanvasViewextendsView {
private Bitmap bitmap;
private Bitmap square;
privatefloatmScaleFactor=1f;
intx=0;
inty=0;
publicCanvasView(Context c) {
super(c);
bitmap= BitmapFactory.decodeResource(c.getResources(), R.drawable.colour);
square = BitmapFactory.decodeResource(c.getResources(), R.drawable.square);
}
@OverrideprotectedvoidonSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@OverrideprotectedvoidonDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.scale(mScaleFactor, mScaleFactor);
canvas.drawBitmap(bitmap, 0, 0, null);
canvas.drawBitmap(square, x, y, null);
}
@OverridepublicbooleanonTouchEvent(MotionEvent event) {
floatx= event.getX();
floaty= event.getY();
Log.d("x and y", "X: " + x + " Y: " + y);
intpixel= bitmap.getPixel((int)x,(int) y);
intredValue= Color.red(pixel);
intblueValue= Color.blue(pixel);
intgreenValue= Color.green(pixel);
Log.d("Colours","R:" +redValue +" B:" + blueValue + " G:" +greenValue);
//Draw onto the square onto imagethis.x = (int) x;
this.y = (int) y;
invalidate();
returntrue;
}
}
To use this CanvasView class simply do this or edit it for your own layouts:
CanvasViewcanvasView=newCanvasView(this);
LinearLayoutlinearlayout= (LinearLayout) findViewById(R.id.linearlayout);
linearlayout.addView(canvasView);
Finally the function to map the x and y to a colour. Simply read the pixel colour of the coordinates.
Post a Comment for "How To Allow User To Drag And Drop An Image Within A Linearlayout"