gpt4 book ai didi

Android 在屏幕上拖放图像?

转载 作者:太空宇宙 更新时间:2023-11-03 11:44:55 27 4
gpt4 key购买 nike

我正在帮助项目用户将图像从一个位置移动到屏幕上的另一个位置。我已经编写了一个示例代码来移动图像,但这里的问题是,如果我移动一个图像,相邻的图像也会开始移动。这是示例代码。任何一个想法。

主.java

public class MainActivity extends Activity  {
int windowwidth;
int windowheight;
ImageView ima1,ima2;

private android.widget.RelativeLayout.LayoutParams layoutParams ;
// private android.widget.RelativeLayout.LayoutParams layoutParams ;
//private android.widget.RelativeLayout.LayoutParams layoutParams ;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();

System.out.println("width" +windowwidth);
System.out.println("height" +windowheight);

ima1 = (ImageView)findViewById(R.id.imageview1);
ima1.setOnTouchListener(new View.OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {
layoutParams = (RelativeLayout.LayoutParams) ima1.getLayoutParams();

switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
break;

case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();

System.out.println("value of x" +x_cord);
System.out.println("value of y" +y_cord);

if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams.leftMargin = x_cord-25;
layoutParams.topMargin = y_cord-25;
// layoutParams.rightMargin = x_cord-25;
// layoutParams.bottomMargin = y_cord-25;
ima1.setLayoutParams(layoutParams);
break;
default: break;
}
return true;
}
});

ima2 = (ImageView)findViewById(R.id.imageview2);
ima2.setOnTouchListener(new View.OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {
layoutParams = (RelativeLayout.LayoutParams) ima2.getLayoutParams();
switch(event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();

System.out.println("value of x1" +x_cord);
System.out.println("value of y1" +y_cord);

if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams.leftMargin = x_cord - 25;
layoutParams.topMargin = y_cord - 75;
ima2.setLayoutParams(layoutParams);
break;
default: break;
}
return true;
}
});
}
}

主.xml

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/imageview1"
android:src="@drawable/image1" />
<ImageView
android:layout_width="100sp"
android:layout_height="100sp"
android:id="@+id/imageview2"
android:src="@drawable/image2" />
</RelativeLayout>

最佳答案

将以下代码写入您的 Activity 文件。

windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();


tv1 = (ImageView)findViewById(R.id.image);
tv1.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
layoutParams1 = (RelativeLayout.LayoutParams) tv1.getLayoutParams();
switch(event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams1.leftMargin = x_cord - 25;
layoutParams1.topMargin = y_cord - 75;
tv1.setLayoutParams(layoutParams1);
break;
default:
break;
}
return true;
}
});

tv2 = (ImageView)findViewById(R.id.image1);
tv2.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
layoutParams2 = (RelativeLayout.LayoutParams) tv2.getLayoutParams();
switch(event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams2.leftMargin = x_cord - 25;
layoutParams2.topMargin = y_cord - 75;
tv2.setLayoutParams(layoutParams2);
break;
default:
break;
}
return true;
}
});

XML 文件:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="50sp" android:layout_height="50sp"
android:id="@+id/image" android:src="@drawable/image">
</ImageView>
<ImageView android:layout_y="30dip" android:layout_x="118dip"
android:layout_width="50sp" android:layout_height="50sp" android:id="@+id/image1"
android:src="@drawable/image1">
</ImageView>
</RelativeLayout>

关于Android 在屏幕上拖放图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11027792/

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