Solved

x y coordinates  animation

Posted on 2012-03-28
3
1,175 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
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:
santhanasamy 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

The case of the missing phone talks about the way a small electronic gadget (the mobile phone) has penetrated into our lives and has made us addicted to it.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now