Android,动态创建的图片无法运行拖放

时间:2016-05-15 01:21:12

标签: android dynamic

public class MatchingGame extends AppCompatActivity implements TextToSpeech.OnInitListener, View.OnDragListener {

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
  rLayout = new RelativeLayout(this);
  rLayout= (RelativeLayout) findViewById(R.id.relative_layout);
        rLayout.setBackgroundColor(Color.YELLOW);
}

@Override
    public void onInit(int status) {
        Log.d(TAG, "matching game oninit");

        if (status == TextToSpeech.SUCCESS) {

   tts.setLanguage(Locale.getDefault());

 setupForGame();

}}

void setupForGame(){

        if(tts.isSpeaking()){
            tts.stop();

        }

        if(isAnimationStarted) {
            isAnimationStarted = false;
            MatchingGame.this.imageAnswer.clearAnimation();
            MatchingGame.this.correctImage[0].clearAnimation();
        }



 rLayParams = new RelativeLayout.LayoutParams(140,140);

        rLayParams.addRule(RelativeLayout.CENTER_IN_PARENT);//ortaya gelecek
        rLayParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);//ortaya gelecek

imgBytes = objectResponse.getObjectImageBlob();
        bmp = BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length);

        imageAnswer = new ImageView(this);
        imageAnswer.setImageBitmap(bmp);
        imageAnswer.setTag(trainingObject.getTrainingobjectAnswer());
        imageAnswer.setId(R.id.imgAnswer);

        imageAnswer.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d(TAG,"imageanswer setontouchlistener");
                if (event.getAction() == MotionEvent.ACTION_DOWN) {


                    Log.d(TAG,"imageanswer setontouchlistener actiondown");
                    ClipData data = ClipData.newPlainText("", "");
                    View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
                    v.startDrag(data, shadowBuilder, v, 0);
                    v.setVisibility(View.INVISIBLE);
                    return true;
                } else {
                    Log.d(TAG,"imageanswer setontouchlistener else");
                    return false;
                }
            }
        });

        imageAnswer.setOnDragListener(this);

        rLayout.removeAllViews();
        rLayout.addView(imageAnswer,rLayParams);

}

Override
    public boolean onDrag(View receivingLayoutView, DragEvent dragEvent) {
        Log.d(TAG, "ondrag start");

        switch (dragEvent.getAction()) {

            case DragEvent.ACTION_DRAG_STARTED:

.     case DragEvent.ACTION_DRAG_ENTERED:
  case DragEvent.ACTION_DRAG_LOCATION:
       case DragEvent.ACTION_DRAG_EXITED:
    case DragEvent.ACTION_DROP:

case DragEvent.ACTION_DRAG_ENDED:

.
.
.



}

}//class end

这是我的代码的简短版本。

当我跑步并且只触摸第二个视图时,日志是:

imageanswer setontouchlistener
imageanswer setontouchlistener actiondown
imageanswer setontouchlistener
 imageanswer setontouchlistener else

它甚至不会调用拖动。它不能进入​​阻力。

我试图让这个类扩展活动但没有改变。

以前,为了测试我创建了不同的类,它正在工作。不同之处在于,imageview位置是静态的,我在oncreate中完成所有操作。

这是有效的:

public class MainActivity extends Activity implements View.OnDragListener {


