在屏幕的Android拖放图片? [英] Android Drag and drop images on the Screen?
本文介绍了在屏幕的Android拖放图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我米工作项目用户移动至在一个位置的图像在屏幕上的另一位置。我写了一个样本code键移动图像,但这里的问题是,如果我将一个图像相邻的形象也开始移动。这里是这个样本code.any一个想法。
Main.java
公共类MainActivity延伸活动{
INT WINDOWWIDTH;
INT WINDOWHEIGHT;
ImageView的IMA1,IMA2;
私人android.widget.RelativeLayout.LayoutParams的LayoutParams;
//私人android.widget.RelativeLayout.LayoutParams的LayoutParams;
//私人android.widget.RelativeLayout.LayoutParams的LayoutParams;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
。WINDOWWIDTH = getWindowManager()getDefaultDisplay()的getWidth()。
。WINDOWHEIGHT = getWindowManager()getDefaultDisplay()的getHeight();
的System.out.println(宽度+ WINDOWWIDTH);
的System.out.println(高度+ WINDOWHEIGHT);
IMA1 =(ImageView的)findViewById(R.id.imageview1);
ima1.setOnTouchListener(新View.OnTouchListener(){
公共布尔onTouch(视图V,MotionEvent事件){
的LayoutParams =(RelativeLayout.LayoutParams)ima1.getLayoutParams();
开关(event.getAction())
{
案例MotionEvent.ACTION_DOWN:
打破;
案例MotionEvent.ACTION_MOVE:
INT x_cord =(INT)event.getRawX();
INT y_cord =(INT)event.getRawY();
的System.out.println(x值+ x_cord);
的System.out.println(+ y_cordY的值);
如果(x_cord> WINDOWWIDTH){
x_cord = WINDOWWIDTH;
}
如果(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);
打破;
默认值:打破;
}
返回true;
}
});
IMA2 =(ImageView的)findViewById(R.id.imageview2);
ima2.setOnTouchListener(新View.OnTouchListener(){
公共布尔onTouch(视图V,MotionEvent事件){
的LayoutParams =(RelativeLayout.LayoutParams)ima2.getLayoutParams();
开关(event.getActionMasked())
{
案例MotionEvent.ACTION_DOWN:
打破;
案例MotionEvent.ACTION_MOVE:
INT x_cord =(INT)event.getRawX();
INT y_cord =(INT)event.getRawY();
的System.out.println(的X1值+ x_cord);
的System.out.println(Y1的值+ y_cord);
如果(x_cord> WINDOWWIDTH){
x_cord = WINDOWWIDTH;
}
如果(y_cord> WINDOWHEIGHT){
y_cord = WINDOWHEIGHT;
}
layoutParams.leftMargin = x_cord - 25;
layoutParams.topMargin = y_cord - 75;
ima2.setLayoutParams(的LayoutParams);
打破;
默认值:打破;
}
返回true;
}
});
}
}
main.xml中
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
< ImageView的
机器人:layout_width =100dp
机器人:layout_height =100dp
机器人:ID =@ + ID / imageview1
机器人:SRC =@可绘制/此搜索/>
< ImageView的
机器人:layout_width =100SP
机器人:layout_height =100SP
机器人:ID =@ + ID / imageview2
机器人:SRC =@可绘制/图像2/>
< / RelativeLayout的>
解决方案
下面写code到你的活动文件。
。 WINDOWWIDTH = getWindowManager()getDefaultDisplay()的getWidth();
。WINDOWHEIGHT = getWindowManager()getDefaultDisplay()的getHeight();
TV1 =(ImageView的)findViewById(R.id.image);
tv1.setOnTouchListener(新View.OnTouchListener(){
@覆盖
公共布尔onTouch(视图V,MotionEvent事件){
layoutParams1 =(RelativeLayout.LayoutParams)tv1.getLayoutParams();
开关(event.getActionMasked())
{
案例MotionEvent.ACTION_DOWN:
打破;
案例MotionEvent.ACTION_MOVE:
INT x_cord =(INT)event.getRawX();
INT y_cord =(INT)event.getRawY();
如果(x_cord> WINDOWWIDTH){
x_cord = WINDOWWIDTH;
}
如果(y_cord> WINDOWHEIGHT){
y_cord = WINDOWHEIGHT;
}
layoutParams1.leftMargin = x_cord - 25;
layoutParams1.topMargin = y_cord - 75;
tv1.setLayoutParams(layoutParams1);
打破;
默认:
打破;
}
返回true;
}
});
TV2 =(ImageView的)findViewById(R.id.image1);
tv2.setOnTouchListener(新View.OnTouchListener(){
@覆盖
公共布尔onTouch(视图V,MotionEvent事件){
layoutParams2 =(RelativeLayout.LayoutParams)tv2.getLayoutParams();
开关(event.getActionMasked())
{
案例MotionEvent.ACTION_DOWN:
打破;
案例MotionEvent.ACTION_MOVE:
INT x_cord =(INT)event.getRawX();
INT y_cord =(INT)event.getRawY();
如果(x_cord> WINDOWWIDTH){
x_cord = WINDOWWIDTH;
}
如果(y_cord> WINDOWHEIGHT){
y_cord = WINDOWHEIGHT;
}
layoutParams2.leftMargin = x_cord - 25;
layoutParams2.topMargin = y_cord - 75;
tv2.setLayoutParams(layoutParams2);
打破;
默认:
打破;
}
返回true;
}
});
XML文件: -
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
< ImageView的机器人:layout_width =50SP机器人:layout_height =50SP
机器人:ID =@ + ID /图片机器人:SRC =@可绘制/图像>
< / ImageView的>
< ImageView的机器人:layout_y =30dip机器人:layout_x =118dip
机器人:layout_width =50SP机器人:layout_height =50SP机器人:ID =@ + ID /此搜索
机器人:SRC =@可绘制/此搜索>
< / ImageView的>
< / RelativeLayout的>
I m working on project user to move the image in one position to Another position on the screen. I have written a sample code to move the image but the problem here is if I move one image the neighbouring image also starts moving.. Here is the sample code.any one Idea of this.
Main.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;
}
});
}
}
main.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>
解决方案
Write Below Code into your Activity File.
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 File:-
<?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拖放图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文