change the position of a bitmap where ever i touch on screen
I have a large image on the screen and I want to display a small image on that image where I touch the screen. but I do not know how to change the position of the image when I touch on the screen and the small image must display where ever I touch on screen.
an开发者_StackOverflow社区y suggestions or hint will be appreciative.
thanks
Suppose you have your moving bitmap already in an ImageView which is part of your RelativeLayout. Whenever the user touches the screen, you just have to change the position of the ImageView, by changing its margins.
You should try something like this:
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN)
{
RelativeLayout.LayoutParams params = myImageView.getLayoutParams();
params.setMargins(event.getX(),event.getY(), 0, 0);
myImageView.setLayoutParams(params);
}
}
User first the method onTouchEvent() to get the position of your touch.
Then i suppose you have your Bitmap placed in a UIElement like for example ImageView? And if you're using AbsoluteLayout you can set the ImageView object at the same coordinates you reveiced in the ontouchEvent.
@Override
public boolean onTouchEvent(MotionEvent event) {
event.getX();
event.getY();
}
精彩评论