    private ImageView img1, img2, img3;
    String TAG = "Chic";
    private Button btn,item;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "oncreate start");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_lands);

        img1 = (ImageView) findViewById(R.id.img1);
        //img1.setOnLongClickListener(this);
        img2 = (ImageView) findViewById(R.id.img2);
       // img2.setOnLongClickListener(this);
        img3 = (ImageView) findViewById(R.id.img3);
      //  img3.setOnLongClickListener(this);

        img1.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    ClipData data = ClipData.newPlainText("", "");
                    View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
                    v.startDrag(data, shadowBuilder, v, 0);
                   v.setVisibility(View.INVISIBLE);
                    return true;
                } else {
                    return false;
                }
            }
        });

        img2.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    ClipData data = ClipData.newPlainText("", "");
                    View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
                    v.startDrag(data, shadowBuilder, v, 0);
                   v.setVisibility(View.INVISIBLE);
                    return true;
                } else {
                    return false;
                }
            }
        });

        img3.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    ClipData data = ClipData.newPlainText("", "");
                    View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
                    v.startDrag(data, shadowBuilder, v, 0);
                    v.setVisibility(View.INVISIBLE);
                    return true;
                } else {
                    return false;
                }
            }
        });




        img1.setOnDragListener(this);
        img2.setOnDragListener(this);
        img3.setOnDragListener(this);



 public boolean onTouch(View view, MotionEvent motionEvent) {
        Log.d(TAG, "ontouch never");
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);
            return true;
        } else {
            return false;
        }
    }


 public boolean onDrag(View receivingLayoutView, DragEvent dragEvent) {
        Log.d(TAG, "ondrag start");
//and so on
.
.

有什么区别,为什么这个工作和我的不相同?

当我在一个线程内运行它时,我的类也可以运行,在oncreate中:

Thread t = new Thread() {
            @Override
            public void run() {

imageAnswer.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) { Log.d(TAG,"6isanswered : "+isAnswered);
                    Log.d(TAG,"matchinggame setontouch");
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        Log.d(TAG,"matchinggame setontouch if yes");
                        ClipData data = ClipData.newPlainText("", "");
                        View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
                        v.startDrag(data, shadowBuilder, v, 0);
                        v.setVisibility(View.INVISIBLE); Log.d(TAG,"7isanswered : "+isAnswered);
                        return true;
                    } else { Log.d(TAG,"8isanswered : "+isAnswered);
                        return false;
                    }
                }
            });

            imageAnswer.setOnDragListener(MatchingGame.this);
            Log.d(TAG,"*");

区别是因为它是线程,我不使用this,我使用matchingame.this

我无法弄明白。

这是我的布局:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:id="@+id/relative_layout"
    tools:context=".games.MatchingGame">



</RelativeLayout>

我使用相同的方法和构造来点击事件,它很好用。但为什么不拖延?

当我触摸图像并离开时,它会永远消失。

在动态中,例如当我放到某个地方时,在日志中它说:

"Drag is in progress but there is no drag window handle."

但不在我的标签中,因为w,警告。

使用相同的代码,当我跌落到空的地方时,它会再次变得可见但是当我跌落时它会立即消失。

我看了所有这些 https://www.google.com.tr/search?q=%22%22Drag+is+in+progress+but+there+is+no+drag+window+handle.%22%22&oq=%22%22Drag+is+in+progress+but+there+is+no+drag+window+handle.%22%22&aqs=chrome..69i57.1353j0j9&sourceid=chrome&ie=UTF-8

但它们不同并且无效。

我不知道原因,我猜是因为我使用动态图片添加。

这是ids

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="refresh" type="id"/>
    <item name="settings" type="id"/>
    <item name="imgAnswer" type="id"/>

我也试过那些但没有用的

   imageAnswer = new ImageView(this);
    imageAnswer.setImageBitmap(bmp);
    imageAnswer.setTag(trainingObject.getTrainingobjectAnswer());
    //imageAnswer.findViewById(R.id.imgAnswer);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        rLayout.generateViewId();
        imageAnswer.setId(R.id.imgAnswer);
    }


    rLayout.removeAllViews();
    rLayout.addView(imageAnswer,rLayParams);


     View.generateViewId();
            imageAnswer.setId(R.id.imgAnswer);



 View.generateViewId();
        rLayout.setId(R.id.imgAnswer);

我尝试了所有。

修改

当我尝试shando的建议时,我的日志是:

 imageanswer setontouchlistener
