Android Drag/Animation of Views

前端 未结 1 1196
情书的邮戳
情书的邮戳 2020-11-30 21:59

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 dire

相关标签:
1条回答
  • 2020-11-30 22:40

    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);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题