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;
}
Related
I'm kinda trying to clone the ripple effect found in Google's material design. Now, I've noticed that the ripple always starts from the position that was touched. For this, I've created a drawable which will be initially invisible and but will become visible once the view is touched. The ImageView containing the drawable will be then moved to the position that was touched and the ripple animation will start thereafter. Now, while trying to do this, I'm stumbling upon one thing which is I can't figure out how to find out the position that was touched. I came across several questions on stackoverflow which said that the touch position can be found out by using the following code.
#Override
public boolean onTouch(View v, MotionEvent e) {
int X = (int)(e.getX());
int Y = (int)(e.getY());
return false;
}
I did try to implement this code in my app but I ended up with a NullPointerException. Please tell me how to find the exact location of the screen that was touched and move the view there?
Use like this:
#Override
public boolean onTouch(View v, MotionEvent e) {
if(e.getAction() == MotionEvent.ACTION_DOWN) { //ACTION_DOWN for the position touched.
int X = (int)(e.getX());
int Y = (int)(e.getY());
}
return false;
}
You should get position for ACTION_DOWN event and then move your view
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
float x = event.getX();
float y = event.getY();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
I'm working on an Android app and I want to add some eyecandy to the UI. I have an activity (let's call it MainActivity) which has some text fields, buttons and a gallery on it.
What I would like to achieve is: When the user touches somewhere on this activity, there should be some visual effect at the point where he touched (e.g. something like sparkles etc.).
So the essential parts of my question are:
a) How can I determine where the user touched
b) how can I draw my effects on to the screen (as an 'overlay' to the activity).
Thanks in advance for every helpful answer.
(I've checked out this answer but it doesn't seem to be applicable to my situation. )
First, you have to add onTouchListener to your root Layout, then you can get coordinates, where the user has touched the screen.
example :
float x,y;
rl = (RelativeLayout) findViewById(R.id.root_layout);
rl.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
return super.onTouchEvent();
}
});
You should override the onTouchEvent method of your activity. You can then obtain the coordinates of the touch event:
#Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// touch down
break;
case MotionEvent.ACTION_MOVE:
// movement
break;
case MotionEvent.ACTION_UP:
// touch up
break;
default:
// default
}
return super.onTouchEvent(event);
}
To answer further on your question, to have a overlay, look at the APIDemos -> Graphics -> OpenGL ES -> Translucent GLSurfaceView. This will help you to create overlay on your activity. There may be some other example in API demos which will get you some help. Android's API Demos are good set of examples to address some known issues.
If you are working on ICS then I recommend you to look at the Developer Options in Settings application. If you are not working with ICS, I believe you can look at the APIDEMOS for touchevents. They draw lines on touches. The code in that, can get you started.
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.
hi i need to rotate an ImageView in my app on touch
and i am using the following code
public android.view.View.OnTouchListener onTableTouched = new android.view.View.OnTouchListener(){
public boolean onTouch(View v, MotionEvent evt) {
WrapMotionEvent event = WrapMotionEvent.wrap(evt);
ImageView view = (ImageView) v;
// Dump touch event to log
dumpEvent(event);
//
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_POINTER_DOWN:
mode = NONE;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
if(mode == NONE){
updateRotation();
}
break;
}
return true;
}
};
private void updateRotation()
{
matrix.postRotate(10);
Bitmap redrawnBitmap = Bitmap.createBitmap(itembmp, 0, 0,itembmp.getWidth(), itembmp.getHeight(), matrix, true);
itembmp=redrawnBitmap;
image.setImageBitmap(itembmp);
}
but by using this i can rotate the image in clock-direction only.
But i need to rotate the image in both directions .
How to do that.
I would make two functions, one called updateRotationClockwise, one called updateRotationCounterClockWise.
In clockwise, it should stay:
matrix.postRotate(10);
In counterclockwise, it should say:
matrix.postRotate(350);
I am in process of developing this logic.
1. Difference in angle. one needs to also check other boundary conditions for more accuracy.
2. Instead of handling touch actions. I used in build ScaleGestureDetector class for information.
In onScaleBegin assuming I get the event using a setter (called in on touch) in the OnScaleGestureListener.
before=gety/getx;
In on ScaleEnd
after=gety/getx;
thus difference is tan-1(before)-tan-1(after)
if +tive anticlockwise -tive clockwise.
It is not so accurate I am adding conditions but one can try this out.
from the class ClockView you can find the method for rotation for your bitmap image
https://github.com/jselbie/xkcdclock
I am developing an small application in a android. Before this i had done surfing on internet but i can't get useful material. In this application i have two tabs I had made the these two tabs but I want after click on first tab i want some images these Images can move on right side when user touch it on right and left side when user touch it left means user can slide the images on both left and right by finger. Can anyone suggest me any tutorial or source code so that i can able to do functionality like this.
Thanks in advance
See this blog post on how to do it. You need to look at SurfaceView and the onTouchListener. Taken from the blog:
surf.setOnTouchListener( new SurfaceView.OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
case MotionEvent.ACTION_MOVE:
if( moving ){
final int x_new = (int)event.getX();
final int y_new = (int)event.getY();
mDrawTiles.draw( new DrawLogic(){
#Override
public void draw(Rect _surface) {
mTiles.setBounds(
x_new - mDrawWidth/2,
y_new - mDrawHeight/2,
x_new + mDrawWidth/2,
y_new + mDrawHeight/2);
}
});
}
int x1 = (int) event.getX();
int y1 = (int) event.getY();
imageobject.layout(x1,x1+(width_of_imageobject),y1,y1+(width_of_imageobject));