imageanswer setontouchlistener actiondown
 parent android.widget.RelativeLayout{bcd0e74 V.E...... ........ 0,0-320,186 #7f0c0058 app:id/relative_layout}
 view android.widget.ImageView{26eaf9d V.ED..... ........ 12,86-26,100 #1}
 imageanswer setontouchlistener
 ontouch else
 android.widget.RelativeLayout{bcd0e74 V.E...... ......ID 0,0-320,186 #7f0c0058 app:id/relative_layout}
 imageanswer setontouchlistener else: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=16.044922, y[0]=149.61914, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=1104235, downTime=1104203, deviceId=0, source=0x1002 }
 view android.widget.ImageView{26eaf9d I.ED..... ........ 12,86-26,100 #1}

我的代码是这样的,我在api 23模拟器上:

 /*    imageAnswer = new ImageView(this);
        imageAnswer.setImageBitmap(bmp);
        imageAnswer.setTag(trainingObject.getTrainingobjectAnswer());
        imageAnswer.findViewById(R.id.imgAnswer);*/

        int idThatCanBeUsedLater = R.id.imgAnswer;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {  Log.d(TAG,"if");
            idThatCanBeUsedLater = View.generateViewId();}
         imageAnswer = new ImageView(this);
            imageAnswer.setId(idThatCanBeUsedLater);
        imageAnswer.setTag(trainingObject.getTrainingobjectAnswer());
        imageAnswer.setImageBitmap(bmp);

        rLayout.removeAllViews();
        rLayout.addView(imageAnswer,rLayParams);


        imageAnswer.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                imageAnswer.getParent().requestDisallowInterceptTouchEvent(true);
                Log.d(TAG,"imageanswer setontouchlistener");
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    Log.d(TAG,"imageanswer setontouchlistener actiondown");
                    Log.d(TAG,"parent "+ String.valueOf(imageAnswer.getParent()));
                    Log.d(TAG, "view "+String.valueOf(v));
                    ClipData data = ClipData.newPlainText("", "");
                 //  Log.d(TAG,"data: "+data.toString());
                    View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
                    v.startDrag(data, shadowBuilder, v, 0);
                    v.setVisibility(View.INVISIBLE);
                    return true;
                } else {

                    Log.d(TAG, "ontouch else");
                    Log.d(TAG, String.valueOf(imageAnswer.getParent()));
                    Log.d(TAG,"imageanswer setontouchlistener else: "+event.toString());

                    Log.d(TAG,"view "+ String.valueOf(v));
                    if (event.getAction() == MotionEvent.ACTION_CANCEL) {
                        ClipData data = ClipData.newPlainText("", "");
                        View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
                        v.startDrag(data, shadowBuilder, v, 0);
                        v.setVisibility(View.INVISIBLE);
                        return true;}
                    return false;
                }
            }
        });

        imageAnswer.setOnDragListener(this);

这是完整代码

https://gist.github.com/anonymous/7206470ffcee91e6e9d096d0e5756025

2 个答案:

答案 0 :(得分:1)

我认为您的问题是,在初始化onDrag时,您将设置为隐藏您的视图:

ClipData data = ClipData.newPlainText("", "");
                    View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
                    v.startDrag(data, shadowBuilder, v, 0);
                    v.setVisibility(View.INVISIBLE); Log.d(TAG,"7isanswered : "+isAnswered);
                    return true;

另一方面,为什么使用setOnTouchListener代替setOnClickListener?对于这种情况,我认为这不是最好的主意。

最后,我想您没有在onDrag中显示所有代码,因为它看起来完全是空的。但是,你必须在Switch内部的每个案例中返回true以继续下一个(并在ACTION_DRAG_ENDED中返回false)

所以你的onDrag需要像:

//Your code
    imageAnswer.setOnDragListener(this);
    imageAnswer.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
            v.startDrag(data, shadowBuilder, v, 0);
            v.setVisibility(View.INVISIBLE); Log.d(TAG,"7isanswered : "+isAnswered);
            return true;
        }
    });

    rLayout.removeAllViews();
    rLayout.addView(imageAnswer,rLayParams);

}

Override
public boolean onDrag(View receivingLayoutView, DragEvent dragEvent) {
    Log.d(TAG, "ondrag start");

    switch (dragEvent.getAction()) {

        case DragEvent.ACTION_DRAG_STARTED:
            //Do something
            return true;
        case DragEvent.ACTION_DRAG_ENTERED:
            //Do something
            return true;
        case DragEvent.ACTION_DRAG_LOCATION:
            //Do something
            return true;
        case DragEvent.ACTION_DRAG_EXITED:
            //Do something
            return true;
        case DragEvent.ACTION_DROP:
            //Do something
            return true;

        case DragEvent.ACTION_DRAG_ENDED:
            //Do something
            return false;



    }

答案 1 :(得分:0)

您在代码中设置了视图ID。你能删除imageAnswer.setId(R.id.imgAnswer);或者,如果您使用min SDK 17将其替换为generateViewId。可能是潜在重复ID内的问题。