我正在开发一款包含拖放功能的游戏。它适用于除三星Galaxy S6以外的所有设备。
class MyDragListener implements View.OnDragListener {
Drawable normalShape = getResources().getDrawable(R.drawable.normal_shape);
Drawable targetShape = getResources().getDrawable(R.drawable.target_shape);
@Override
public boolean onDrag(View v, DragEvent event) {
// Handles each of the expected events
switch (event.getAction()) {
//signal for the start of a drag and drop operation.
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
//the drag point has entered the bounding box of the View
case DragEvent.ACTION_DRAG_ENTERED:
//v.setBackground(targetShape); //change the shape of the view
break;
//the user has moved the drag shadow outside the bounding box of the View
case DragEvent.ACTION_DRAG_EXITED:
break;
//drag shadow has been released,the drag point is within the bounding box of the View
case DragEvent.ACTION_DROP:
View view = (View) event.getLocalState();
// There is some code here
break;
//the drag and drop operation has concluded.
case DragEvent.ACTION_DRAG_ENDED:
//v.setBackground(normalShape); //go back to normal shape
default:
break;
}
return true;
}
}
class MyClickListener implements View.OnTouchListener {
@Override
public boolean onTouch(View view, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// create it from the object's tag
ClipData.Item item = new ClipData.Item((CharSequence) view.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData data = new ClipData(view.getTag().toString(), mimeTypes, item);
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, //data to be dragged
shadowBuilder, //drag shadow
view, //local data about the drag and drop operation
0 //no needed flags
);
view.setVisibility(View.INVISIBLE);
return true;
}
else if (event.getAction() == MotionEvent.ACTION_UP){
view.setVisibility(View.VISIBLE);
return true;
}
else
{
return false;
}
}
}
构建gradle看起来像这样。
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "________"
minSdkVersion 16
targetSdkVersion 22
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
debug {
debuggable false
}
}
}
dependencies {
compile 'com.android.support:support-v4:23.0.0'
compile 'com.android.support:appcompat-v7:23.1.0'
}
在三星Galaxy S6(Android版本5.1.1)拖放后,图像会消失。控制台中显示的错误是 ViewRootImpl:报告丢弃结果:false 该应用在任何其他设备上都没有问题。这是API问题还是什么?为了您的信息,我检查了solution,但这无法解决我的问题。
答案 0 :(得分:0)
检查此代码,它将解决问题
(click)=""
答案 1 :(得分:-1)
为了防止图像消失,我建议添加:
if (!isDropped) {
((View) dragEvent.getLocalState()).setVisibility(View.VISIBLE);
}
在onDrag(...)
方法的末尾,其中isDropped
是一个声明为boolean
的全局false
变量,但在ACTION_DROP
案例中使其等于到true
。