Android 触摸时拖动图像视图或位图
我目前有一些工作,我可以在屏幕上水平拖动一个框(我希望它做什么)。但是,当您单击屏幕上的任何位置时,它都会工作,我希望它仅在单击框时工作。我尝试过用不同的方式来实现这一点,但我到处都找遍了,仍然迷路。有人能帮忙吗?我也不知道位图是如何放置的(我现在正在使用位图,因为我一辈子都不知道如何在我的SurfaceView中实现ImageView)。如果我说我的位图被放置在0,0,那会根据它的左上角将位图放置在0,0吗?我也有一个算法,当盒子到达边缘时停止它,但我必须重写它,因为我必须删除它。如果你能提供你的知识,我将不胜感激Android 触摸时拖动图像视图或位图,android,imageview,drag,Android,Imageview,Drag,我目前有一些工作,我可以在屏幕上水平拖动一个框(我希望它做什么)。但是,当您单击屏幕上的任何位置时,它都会工作,我希望它仅在单击框时工作。我尝试过用不同的方式来实现这一点,但我到处都找遍了,仍然迷路。有人能帮忙吗?我也不知道位图是如何放置的(我现在正在使用位图,因为我一辈子都不知道如何在我的SurfaceView中实现ImageView)。如果我说我的位图被放置在0,0,那会根据它的左上角将位图放置在0,0吗?我也有一个算法,当盒子到达边缘时停止它,但我必须重写它,因为我必须删除它。如果你能提供
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();
}
}
public类BoardView扩展了SurfaceView实现了SurfaceHolder.Callback{
语境;
专用板线;
专用浮箱_x=140;
专用浮箱_y=378;
ImageView i=(ImageView)findViewById(R.id.box\u视图);
位图框=
(BitmapFactory.decodeResource)
(getResources(),R.drawable.box));
私有float-boxWidth=box.getWidth();
私有float-boxHeight=box.getHeight();
公共BoardView(上下文){
超级(上下文);
//surfaceHolder提供了我们绘制的画布
getHolder().addCallback(此);
//控制图形
thread=新的BoardThread(getHolder(),this);
//拦截触摸事件
设置聚焦(真);
}
@凌驾
公共空白onDraw(画布){
画布。drawColor(颜色。白色);
//绘制框并设置开始位置
画布.绘图位图(长方体,长方体x-(长方体宽度/2),
方框y-(方框高度/2),空值);
}
@凌驾
公共布尔onTouchEvent(运动事件){
//布尔模式=假;
if(event.getAction()==MotionEvent.ACTION\u向下){
//int x=(int)event.getX();
//int y=(int)event.getY();
//如果(x>box\ux&&xbox\uy&&y
只需检查触摸事件x和y坐标,并检查此x、y组合是否在框的范围内。通过查看注释掉的代码,我可以看出您走的是正确的道路
这样做:
RectF rect = new RectF(x,y, x + box.getWidth(), y+box.geHeight());
if(rect.contains(touchX, touchY)) {
// You hit the box, allow dragging...
}
试试这个:
package teste.com.br.teste;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Tacila on 03/07
*/
public class Game extends View {
private Context context;
private BitmaptArrastavel bmArrastavel;
private BitmaptArrastavel bmArrastavel2;
private BitmaptArrastavel bmArrastavelTeste;
private List<BitmaptArrastavel> btms;
private BitmaptArrastavel[] btmsAtivas;
private BitmaptArrastavel[] lake;
private BitmaptArrastavel[] ativos;
private int qntDeItens = 5;
public Game(Context context) {
super(context);
this.context = context;
init();
}
public void init() {
btms = new ArrayList<BitmaptArrastavel>();
btmsAtivas = new BitmaptArrastavel[1];
ativos=new BitmaptArrastavel[1];
lake = new BitmaptArrastavel[3];
lake[0] = new BitmaptArrastavel(escalaBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.cartadc)), 200);
lake[1] = new BitmaptArrastavel(escalaBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.cartaao)), 210);
lake[2] = new BitmaptArrastavel(escalaBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.cartake)), 220);
btms.add(bmArrastavel);
btms.add(bmArrastavelTeste);
btms.add(bmArrastavel2);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRGB(0,0,139);
for(int i = 0; i<=2; i++){
lake[i].drawOnCanvas(canvas);
}
}
public void travarArr(float x, float y ){
for(int i = 0; i<=2 ; i++){
if(lake[i].isDentro(x,y)){
ativos[0]=lake[i];
}
}
}
public void destravarBitmap() {
ativos[0]=null;
}
public Bitmap escalaBitmap(Bitmap bm) {
return Bitmap.createScaledBitmap(bm, 200, 300, false);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getActionMasked();
switch (action) {
case MotionEvent.ACTION_DOWN:
System.out.println("Dentro do MotionEvent.ActionDown");
travarArr(event.getX(),event.getY());
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
System.out.println("Dentro do MotionEvent.ActionUP e ActionCancel tenho q entrar no destrava ");
destravarBitmap();
break;
case MotionEvent.ACTION_MOVE:
System.out.println("Dentro do MotionEvent.ActionMove");
for(int i = 0; i<ativos.length;i++){
if(ativos[i]!=null){
ativos[i].mover((int)event.getX(),(int)event.getY());
invalidate();
}
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
System.out.println("Dentro do MotionEvent.ActionPointerDown");
travarArr(event.getX(), event.getY());
break;
case MotionEvent.ACTION_POINTER_UP:
System.out.println("Dentro do MotionEvent.ActionPointerUp");
destravarBitmap();
break;
default:
return super.onTouchEvent(event);
}
return true;
}
// int rotation = 0;
//
// public Bitmap vertical() {
// Matrix matrix = new Matrix();
// matrix.postRotate(90);
// Bitmap bm = escalaBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.carta4c));
// return Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
// }
}
package teste.com.br.teste;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.graphics.Canvas;
导入android.view.MotionEvent;
导入android.view.view;
导入java.util.ArrayList;
导入java.util.List;
/**
*由塔西拉于2007年3月创建
*/
公共类游戏扩展视图{
私人语境;
私有Bitmaptarastavel bmArrastavel;
私有位图tarrastavel bmArrastavel2;
私有比特映射地址bmArrastavelTeste;
私有列表BTM;
私有Bitmaptarastavel[]btmsAtivas;
私人Bitmaptarastavel[]湖;
私有Bitmaptarastavel[]ativos;
私人int qntDeItens=5;
公共游戏(上下文){
超级(上下文);
this.context=上下文;
init();
}
公共void init(){
btms=新的ArrayList();
btmsAtivas=新的Bitmaptarastavel[1];
ativos=新的Bitmaptarastavel[1];
lake=新Bitmaptarastavel[3];
lake[0]=新的BitmaptArrastavel(escalaBitmap(BitmapFactory.decodeResource(context.getResources(),R.drawable.cartadc)),200);
lake[1]=新的位图tarrastavel(escalaBitmap(BitmapFactory.decodeResource(context.getResources(),R.drawable.cartaao)),210);
lake[2]=新的位图tarrastavel(escalaBitmap(BitmapFactory.decodeResource(context.getResources(),R.drawable.cartake)),220);
btms.add(bmArrastavel);
btms.add(bmArrastavelTeste);
btms.add(bmArrastavel2);
}
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
canvas.drawRGB(0,0139);
对于(int i=0;i)我应该在ActionDown事件或ActionMove事件中执行此操作吗?现在真的很麻烦,我需要在其中解决一些问题。使其移动更平滑。
package teste.com.br.teste;
import android.graphics.Bitmap;
import android.graphics.Canvas;
/**
* Created by Tacila on 05/07/2017.
*/
public class BitmaptArrastavel {
public int x, y, altura, largura;
private Bitmap bitmap;
public BitmaptArrastavel(Bitmap b, int x) {
bitmap = b;
this.x = x;
init();
}
public void init() {
largura = 200;
altura = 350;
}
public Bitmap escalaBitmap(Bitmap bm) {
return Bitmap.createScaledBitmap(bm, largura, altura, false);
}
public boolean isDentro(float x, float y) {
return (x >= this.x && x <= this.x + largura && y >= this.y && y <= this.y + altura);
}
public void drawOnCanvas(Canvas canvas) {
canvas.drawBitmap(bitmap, x, y, null);
}
public void mover(int x, int y) {
this.x = x;
this.y = y;
}
}