开发者

How can I create a widget against a background image that is touchable and dragable?

开发者 https://www.devze.com 2023-01-14 16:26 出处:网络
I am trying to create an application that has a background image with widget like objects that can be touched and dragged.Think of a chess set app where you can touch and drag a chess piece to the tar

I am trying to create an application that has a background image with widget like objects that can be touched and dragged. Think of a chess set app where you can touch and drag a chess piece to the target square. I've poured over the samples but I just can't seem to get it. I thought widge开发者_Python百科ts might be the answer, but they are more for home screen like apps. I thought TouchRotateActivity in the samples would have the answer, but I can't get it to run so I can see if it behaves the way I want.

Any suggestions would be greatly appreciated.


You can start by creating a View and using a Drawable or Bitmap for the Touchable objects. In the onDraw(Canvas) method of the View you can "draw" your Touchable at the desired positions. Then override the onTouchEvent() to change the position of the Drawable. Here's a very basic example:

`

public class MyActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        RelativeLayout rl = new RelativeLayout(this);

        // The View
        SampleView myView = new SampleView(this);
        myView.setBackgroundColor(Color.LTGRAY);

        rl.addView(myView,
              new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
              RelativeLayout.LayoutParams.FILL_PARENT));

        setContentView(rl);
     }

    // The View for drawing and moving
    class SampleView extends View
    {
        private static final String TAG = "SampleView";
        // Track the Touch
        float mLastX = 0;
        float mLastY = 0;
        boolean mDragging = false;

        // Used to draw the square
        ShapeDrawable mDrawable = new ShapeDrawable(new RectShape());
        // Square size
        int mSquareWidth = 50;

        public SampleView(Context ctx)
        {
            super(ctx);
            this.mDrawable.getPaint().setColor(Color.RED);
        }

        @Override
        public void onDraw(Canvas canvas)
        {
            int x = (int)this.mLastX;
            int y = (int)this.mLastY;
            // Set the Bounds of the Square
            this.mDrawable.setBounds(x,y,x+mSquareWidth,y+mSquareWidth);
            // Draw it
            this.mDrawable.draw(canvas);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event)
        {
            int action = event.getAction();
            float x1 = event.getX(0);
            float y1 = event.getY(0);
            switch (action)
            {
                case MotionEvent.ACTION_DOWN:
                    // Check if you're touching in the Square. Only move the Square
                    // if we're touching in it.
                    Log.i(TAG,"DOWN");
                    if (this.mDrawable.getBounds().contains((int)x1,(int)y1))
                    {
                        Log.i(TAG,"IN BOUNDS");
                        this.mDragging = true;
                        this.mLastX = x1;
                this.mLastY = y1;
                    }
                    break;
                case MotionEvent.ACTION_MOVE:
                    if(this.mDragging)
                    {
                        Log.i(TAG,"DRAGGING");
                        // If we're dragging it, offset the draw points
                        this.mLastX = x1;
                        this.mLastY = y1;
                        this.invalidate();
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    // Stop dragging
                    this.mDragging = false;
                    break;
            }
        return true;
      }
   }
}

`

0

精彩评论

暂无评论...
验证码 换一张
取 消