Solved

x y coordinates  animation

Posted on 2012-03-28
3
1,290 Views
Last Modified: 2012-04-17
I can display an animation frame by frame but it doesnt move.


If I have an image view , how do I get the x,y coordinates of the image ?
How do I set the new x y values of this image?

MY code works with frame anmation only.

private void animate() {
            ImageView imgView = (ImageView) findViewById(R.id.animationImage);
            // imgView.setVisibility(ImageView.VISIBLE);
            imgView.setBackgroundResource(R.drawable.ani);
            

            AnimationDrawable frameAnimation = (AnimationDrawable) imgView
                        .getBackground();

            if (frameAnimation.isRunning()) {
                  frameAnimation.stop();
                  b.setText("Start");
            
            } else {
                  frameAnimation.start();
                  b.setText("Stop");
            }
      }
0
Comment
Question by:jagguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 37781673
0
 
LVL 1

Expert Comment

by:garikapatisuresh
ID: 37782262
Based on your I understand that you want to move an object from one place to another.

I have created one sample and attached below. I think it will full fill your requirement.
AnimationSampleActivity.java
Ball.java
AndroidManifest.xml
0
 
LVL 8

Accepted Solution

by:
Santhana earned 500 total points
ID: 37785715
Hi jagguy,

1) I can display an animation frame by frame but it doesnt move.

You are using animation drawable to animate the ImageView.

It is used to create  frame-by-frame animations, defined by a series of Drawable objects, which can be used as a View object's background

http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html
It will not be used to move the view object.

2) how do I get the x,y coordinates of the image ? How do I set the new x y values of this image?

http://stackoverflow.com/questions/3619693/how-to-get-views-position

The geometry of a view is that of a rectangle.
A view has a location, expressed as a pair of left and top coordinates,
and two dimensions, expressed as a width and a height.

The unit for location and dimensions is the pixel.
It is possible to retrieve the location of a view by invoking the methods
getLeft()  - Returns the left, or X, coordinate of the rectangle representing the view.
getTop() - Returns the top, or Y, coordinate of the rectangle representing the view.
getRight() and getBottom() - These methods return the coordinates of the right and bottom edges of the rectangle representing the view.
calling getRight() is similar to the following computation: getLeft() + getWidth()
These methods both return the location of the view relative to its parent. For instance, when getLeft() returns 20, that means the view is located 20 pixels to the right of the left edge of its direct parent.
------------------------------------------------------------------------------------------------------------------------------

If you want to move the view by using Tranlate Animation  use following code

Animation lTraslateAnimation = new TranlateAnimation()0 , 0, 0, 500);
lTraslateAnimation.setDuration(2000);
lTraslateAnimation.setFillAfter(true);
lTraslateAnimation.setRepeatCount(-1);
lTraslateAnimation.setRepeatMode(Animation.REVERSE);
 imgView .startAnimation(lTraslateAnimation);

But here the problem is by using this TranslateAnimation we can move the views only in straight lines...
If you want to move views in customized way  like(Curve ,circle)...Then you need to use custom views and canvas , where you can pass x and y coordinates while drawing and invalidating the view. You need to override the following method while extending the View class. The changeViewCoordinates() method here can change the x,y coordinates in a curve.

public void onDraw(Canvas aCanvas){  
           aCanvas.drawBitmap(bitmap,x,y,null);  
          changeViewCoordinates();  
           invalidate()
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
There is a lot to be said for protecting yourself and your accounts with 2 factor authentication.  I found to my own chagrin, that there is a big downside as well.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question