I've spent some time researching this and still haven't found a great answer.
What I would like is to drag an ImageView around the screen. Not a drag and drop, but just an actual drag.
This tutorial has been helpful:
http://code.tutsplus.com/tutorials/android-gesture--mobile-2239
It works perfect, but it's designed to drag a bitmap, not an ImageView. I've tried replacing the Bitmap with an ImageView, but I'm not sure how the Canvas and ImageView interact in the onDraw method. I've tried imageView.draw(canvas); and that doesn't seem to work - the ImageView doesn't show up at all.
Bitmaps are not idea, because you can't add borders to them like you can with an ImageView.
I'd also like to be able to fling the ImageView off the screen.
Any suggestions?
Thanks in advance.
You can create RelativeLayout that match all your screen. In the layout create ImageView.
Set OnTouchListener listener to your relative layout to handle your finger coordinate.
After that you can write something like this :
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_MOVE)
{
move(event);
}
}
public void move(MotionEvent event)
{
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mImageView.getLayoutParams();
layoutParams.topMargin = event.getY();
layoutParams.leftMargin = event.getX();
mImageView.setLayoutParams(layoutParams);
}
Like using method move(MotionEvent event) you can drag your imageView where you want.
I hope this help you!
Related
Im facing the next 2 problems:
1) When I translate an imageview from outside the screen to inside the screen, when the image is moving the image is cropped, and when finishes moving the imageview fixes and shows ok. Why this is happening?
2) I use translateAnimation to move an imageview for example from (0,0) to (300,0).
I have implemented the on touch listener. When I try to move the imageview that translate before, when I try to move with the finger the ontouch not works, but if a touch with the finger in the position (0,0) the imageview moves but the image not shows! I don’t understand what is happening.
I try to move the imageView with the finger in the position (300,0) but not work, and when i touch in the position (0,0) moves. But if i move for example 100 in x axis, the imageview is not in position (100,0), its in (400,0)!! i dont understand what is happening. Something is missing, i think the imageview saves the position that start to move or someting, i dont understand. If i remove the translate animation to that imageview, the ontouch works OK!
I hope I was clear.
Here is my code for problem 1:
public void showImageFromOutside(ImageView image){
image.setImageResource(obtenerImagen(cartasJugador.get(0).toString()));
TranslateAnimation animation1 = new TranslateAnimation(100, 425, 100, 525);
animation1.setDuration(800);
animation1.setFillAfter(true);
animation1.setAnimationListener(animationListener);
image.startAnimation(animation1);
}
Here is my code for problem 2:
public void moveImageView(Imageview image){
animation = new TranslateAnimation(0, 300, 0, 0);
animation.setDuration(250);
animation.setFillAfter(true);
animation.setAnimationListener(animationListener);
image.startAnimation(animation);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
seMueve = true;
break;
case MotionEvent.ACTION_MOVE:
if (seMueve) {
x = (int) event.getRawX() - v.getWidth()/2;
y = (int) event.getRawY() - v.getHeight();
v.setX(x);
v.setY(y);
}
break;
case MotionEvent.ACTION_UP:
seMueve = false;
break;
}
v.performClick();
return seMueve;
}
Greets
Takes a look:
http://developer.android.com/guide/topics/graphics/prop-animation.html#property-vs-view
especially to
...another disadvantage of the view animation system is that it only modified where the View was drawn, and not the actual View itself. For instance, if you animated a button to move across the screen, the button draws correctly, but the actual location where you can click the button does not change, so you have to implement your own logic to handle this.
I think that you can not use onTouchListener with Animation. You shoulds use ObjectAnimator, its easy.
If your API is lower than 11 you can try with http://nineoldandroids.com/.
Regards
I am currently working on galaxy s4 app, in which i want moving image with moving finger, but how to implement this onHoverListener. Please Help.
And thanks in advance.
you can implementing onTouchListener() to your imageView like below:
img = ((ImageView) findViewById(R.id.img_arrow));
img.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent motionEvent) {
int rawX = (int) motionEvent.getX();
int rawY = (int) motionEvent.getY();
if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
TranslateAnimation anim = new TranslateAnimation(rawX, rawY, 0, 0);
anim.setDuration(500L);
img.startAnimation(anim);
}
return true;
}
});
Here we are applying TranslationAnimation to our imageView img
Instead of hoverListener implement touch listener on ur image and also implement touchevent in ur activity. then u can use OnTouchEvent's actionMove.
when user doing actionMove find the coordinates using motionEvent object and translate ur view to those coordinates using translate animation.this will give look as if u r moving the image.
I have to draw text annotations on Image. It should also have choice of moving the text over the Image. Is it possible to move the text by using the Canvas? I searched google and found a suggestion to use a TextView on ImageView and take a screenshot. But when I move the TextView, it is not proper. I mean if I touch the textview, then it is moving somewhere, it is not being exactly below the finger.
One way to do this, is to create a custom view and set a bitmap with Your picture to canvas:
canvas.drawBitmap(yourBitmap, new Rect(0,0,100,100), rectangle, null);
and then draw text:
canvas.drawText("Your Text", textX, textY, paint);
and in the custom view´s onTouch:
public boolean onTouch(View view, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
textX = event.getX();
textY = event.getY();
invalidate();
return true;
}
return super.onTouchEvent(event);
}
With that code You are moving only the text. By moving a textView like You described, it depends on the textView´s behavihour how the text is aligned inside the textView.
I cannot test this code for now, but it should give You an idea how to do it
How to make picture in a Canvas onTouch, like a button. Just to be press and nothing else. I can insert picture in Canvas, now how can I make her onTouch? Can somebody pls give me easy exsample? Dont be mad, cuz this is probably stupid and easy question
Thank you
public boolean handleTouch(MotionEvent event)
{
float newX = event.getX();
float newY = event.getY();
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
if (isInRect((int) newX, (int) newY))
{
refreshView();
}
//....
}
}
You cannot add an onClick or onTouch listener to something you've drawn with the canvas. These listeners can only be applied to views.
Another method will be to apply the onTouchListener to the view, and find the touch position to find whether the touch was on the pictures position.
I have a large image on the screen and I want to display a small image on that image where I touch the screen. but I do not know how to change the position of the image when I touch on the screen and the small image must display where ever I touch on screen.
any suggestions or hint will be appreciative.
thanks
Suppose you have your moving bitmap already in an ImageView which is part of your RelativeLayout.
Whenever the user touches the screen, you just have to change the position of the ImageView, by changing its margins.
You should try something like this:
#Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN)
{
RelativeLayout.LayoutParams params = myImageView.getLayoutParams();
params.setMargins(event.getX(),event.getY(), 0, 0);
myImageView.setLayoutParams(params);
}
}
User first the method onTouchEvent() to get the position of your touch.
Then i suppose you have your Bitmap placed in a UIElement like for example ImageView? And if you're using AbsoluteLayout you can set the ImageView object at the same coordinates you reveiced in the ontouchEvent.
#Override
public boolean onTouchEvent(MotionEvent event) {
event.getX();
event.getY();
}