查看Android的拖/动画 [英] Android Drag/Animation of Views

查看:100
本文介绍了查看Android的拖/动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是一个程序员,但我努力学习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.

我还开发了一个动画版的ImageView的与我在里面的形象,但是,我不能管理应用相同的拖动和放大器;拖放功能,而无需使用AbsoluteLayout,这是我所知道的是一个没有没有。

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.

所以...我怎么走动的ImageView的???使用MotionEvents布局,或者我怎么动画(使用XML定义的补间)的自定义类是非观?

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.

还有href="http://www.anddev.org/need_advice_on_layouts-views-drag-animation-t11648.html"> 这个问题复制在anddev.org论坛上

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.

推荐答案

你为什么不能扩展视图?然后,你可以完全控制它是如何吸引,因为你可以覆盖的OnDraw()方法。只要在ColorBall类扩展视图。当您移动改变其位置,然后失效只是一个视图,并将它绘制具有drawView函数类绘制它的本身,而不是。

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.

编辑 - 下面是一个例子类

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);
        }
    }
}

这篇关于查看Android的拖/动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