Draw image in activity android - android

I have a activity where I have two imageViews. In onTouch I recognize which imageView was clicked. Now I have a question. How I can draw new very small image in place where I clicked. I know that onTouch i have getX() and getY() and I save this position when I touch the screen, but I don't know how I can draw image in place where I clicked. My image is in drawable resources. I need to use canvas or is better way. I need draw this picture and after that I need run animation this image.
edit1: How can I set positionX and positionY my image. I have position where I click but I don't know how I can set image in this coordinates.

you should have a image view with visible attribute of FALSE and after touch (or any event you want) set three attribute : visible = true; x = getX() and y = getY();
is it enough or explain more?

I see the following approach.
Replace the ImageView by a custom view derived from ImageView.
Override onDraw.
call super.onDraw to draw the image
paint your image using onDraw's canvas at the last touch position.
each time you get a touch, you need to invalidate the custom view

Related

making a view move from one coordinate to another coordinate in android studio

I have an image view and a photoview in android studio. The photoview is set to an image bitmap. I want to initially place a view on a specific x, y coordinate on top of the photoview but I want to place the view depending on the matrix of the image bitmap, not the photoview. Then I want to make the image view translate from 1 x, y coordinate to another coordinate. what is the best way to do this?
Your question is a little vague but looking at the title of the question, to move a view to the coordinates of another view, you could set the x and y to the values of the view you want to move to. Let's say you want to move imagview1 to the point where imageview2 is:
imageview1.animate().x(imageview2.getX()).y(imageview2.getY());
Or if you don't wanna animate it just remove animate():
imageview1.setX(imageview2.getX());```
Could you elaborate more or in this case, clearly

How to draw an arrow instead of a dragshadow?

I need the user to drag an arrow between two views, but all info I can find online is to make a dragshadow.
here's an exemple of the result i need.
https://imgur.com/pfQxtSK
thanks in advance.
What you want requires you to observe touch events inside of your parent ViewGroup, or custom view (whatever is drawing the light-blue grid from the image), using onTouchEvent(MotionEvent) callback. Next, you wanna determine the direction of the drag using the MotionEvent's x and y coordinates and some basic trigonometry. This will allow you to determine the angle of the drag and therefore dynamically the adjust the angle (rotation) of your arrow so that it accurately tracks the finger. Next use the same x and y coordinates you got from the MotionEvent to determine where on the canvas to draw the arrow. If the arrow is another view added as a child, you can set that view's x and y coordinates or translate the view using methods provided by the View class (you may need to play with offset's so the view doesn't get drawn off-screen after several drags/touches). If the arrow is a bitmap our drawable, you will need to adjust the draw coordinates in your view's onDraw(Canvas) method.

Touch event on blended layer image Android

I have a precut image i.e One base image and multiple transparent layer of the the base image.
I am trying to add the layer image on top of base image to select the layer and apply different color on it.
I have tried the following way to achieve it but not able to finish.
ImageView - I am overlapping imageview with transparent layer image. It shows the blended image but the touch event detects the finally overlapped image id always. Because I draw the image with fill parent, all the layer image is also the same
Layer Drawable - It can allow only drawable images, but my use case is to load the precut from gallery or other resource. Even I can't select layer on touch.
GPUImage library - The image is not showing full imageview.
Regards
Sathiya
Extend the View class, make the onDraw() function draw what you want to draw. The onTouchEvent() function implement the touch events. What you're trying to do isn't subsumed in any view already made but it's pretty easy to make a view. Just draw the bitmaps on the canvas in the correct order with the correct paints with the right transparency.
#Override
protected void onDraw(Canvas canvas) {
int numLayers = mLayers.size();
for (int i = 0; i < numLayers; i++) {
canvas.save();
canvas.concat(matrix);
//or
canvas.translate(rect.left,rect.top);
Bitmap b = mLayers.get(i);
canvas.drawBitmap(b, paint);
canvas.restore();
}}
You end up doing this a bunch just adding bitmaps to the canvas in whatever places and order as needed.

How to check touch on a bitmap

I want to know if there is a way to check if the user touched the bitmap, ofcourse im talking about an image without background, like a circle, a triangle and so on.
Thanks!
image without background, like a circle, a triangle and so on
then you should probably override View's onTouch, retrieve x and y from the touch event you got as parameter and compare it with the coords of the shape you drawn. The easiest way is to use Rect.contains(int x, int y)

How to apply animation effect to a View without using time interpolation

I am trying to change View's position, scale and alpha with touch events (at the time). When user touches a view and makes some movement the view should be effected. It is like applying combined animation via touch distance instead of time.
I can also calculate interpolated value for distance. For example if I move my finger half of the target distance I have the interpolation value 0,5.
Actually I could achieve this by changing the margins around the view but it only works with ImageView. Otherwise the position of the elements change during applying the effects. (Like TextViews in RelativeLayout)
So I thought that I can create a combined animation and use my own interpolated value instead of the time interpolation. I hope it is possible.
I would do it following way:
Create custom FrameLayout and call setWillNotDraw(false);
Load the image as Bitmap to be drawn
Catch touch event's inside the FrameLayout
Implement proper drawing and transformation(position, alpha, scale) in onDraw method for the Bitmap

Categories

Resources