我已经实现了缩放和拖动功能,工作正常,同时我面临的问题是,一旦拖动结束,图像就会回到右下角。下面是我用于拖动的代码。你能指导我如何控制/记住我最后拖动的位置。
frameLayout = (FrameLayout) findViewById(R.id.relativeLayout);
frameLayout.setDrawingCacheEnabled(true);
frameLayout.buildDrawingCache();
bm = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bm);
frameLayout.draw(c);
frameLayout.setOnTouchListener(this);`
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
if (x < 0) {
img_must_cities.setX(0);
img_must_cities.setY(y);
img_road_names.setX(0);
img_road_names.setY(y);
x = 0;
Toast.makeText(getApplicationContext(), "x=0", Toast.LENGTH_SHORT).show();
}
if (y < 0) {
img_must_cities.setY(0);
img_must_cities.setX(x);
img_road_names.setY(0);
img_road_names.setX(x);
y = 0;
Toast.makeText(getApplicationContext(), "y=0", Toast.LENGTH_SHORT).show();
}
if (x > bm.getWidth()) {
x = bm.getWidth() - 20;
img_must_cities.setX(x);
img_must_cities.setY(y);
img_road_names.setX(x);
img_road_names.setY(y);
Toast.makeText(getApplicationContext(), "x=bitmap.maxwidth(}", Toast.LENGTH_SHORT).show();
}
if (y > bm.getHeight()) {
y = bm.getHeight() - 20;
img_must_cities.setX(x);
img_must_cities.setY(y);
img_road_names.setX(x);
img_road_names.setY(y);
Toast.makeText(getApplicationContext(), "y=bitmap.maxheight(}", Toast.LENGTH_SHORT).show();
}
if (x > 0 && x < bm.getWidth() && y > 0 && y < bm.getHeight()) {
int action = event.getAction();
int pixel = bm.getPixel((int) x, (int) y);
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
Log.e("COORDINATES", "Touch coordinates : x" + String.valueOf(x) + "y" + String.valueOf(y));
img_must_cities.setX(x);
img_must_cities.setY(y);
img_road_names.setX(x);
img_road_names.setY(y);
inRed = Color.red(pixel);
inBlue = Color.blue(pixel);
inGreen = Color.green(pixel);
Log.e("Colors", "R:" + inRed + " G:" + inGreen + " B:" + inBlue);
break;
}
case MotionEvent.ACTION_MOVE: {
Log.e("COORDINATES", "Touch coordinates : x" + String.valueOf(x) + "y" + String.valueOf(y));
img_must_cities.setX(x);
img_must_cities.setY(y);
img_road_names.setX(x);
img_road_names.setY(y);
inRed = Color.red(pixel);
inBlue = Color.blue(pixel);
inGreen = Color.green(pixel);
Log.e("Colors", "R:" + inRed + " G:" + inGreen + " B:" + inBlue);
break;
}
}
}
if (x < 0 && x < bm.getWidth() && y < 0 && y < bm.getHeight()) {
int action = event.getAction();
int pixel = bm.getPixel((int) x, (int) y);
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
Log.e("COORDINATES", "Touch coordinates : x" + String.valueOf(x) + "y" + String.valueOf(y));
img_must_cities.setX(x);
img_must_cities.setY(y);
img_road_names.setX(x);
img_road_names.setY(y);
inRed = Color.red(pixel);
inBlue = Color.blue(pixel);
inGreen = Color.green(pixel);
Log.e("Colors", "R:" + inRed + " G:" + inGreen + " B:" + inBlue);
break;
}
case MotionEvent.ACTION_MOVE: {
Log.e("COORDINATES", "Touch coordinates : x" + String.valueOf(x) + "y" + String.valueOf(y));
img_must_cities.setX(x);
img_must_cities.setY(y);
img_road_names.setX(x);
img_road_names.setY(y);
inRed = Color.red(pixel);
inBlue = Color.blue(pixel);
inGreen = Color.green(pixel);
Log.e("Colors", "R:" + inRed + " G:" + inGreen + " B:" + inBlue);
break;
}
}
}
return false;
}
答案 0 :(得分:2)
也许你可以试试:
最初,保存x和y,当您检测到ACTION_DROP
事件使用getX()
和getY()
时,如果要记录它们,请将x和y设置为初始值
您可以在此处找到活动的文档:https://developer.android.com/guide/topics/ui/drag-drop.html