gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:39:17 25 4
gpt4 key购买 nike

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

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

当我运行并且第二次只触摸 ImageView 时,日志是:

imageanswer setontouchlistener
imageanswer setontouchlistener actiondown
imageanswer setontouchlistener
imageanswer setontouchlistener else

它甚至不叫拖动。它不能进入​​阻力。

我试图让这个类扩展 Activity 但没有任何改变。

之前,为了测试我创建了不同的类并且它正在运行。不同之处在于,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

但它们是不同的并且不起作用。

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

这是ID

<?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

最佳答案

我认为您的问题是您在初始化 onDrag 时将 View 设置为 INVISIBLE:

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;



}

关于Android,动态创建的图片无法运行拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37233539/

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