Now I have an ImageView and its a circle which is at the slightly below than center position (But this should not matter).
I have written code onTouch of ImageView for ACTION_DOWN,ACTION_UP ,now consider as user have put finger on circle and move and move.... I want to active some code when user move finger and exceed the region of CIRCLE image(As soon as user exceed the region the code should be ececuted onlly once)
Here is my code
ImageView view1 = (ImageView) findViewById(R.id.fccircledetectionarea);
view1.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
ImageView imageView=(ImageView) findViewById(R.id.fccircledetectionarea);
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
final float x=imageView.getTop();
Toast.makeText(PlayScreen.this, "Top Position:"+x, Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
Toast.makeText(PlayScreen.this, "Over", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_MOVE:
Toast.makeText(PlayScreen.this, "Over", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
});
I cannot archive my goal through ACTION_MOVE: as it start to work if user move finger within the circle
And my second query is that How to set alpha of this imageview I have used
view1.setAlpha(0);
But is not working and I have also made this imageview invisible but than my onTouch code is not working
Within your onTouch method record a boolean to check if the user is touching within the bounds of the circle.
boolean inCircle = java.lang.Math.sqrt(x*x + y*y) <= circleRadius
Where x is taken from event.getX() - circleCenterX and y is taken from event.getY() - circleCenterY (touch position's offset from center of the circle)
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 would like to display an .jpg image into the screen and add Listener to this image which will can detect the location of tap on that image. I would like to bind XY axis to the image.
Example:
To display an image I should use:
private ImageView imgView;
imgView = (ImageView) findViewById(R.id.imageViewId);
imgView.setImageBitmap(BitmapFactory.decodeFile("pathToImageFile"));
To add listener to the image
iv.setOnClickListener(clickListener);
But how to define that listener which will detect the click coordinates (x,y) but the coordinates should be binded to the image not to the screen.
ImgeView.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
//Finger placed on screen
event.getX();
event.getY();
break;
case MotionEvent.ACTION_MOVE:
//Finger is moving
event.getX();
event.getY();
break;
case MotionEvent.ACTION_POINTER_DOWN:
//Second finger is touching screen
break;
}
return false;
}
});
I have a GridView with images. On long click the screen gets dark (with black half transparent image that gets visible) and another image become visible that has the same resource of the image long clicked from the grid.
What i want is to be able to drag that image (which i have succeeded), but the image shows at the top corner (as i designed the layout in the XML) and I want it to show up where I clicked (To be precised, i want the center of the dragable picture to be where I perfumed the long click).
My image is fill_parent on FrameLayout and is set on matrix scale to control position...
this is the image part of the XML:
<ImageView
android:id="#+id/imgMainSelectedMovie"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="matrix"
android:visibility="invisible"
android:src="#drawable/escape" />
and this is the OnTouch() part:
public boolean onTouch(View v, MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
X = ev.getX();
Y = ev.getY();
if (longClicked) return true;
break;
case MotionEvent.ACTION_MOVE:
if (longClicked) {
if (!dragStarted) {
dragStarted = true;
myImageView.setX(X);
myImageView.setY(Y);
}
matrix.postTranslate((ev.getX() - X), (ev.getY() - Y));
X = ev.getX();
Y = ev.getY();
myImageView.setImageMatrix(matrix);
return true;
}
break;
case MotionEvent.ACTION_UP:
if (longClicked) {
dark.setVisibility(View.INVISIBLE);
myImageView.setVisibility(View.INVISIBLE);
longClicked = false;
return true;
}
}
return false;
}
thist is what I tried to solve the problem:
if (!dragStarted) {
dragStarted = true; //Changed to true when OnItemLongClick called
myImageView.setX(X);
myImageView.setY(Y);
}
But it only changes the "start point" of the image layout to the entered X and Y.
Problem solved :)
What I did is when onItemLongClick calls I took the X and Y measured from the ACTION_DOWN case in the onTouch, took the matrix current location and did postTranslate to the coordinates from the ACTION_DOWN minus the location of the image.
this is the solution part from the onItemLongCliick:
matrix.getValues(values);
matrixX = values[Matrix.MTRANS_X];
matrixY = values[Matrix.MTRANS_Y];
matrix.postTranslate(X-matrixX, Y-matrixY);
myImageView.setImageMatrix(matrix);
I'm wondering why they didn't put some kind of "setPlace" method for matrix...
Here is my scenerio. Wherever you touch my screen, an circle is drawn, and when you lift your finger/stylus of the screen, the circle disappears. This is fine, but the problem now is if you touch, then move, the circle will not update to the new position of your finger/stylus. It will just stay at the original touch position regardless of movement until you lift your finger/stylus off the screen.
How am I able to get the circle(cursor) to redraw or move itself to the new position of your finger/stylus? Here is my code:
rLO.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
cursor.x = (int)event.getX() - (cursor.radius / 2);
cursor.y = (int)event.getY() - (cursor.radius / 2);
cursor.onDraw(cursor.e);
rLO.addView(cursor);
break;
case MotionEvent.ACTION_UP:
rLO.removeView(cursor);
break;
case MotionEvent.ACTION_MOVE:
}// end switch
return true;
}
});
Thanks in advance!
i am developing game.i am displaying gun object center bottom of the screen.when user tap on screen i need to rotate gun that direction.i done rotating image.but when user tap on screen i need to rotate image that direction.can you please help me
Thanks in advance
Aswan
is your image a bitmap? could you convert it to one? Would an onClick listener not work and when the user clicks do something like.. http://www.anddev.org/resize_and_rotate_image_-_example-t621.html
Seeing some code as to what you've attempting would be nice also. The following will help you with checking if the bitmap has been clicked in case you're stuck on that also http://developer.android.com/reference/android/view/View.html .
I've never attemped this myself but I think that would work. Try redrawing the bitmap when you have resized it.
Just use onTouchListener for your View and use this code for that
#Override
public boolean onTouch(View v, MotionEvent event) {
float currentX = event.getX();
float currentY = event.getY();
Log.i(TAG, "action type is"+event.getAction());
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: {
Log.i(TAG, "Entering in onTouch");
double rotationAngleRadians = Math.atan2(currentX - dialer.centerX, dialer.centerY - currentY);
dialer.rotationAngle = (int) Math.toDegrees(rotationAngleRadians);
Log.i(TAG, "rotaion angle"+dialer.rotationAngle);
dialer.invalidate();
return true;
}
}
return true;
}