gpt4 book ai didi

android - 两个图像拖动

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:12:32 25 4
gpt4 key购买 nike

我正在制作一个模块,其中我有两个图像,每当我触摸一个图像时,它应该跟随手指或鼠标(在模拟器中)拖动,如果它越过另一个图像,那么它们会改变它们在第一个图像所在的位置第一次触摸(ACTION_DOWN)。我编写了以下代码,其中 View 在移动,但是当我拖动第一张图像时,第二张图像也被拖动了。进一步想知道如何改变立场。

.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/vg"
>

<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>

Activity 文件

public class MainActivity extends Activity {
private View selected_item = null;
private int offset_x = 0;
private int offset_y = 0;
Canvas can;
Paint paint;
ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewGroup vg = (ViewGroup)findViewById(R.id.vg);
vg.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getActionMasked())
{

case MotionEvent.ACTION_MOVE:
if(selected_item == img) {
int x = (int)event.getX() - offset_x;
int y = (int)event.getY() - offset_y;

int w = getWindowManager().getDefaultDisplay().getWidth() - 100;
int h = getWindowManager().getDefaultDisplay().getHeight() - 100;
if(x > w)
x = w;
if(y > h)
y = h;
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
new ViewGroup.MarginLayoutParams(
100,
100));
lp.setMargins(x, y, 0, 0);

selected_item.setLayoutParams(lp);
}
break;
default:
break;
}
return true;
}
});
img = (ImageView)findViewById(R.id.img);

//timerDelayRemoveView(500, img);

BitmapDrawable drawable = (BitmapDrawable)getResources().getDrawable(R.drawable.imagesl_02);
Bitmap bitmap = drawable.getBitmap();
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, true);
img.setImageBitmap(scaledBitmap);
LinearLayout.LayoutParams lp0 = new LinearLayout.LayoutParams(100, 100);
lp0.leftMargin = 0;
lp0.topMargin = 0;
img.setLayoutParams(lp0);
//vg.addView(img, lp1);
// vg.addView(img, 1);
img.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
offset_x = (int)event.getX();
offset_y = (int)event.getY();
selected_item = v;
Toast.makeText(MainActivity.this, "down",Toast.LENGTH_SHORT).show();
break;

default: break;
}

return false;
}
});

ImageView img1 = (ImageView)findViewById(R.id.img1);
BitmapDrawable drawable1 = (BitmapDrawable)getResources().getDrawable(R.drawable.realimage);
Bitmap bitmap1 = drawable1.getBitmap();
Bitmap scaledBitmap1 = Bitmap.createScaledBitmap(bitmap1, 100, 100, true);
img1.setImageBitmap(scaledBitmap1);
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(100, 100);
lp1.leftMargin = 100;
lp1.topMargin = 100;
img1.setLayoutParams(lp1);

//img.setImageBitmap(scaledBitmap1);

img1.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
// offset_x = (int)event.getX();
// offset_y = (int)event.getY();
selected_item = v;
break;
default:
break;
}

return false;
}
});

}
}

最佳答案

第二个图像被拖动,因为您将两个图像放在一个 LinearLayout 中。第二张图片的布局参数取决于第一张,这意味着当您向右调整它们时,第二张图片会绑定(bind)到第一张图片的右边缘。

如果我正在处理这个问题(假设我不能使用 Honeycomb 中引入的拖动 API),我会首先将所有内容放在一个带有 2 个 ImageViewRelativeLayout 中.当您“拿起”其中一张图片时,您可以在移动它时调整其布局参数,然后当您放下它时,您可以调整两者的 LayoutParams 以满足您想要的布局。

关于android - 两个图像拖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11896549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com