在图像视图上拖放图像

时间:2013-12-24 23:01:25

标签: android drag-and-drop imageview android-imageview

嗨,我今天问了这个问题how to drag and drop a image onto a image,有人高兴地给了我一个看起来应该有效的答案,但把它放到我的代码中并取出我被告知的内容,仍然给我错误

无法解析符号getId,也告诉我语法错误

任何人都可以提供帮助(也许可以解释一下)

我的更新代码如下,永远感谢任何和所有帮助

.Java(UPDTAED)

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.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.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();
        if(view.getId()==R.id.squareImage && v.getId()==R.id.squareImage1)
        {
            ViewGroup from = (ViewGroup) view.getParent();
            from.removeView(view);
            v.setBackground(R.drawable.dragsquare);
            return true;
        } else if(view.getId()==R.id.circleImage1 && v.getId()==R.id.circleImage){
            ViewGroup from = (ViewGroup) view.getParent();
            from.removeView(view);
            v.setBackground(R.drawable.dragcircle);
            return true;
        } else if(view.getId()==R.id.triangleImage1 && v.getId()==R.id.triangleImage){
            ViewGroup from = (ViewGroup) view.getParent();
            from.removeView(view);
            v.setBackground(R.drawable.dragtriangle);
            return true;
        }
        else {
            return false;
        }

    }
return true;
}
}

这是我更新的logcat

Information:3 errors
Information:Compilation completed with 13 errors and 0 warnings in 3 min 40 sec
Information:13 errors
Information:0 warnings
Error:Gradle: Execution failed for task ':SocialStories2:compileDebug'.
> Compilation failed; see the compiler error output for details.
C:\Users\dawne  allen\AndroidStudioProjects\SocialStories2Project\
SocialStories2\src\main\java\com\martinsapp\socialstories\DragandDrop.java
Error:Error:line (46)Gradle: error: method setBackground in 
class View cannot be     
applied to given types;
Error:Error:line (46)Gradle: error: method setBackground in 
class View cannot be   applied to given types;

required: Drawable
Error:Error:line (46)Gradle: error: method setBackground in 
class View cannot be  applied to given types;

required: Drawable

found: int
Error:Error:line (46)Gradle: error: method 
setBackground in class View cannot be applied to given types;

required: Drawable

found: int

reason: actual argument int cannot be converted to Drawable by 
method invocation  conversion
Error:Error:line (51)Gradle: error: method setBackground in  
class View cannot be  applied to given types;
Error:Error:line (51)Gradle: error: method setBackground in 
class View cannot be applied to given types;

required: Drawable
Error:Error:line (51)Gradle: error: method setBackground in 
class View cannot be  applied to given types;

required: Drawable

found: int
    Error:Error:line (51)Gradle: error: method setBackground in 
class View cannot be   
applied to given types;

required: Drawable

found: int

reason: actual argument int cannot be converted to Drawable by 
method invocation     conversion
Error:Error:line (56)Gradle: error: method setBackground in 
class View cannot be  applied to given types;
Error:Error:line (56)Gradle: error: method setBackground in 
class View cannot be   applied to given types;

required: Drawable
Error:Error:line (56)Gradle: error: method setBackground in 
class View cannot be      applied to given types;

required: Drawable

found: int
Error:Error:line (56)Gradle: error: method setBackground 
in class View cannot be    applied to given types;

required: Drawable

found: int

reason: actual argument int cannot be converted to Drawable 
by method invocation conversion

1 个答案:

答案 0 :(得分:1)

getId是方法名称,因此应使用()

进行调用

你应该做

if(view.getId()==R.id.squareImage && v.getId()==R.id.squareImage1)

如果还有其他语法错误,请发布您的logcat