如何拖放一个图像到图像 [英] how to drag and drop a image onto a image
问题描述
您好,圣诞快乐给大家明天(英国),我是比较新的到Android,但已经成功地管理了一些零零碎碎的东西我尝试达到现在的问题是一个简单的拖放的活动,允许用户拖放形状的ImageView到另一个ImageView的,如果图像匹配,则应该更换重叠的图像,并且如果它不应该弹回的地方是,我知道这意味着创建在我的drop事件的的if / else块但所作所为似乎少很多教程后,我无法拼凑出我想要的,目前我没有足够的Java知识来完成这件事目前,我有一个布局持有6图像视图,3是静态的,其他3可以在移动屏幕放置在布局,但不是在imageviews,我想这是因为香港专业教育学院deisgned的draglistener到我的布局,而不是我的imageviews但即时通讯有点失落,有人可以帮我吗?感谢您的任何和所有的建议。 这里是我的code
DragandDrop.java
进口android.os.Bundle;
进口android.view.DragEvent;
进口android.view.MotionEvent;
进口android.view.View;
进口android.view.View.DragShadowBuilder;
进口android.view.View.OnDragListener;
进口android.view.View.OnTouchListener;
进口android.view.ViewGroup;
进口android.widget.LinearLayout;
进口android.app.Activity;
公共类DragandDrop扩展活动实现OnTouchListener,OnDragListener {
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.draganddrop);
findViewById(R.id.squareImage).setOnTouchListener(本);
findViewById(R.id.circleImage).setOnTouchListener(本);
findViewById(R.id.triangleImage).setOnTouchListener(本);
findViewById(R.id.top_container).setOnDragListener(本);
findViewById(R.id.bottom_container).setOnDragListener(本);
findViewById(R.id.squareImage1).setOnDragListener(本);
findViewById(R.id.circleImage1).setOnDragListener(本);
findViewById(R.id.triangleImage1).setOnDragListener(本);
}
@覆盖
公共布尔onTouch(视图V,MotionEvent E){
如果(e.getAction()== MotionEvent.ACTION_DOWN){
DragShadowBuilder shadowBuilder =新View.DragShadowBuilder(V);
v.startDrag(空,shadowBuilder,V,0);
v.setVisibility(View.INVISIBLE);
返回true;
} 其他 {
返回false;
}
}
@覆盖
公共ondrag当布尔(视图V,的dragEvent E){
如果(e.getAction()== DragEvent.ACTION_DROP){
查看查看=(查看)e.getLocalState();
ViewGroup中的=(的ViewGroup)view.getParent();
from.removeView(视图);
的LinearLayout为=(的LinearLayout)V;
to.addView(视图);
view.setVisibility(View.VISIBLE);
}
返回true;
}
}
draganddrop.xml
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
工具:draganddrop上下文=
机器人:背景=@可绘制/ dragshapes
机器人:方向=垂直
机器人:ID =@ + ID / dropLayout>
<的LinearLayout
机器人:ID =@ + ID / top_container
机器人:layout_width =FILL_PARENT
机器人:layout_height =match_parent
机器人:layout_weight =1
机器人:背景=@机器人:彩色/透明
机器人:方向=横向
机器人:重力=中心>
< ImageView的
机器人:contentDescription =@字符串/ square_text_content
机器人:ID =@ + ID / squareImage1
机器人:layout_width =100dp
机器人:layout_height =100dp
机器人:layout_margin =10dp
机器人:SRC =@可绘制/ dragsquare1/>
< ImageView的
机器人:contentDescription =@字符串/ circle_text_content
机器人:ID =@ + ID / circleImage1
机器人:layout_width =100dp
机器人:layout_height =100dp
机器人:layout_margin =10dp
机器人:SRC =@可绘制/ dragcircle1/>
< ImageView的
机器人:contentDescription =@字符串/ triangle_text_content
机器人:ID =@ + ID / triangleImage1
机器人:layout_width =100dp
机器人:layout_height =100dp
机器人:layout_margin =10dp
机器人:SRC =@可绘制/ dragtriangle1/>
< / LinearLayout中>
<的LinearLayout
机器人:ID =@ + ID / bottom_container
机器人:layout_width =FILL_PARENT
机器人:layout_height =match_parent
机器人:layout_weight =1
机器人:背景=@机器人:彩色/透明
机器人:方向=横向
机器人:重力=中心>
< ImageView的
机器人:contentDescription =@字符串/ square_text_content
机器人:ID =@ + ID / squareImage
机器人:layout_width =100dp
机器人:layout_height =100dp
机器人:layout_margin =10dp
机器人:SRC =@可绘制/ dragsquare/>
< ImageView的
机器人:contentDescription =@字符串/ circle_text_content
机器人:ID =@ + ID / circleImage
机器人:layout_width =100dp
机器人:layout_height =100dp
机器人:layout_margin =10dp
机器人:SRC =@可绘制/ dragcircle/>
< ImageView的
机器人:contentDescription =@字符串/ triangle_text_content
机器人:ID =@ + ID / triangleImage
机器人:layout_width =100dp
机器人:layout_height =100dp
机器人:layout_margin =10dp
机器人:SRC =@可绘制/ dragtriangle/>
< / LinearLayout中>
< / LinearLayout中>
删除设置阻力监听器容器
findViewById(R.id.top_container).setOnDragListener(本); //删除此
findViewById(R.id.bottom_container).setOnDragListener(本); //删除此
检查拖动图像视图是否与你匹配的下降view.If它们匹配设置降至图像视图的背景是,拖一个。(其它尺寸相同,每个XML文件) 样品code -
@覆盖
公共ondrag当布尔(视图V,的dragEvent E){
如果(e.getAction()== DragEvent.ACTION_DROP){
查看查看=(查看)e.getLocalState();
如果(view.getId()== R.id.squareImage1&安培;&安培; v.getId()== R.id.squareImage)
{
ViewGroup中的=(的ViewGroup)view.getParent();
from.removeView(视图);
v.setBackground(@绘制/ dragsquare1); // TODO:改变这种伪code。
返回true;
}否则,如果(view.getId()== R.id.circleImage1和放大器;&安培; v.getId()== R.id.circleImage){
ViewGroup中的=(的ViewGroup)view.getParent();
from.removeView(视图);
v.setBackground(@绘制/ dragcircle1); // TODO:改变这种伪code。
返回true;
}否则,如果(view.getId()== R.id.triangleImage1和放大器;&安培; v.getId()== R.id.triangleImage){
ViewGroup中的=(的ViewGroup)view.getParent();
from.removeView(视图);
v.setBackground(@绘制/ dragtriangle1); // TODO:改变这种伪code。
返回true;
} 其他 {
返回false;
}
}
}
希望这有助于you..merry圣诞节
hello and merry Christmas to everyone for tomorrow(UK), I'm rather new to android but have successfully managed a few bits and pieces what im trying to achieve right now is a simple drag and drop activity that allows the user to drag and drop a shape ImageView onto another ImageView, if the image matches then it should replace the image it overlapped, and if it doesnt it should snap back to where it was, i know this means creating an if/else block in my drop event but after thumbing few many tutorials i couldn't piece together what i wanted and i dont currently have enough java knowledge to pull this off currently i have a layout which holds 6 image views, 3 are static and the other 3 can be moved on the screen and placed on the layout but not on the imageviews, i think this is because ive deisgned the draglistener to my layout and not to my imageviews but im a little lost, can somebody help me? thank you for any and all suggestions. here is my code
DragandDrop.java
import android.os.Bundle;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.app.Activity;
public class DragandDrop extends Activity implements OnTouchListener, OnDragListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.draganddrop);
findViewById(R.id.squareImage).setOnTouchListener(this);
findViewById(R.id.circleImage).setOnTouchListener(this);
findViewById(R.id.triangleImage).setOnTouchListener(this);
findViewById(R.id.top_container).setOnDragListener(this);
findViewById(R.id.bottom_container).setOnDragListener(this);
findViewById(R.id.squareImage1).setOnDragListener(this);
findViewById(R.id.circleImage1).setOnDragListener(this);
findViewById(R.id.triangleImage1).setOnDragListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(null, shadowBuilder, v, 0);
v.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
@Override
public boolean onDrag(View v, DragEvent e) {
if (e.getAction()==DragEvent.ACTION_DROP) {
View view = (View) e.getLocalState();
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
LinearLayout to = (LinearLayout) v;
to.addView(view);
view.setVisibility(View.VISIBLE);
}
return true;
}
}
draganddrop.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".draganddrop"
android:background="@drawable/dragshapes"
android:orientation="vertical"
android:id="@+id/dropLayout">
<LinearLayout
android:id="@+id/top_container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/transparent"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:contentDescription="@string/square_text_content"
android:id="@+id/squareImage1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:src="@drawable/dragsquare1" />
<ImageView
android:contentDescription="@string/circle_text_content"
android:id="@+id/circleImage1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:src="@drawable/dragcircle1" />
<ImageView
android:contentDescription="@string/triangle_text_content"
android:id="@+id/triangleImage1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:src="@drawable/dragtriangle1" />
</LinearLayout>
<LinearLayout
android:id="@+id/bottom_container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/transparent"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:contentDescription="@string/square_text_content"
android:id="@+id/squareImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:src="@drawable/dragsquare" />
<ImageView
android:contentDescription="@string/circle_text_content"
android:id="@+id/circleImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:src="@drawable/dragcircle" />
<ImageView
android:contentDescription="@string/triangle_text_content"
android:id="@+id/triangleImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:src="@drawable/dragtriangle" />
</LinearLayout>
</LinearLayout>
Remove setting drag listeners for the containers
findViewById(R.id.top_container).setOnDragListener(this);//remove this
findViewById(R.id.bottom_container).setOnDragListener(this);//remove this
Check whether the dragged image view matches with your dropped view.If they are matching set the dropped image view's background to that of the dragged one.(Other dimensions are same as per your xml file) sample code-
@Override
public boolean onDrag(View v, DragEvent e) {
if (e.getAction()==DragEvent.ACTION_DROP) {
View view = (View) e.getLocalState();
if(view.getId()==R.id.squareImage1 && v.getId()==R.id.squareImage)
{
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
v.setBackground(@drawable/dragsquare1);//TODO: change this pseudo code.
return true;
} else if(view.getId()==R.id.circleImage1 && v.getId()==R.id.circleImage){
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
v.setBackground(@drawable/dragcircle1);//TODO: change this pseudo code.
return true;
} else if(view.getId()==R.id.triangleImage1 && v.getId()==R.id.triangleImage){
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
v.setBackground(@drawable/dragtriangle1);//TODO: change this pseudo code.
return true;
} else {
return false;
}
}
}
Hope this helps you..merry christmas
这篇关于如何拖放一个图像到图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!