开发者

How to manage a MotionEvent going from one View to another?

I have a SurfaceView that takes up part of the screen, and some buttons along the bottom. When a button is pressed and the user drags, I want to be able to drag a picture (based on the button) onto the SurfaceView and have it drawn there.

I want to be able to use clickListeners and the like, and not just have a giant SurfaceView with me writing code to detect where the user pressed and if it's a button, etc.

I have somewhat of a solution, but it seems a bit of a hack to me. What is the best way to accomplish this using the framework intelligently?

Part of my XML:

<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background">

<!-- Place 开发者_开发百科buttons along the bottom -->
<RelativeLayout android:id="@+id/bottom_bar"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="40dip"
    android:layout_alignParentBottom="true"
    android:background="@null">

    <ImageButton android:id="@+id/btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="@null"
        android:src="@drawable/btn_1">
    </ImageButton>

    <!-- More buttons here... -->
</RelativeLayout>

 <!-- Place the SurfaceView in a frame so we can stack on top of it -->
<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="0px"
    android:layout_weight="1"
    android:layout_above="@id/bottom_bar">

    <com.project.question.MySurfaceView
        android:id="@+id/my_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />
</FrameLayout>

And the relevant Java code in MySurfaceView, which extends SurfaceView. mTouchX and Y are used in the onDraw method to draw the image:

@Override
public boolean onTouchEvent(MotionEvent event){
    mTouchX = (int) event.getX();
    mTouchY = (int) event.getY();
    return true;
}

public void onButtonTouchEvent(MotionEvent event){
    event.setLocation(event.getX(), event.getY() + mScreenHeight);
    onTouchEvent(event);
}

Finally, the activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.my_surface);

    mView = (MySurfaceView) findViewById(R.id.my_view);
    mSurfaceHeight = mView.getHeight();
    mBtn = (ImageButton) findViewById(R.id.btn_1);
    mBtn.setOnTouchListener(mTouchListener);
}

OnTouchListener mTouchListener = new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        int [] location = new int[2];
        v.getLocationOnScreen(location);
        event.setLocation(event.getX() + location[0], event.getY());
        mView.onButtonTouchEvent(event);
        return true;
    }
};

Strangely, one has to add to the x-coordinate in the activity, then add to the y coordinate in the View. Otherwise, it doesn't show up in the correct position. If you add nothing, something drawn using mTouchX and mTouchY will show up in the upper left corner of the SurfaceView.

Any direction would be greatly appreciated. If I'm going about this completely the wrong way, that would be good information too.


I'm not sure if I completely understand what you're trying to do, but I'll try to give you some useful information anyway:

Is your main problem with the coordinates?

You could have an invisible view on top of everything (which is an ugly solution), or you could use some math. You get the coordinates of the view, which, together with information about the display size and total view size can give you all the information you need

As previously mentioned, I am not entirely sure where you run into problems, but in my opinion your approach seems fine.

Oh and one more thing:

You should try to make use of the MotionEvent called ACTION_MOVE with

float x = event.getX();
float y = event.getY();
switch(event.getAction()){

case MotionEvent.ACTION_DOWN:

    //DO SOMETHING

case MotionEvent.ACTION_MOVE:

    //DO SOMETHING using x and y.
}

within your onTouch.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