Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Drag/Animation of Views

Tags:

android

I'm not a programmer, but I'm trying to learn android development, and having a blast.

Lately I've hit a fork in the road, and I don't know that any of the directions I can identify will meet all my needs. This is where you (the experts) can help ;)

Endpoint: I want to have the user touch a ball, and drag it to any location on the screen. I would also like to animate this ball when it gets drawn, and when it gets dropped.

I can accomplish the dragging part using a custom class (that doesn't extend anything... just a class like is found in this tutorial: basic drag & drop, however, I don't know how to apply the tween animation to it since it's not a view.

I have also developed an animated version of an ImageView with my image in it, however, I can't manage to apply the same drag & drop functionality without using AbsoluteLayout, which I know is a no-no.

So... how do I move an ImageView around a ??? layout using MotionEvents, or how do I animate (using tweens defined in XML) a non-view based custom class?

Please ask questions if this is not clear. I don't know all the terminology as well as most of you might.

There is also a copy of this question on the anddev.org forums, so I'll keep this post updated with any responses I get over there.

like image 810
Peter Avatar asked Mar 02 '10 14:03

Peter


People also ask

How to animate view in Android studio?

xml in the res/anim/ directory of the project, the following code will reference it and apply it to an ImageView object from the layout. ImageView spaceshipImage = (ImageView) findViewById(R. id. spaceshipImage);

What is fling animation?

Fling animation is an animation that continues an initial momentum (most often from gesture velocity) and gradually slows down. The fling animation will come to a stop when the velocity of the animation is below the threshold derived from DynamicAnimation.

What is fling gesture in Android?

For example, a fling gesture involves the user pressing a finger down on the screen, swiping across the screen, and lifting the finger up off the screen while the swipe is still in motion (that is, without slowing down to stop before lifting the finger).


1 Answers

Why can't you extend View? Then, you have complete control over how it draws because you can override the OnDraw() method. Just make the ColorBall class extend View. Change its position when you move and then invalidate just that one view and have it draw itself instead of having the DrawView class draw it.

Edit - Here is an example class

public class Card extends View
{
    private Bitmap mImage;
    private final Paint mPaint = new Paint();
    private final Point mSize = new Point();
    private final Point mStartPosition = new Point();

    public Card(Context context)
    {
        super(context);

    }

    public final Bitmap getImage() { return mCardImage; }
    public final void setImage(Bitmap image)
    {
        mImage = image;
        setSize(mCardImage.getWidth(), mCardImage.getHeight());
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        Point position = getPosition();
        canvas.drawBitmap(mCardImage, position.x, position.y, mPaint);
    }

    public final void setPosition(final Point position)
    {
        mRegion.set(position.x, position.y, position.x + mSize.x, position.y + mSize.y);
    }

    public final Point getPosition()
    {
        Rect bounds = mRegion.getBounds();
        return new Point(bounds.left, bounds.top);
    }

    public final void setSize(int width, int height)
    {
        mSize.x = width;
        mSize.y = height;

        Rect bounds = mRegion.getBounds();
        mRegion.set(bounds.left, bounds.top, bounds.left + width, bounds.top + height);
    }

    public final Point getSize() { return mSize; }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        // Is the event inside of this view?
        if(!mRegion.contains((int)event.getX(), (int)event.getY()))
        {
            return super.onTouchEvent(event);
        }

        if(event.getAction() == MotionEvent.ACTION_DOWN)
        {
            mStartPosition.x = (int)event.getX();
            mStartPosition.y = (int)event.getY();
            bringToFront();
            onSelected();
            return true;
        }
        else if(event.getAction() == MotionEvent.ACTION_MOVE)
        {
            int x = 0, y = 0;

            if(mLock == DirectionLock.FREE || mLock == DirectionLock.HORIZONTAL_ONLY)
            {
                x = (int)event.getX() - mStartPosition.x;
            }

            if(mLock == DirectionLock.FREE || mLock == DirectionLock.VERTICAL_ONLY)
            {
                y = (int)event.getY() - mStartPosition.y;
            }

            mRegion.translate(x, y);
            mStartPosition.x = (int)event.getX();
            mStartPosition.y = (int)event.getY();

            invalidate();

            return true;
        }
        else
        {
            return super.onTouchEvent(event);
        }
    }
}
like image 79
CaseyB Avatar answered Oct 27 '22 11:10

CaseyB