拖动ImageView的或位图触摸后 [英] Drag ImageView Or Bitmap Upon Touch
问题描述
我现在有什么工作,我可以在屏幕上水平拖动一个框(我想要它做的事)。但是,当您单击任意位置在屏幕上它的工作原理,我要被点击的盒子时,它只能工作。我曾尝试用不同的方式实现这一点,我已经看了所有的地方,但仍保持丢失。任何人可以帮助?我也想不出一个位图如何放置(我使用位图,现在,我不能为我的生活弄清楚如何实现我的SurfaceView内ImageView的)。如果我说我的位图放置在0,0的意愿,根据其左上角0,0放置位图?我也有停止的盒子,当它达到了一个边缘的算法,但我只好重写,因为我必须将其删除。请,如果你可以提供你的知识,我将不胜AP preciate它
公共类BoardView扩展SurfaceView实现SurfaceHolder.Callback {
上下文mContext; 私人BoardThread螺纹;
私人浮动box_x = 140;
私人浮动box_y = 378;
ImageView的I =(ImageView的)findViewById(R.id.box_view); 位图箱=
(BitmapFactory.de codeResource
(getResources(),R.drawable.box));
私人浮动boxWidth = box.getWidth();
私人浮动boxHeight = box.getHeight(); 公共BoardView(上下文的背景下){
超级(上下文);
// surfaceHolder规定,我们在画布上绘制
。getHolder()的addCallback(本);
//控制图纸
线程=新BoardThread(getHolder(),这一点);
//拦截触摸事件
setFocusable(真); }
@覆盖 公共无效的onDraw(帆布油画){
canvas.drawColor(Color.WHITE);
//抽奖箱,并设置起始位置
canvas.drawBitmap(盒,box_x - (boxWidth / 2),
box_y - (boxHeight / 2),空);
} @覆盖
公共布尔onTouchEvent(MotionEvent事件){ //布尔模式= FALSE; 如果(event.getAction()== MotionEvent.ACTION_DOWN){
// INT X =(INT)event.getX();
// INT Y =(int)的event.getY(); //如果(X GT; box_x&放大器;&放大器; X&下; box_x + 29&放大器;&放大器y与其所连接; box_y&放大器;&放大器; Y&下; box_y + 30){
//模式=真实的;
box_x =(int)的event.getX();
//}
} 如果(event.getAction()== MotionEvent.ACTION_MOVE){
// INT X =(INT)event.getX();
// INT Y =(int)的event.getY(); //如果(模式==真){
box_x =(int)的event.getX();
//}
} 无效(); 返回true;
}
@覆盖
公共无效surfaceChanged(SurfaceHolder持有人,
INT格式,诠释的宽度,高度INT){ }
@覆盖
公共无效surfaceCreated(SurfaceHolder持有人){
thread.startRunning(真);
thread.start();
} @覆盖
公共无效surfaceDestroyed(SurfaceHolder持有人){
thread.startRunning(假);
使用Thread.stop();
}}
只需点击触摸事件的X和Y坐标,并检查该X,Y-组合框的范围内。我可以看到你在正确的道路上通过查看注释掉code。
做这样的事情:
RectF RECT =新RectF(X,Y,X + box.getWidth(),Y + box.geHeight());如果(rect.contains(touchX,敏感)){
//你打箱子,让拖...
}
I currently have something working where I can drag a box horizontally on the screen (what I want it to do). However, it works when you click ANYWHERE on the screen, and I want it to work only when the box has been clicked. I have tried implementing this in different ways, and I've looked all over the place and still remain lost. Can anybody help? I also can't figure out how a bitmap is placed (I'm using a bitmap right now as I can't for the life of me figure out how to implement the ImageView inside my SurfaceView). If I say my bitmap is placed at 0,0 will that place the bitmap according to its top left corner at 0,0? I also had an algorithm for stopping the box when it reached an edge, but I'll just have to rewrite that as I must have deleted it. Please if you can offer your knowledge I would GREATLY appreciate it
public class BoardView extends SurfaceView implements SurfaceHolder.Callback{
Context mContext;
private BoardThread thread;
private float box_x = 140;
private float box_y = 378;
ImageView i = (ImageView) findViewById(R.id.box_view);
Bitmap box =
(BitmapFactory.decodeResource
(getResources(), R.drawable.box));
private float boxWidth = box.getWidth();
private float boxHeight = box.getHeight();
public BoardView(Context context){
super(context);
//surfaceHolder provides canvas that we draw on
getHolder().addCallback(this);
// controls drawings
thread = new BoardThread(getHolder(),this);
//intercepts touch events
setFocusable(true);
}
@Override
public void onDraw(Canvas canvas){
canvas.drawColor(Color.WHITE);
//draw box and set start location
canvas.drawBitmap(box, box_x - (boxWidth/2),
box_y - (boxHeight/2), null);
}
@Override
public boolean onTouchEvent(MotionEvent event){
//boolean mode = false;
if(event.getAction() == MotionEvent.ACTION_DOWN){
//int x = (int)event.getX();
//int y = (int)event.getY();
//if (x > box_x && x < box_x + 29 && y > box_y && y < box_y + 30){
//mode = true;
box_x = (int)event.getX();
//}
}
if(event.getAction() == MotionEvent.ACTION_MOVE) {
//int x = (int)event.getX();
//int y = (int)event.getY();
//if (mode == true){
box_x = (int)event.getX();
//}
}
invalidate();
return true;
}
@Override
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height ){
}
@Override
public void surfaceCreated(SurfaceHolder holder){
thread.startRunning(true);
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder){
thread.startRunning(false);
thread.stop();
}
}
Simply check the touch event x and y coordinate, and check if this x,y-combination is within the box's bounds. I can see you are on the right path by looking at your commented out code.
Do something like this:
RectF rect = new RectF(x,y, x + box.getWidth(), y+box.geHeight());
if(rect.contains(touchX, touchY)) {
// You hit the box, allow dragging...
}
这篇关于拖动ImageView的或位图触摸后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!