我使用DragShadowBuilder.java
,這個類是爲了一個樣本。Android - 如何使用DragShadowBuilder?
我不知道我怎麼可以用我的Activity
,我應該送什麼參數constructor
DrawableDragShadowBuilder
的:
感謝的很多。
我使用DragShadowBuilder.java
,這個類是爲了一個樣本。Android - 如何使用DragShadowBuilder?
我不知道我怎麼可以用我的Activity
,我應該送什麼參數constructor
DrawableDragShadowBuilder
的:
感謝的很多。
你要通過要拖動View
,在DragShadowBuilder
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
構造函數,然後使用啓動拖動:
view.startDrag(data, shadowBuilder, view, 0);
這裏是以下這個優秀的完整的示例tutorial:
import android.app.Activity;
import android.content.ClipData;
import android.graphics.drawable.Drawable;
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;
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());
findViewById(R.id.myimage2).setOnTouchListener(new MyTouchListener());
findViewById(R.id.myimage3).setOnTouchListener(new MyTouchListener());
findViewById(R.id.myimage4).setOnTouchListener(new MyTouchListener());
findViewById(R.id.topleft).setOnDragListener(new MyDragListener());
findViewById(R.id.topright).setOnDragListener(new MyDragListener());
findViewById(R.id.bottomleft).setOnDragListener(new MyDragListener());
findViewById(R.id.bottomright).setOnDragListener(new MyDragListener());
}
private final class MyTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
}
class MyDragListener implements OnDragListener {
Drawable enterShape = getResources().getDrawable(R.drawable.shape_droptarget);
Drawable normalShape = getResources().getDrawable(R.drawable.shape);
@Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
v.setBackgroundDrawable(enterShape);
break;
case DragEvent.ACTION_DRAG_EXITED:
v.setBackgroundDrawable(normalShape);
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) v;
container.addView(view);
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
v.setBackgroundDrawable(normalShape);
default:
break;
}
return true;
}
}
}
如果您有任何我的代碼示例,請聯繫我。非常感謝。 –
好的,我會:) –
I找到範例我的問題:
我StartActivity.java
:
public class StartActivity extends Activity implements View.OnLongClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
findViewById(R.id.imgv).setOnLongClickListener(this);
}
@Override
public boolean onLongClick(View v) {
View.DragShadowBuilder shadowBuilder = new DrawableDragShadowBuilder(v,
getResources().getDrawable(R.drawable.ic_launcher));
v.startDrag(null, shadowBuilder, ((ImageView) v).getDrawable(), 0);
return true;
}
}
而且activity_start.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StartActivity">
<ImageView
android:id="@+id/imgv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
</RelativeLayout>
更簡單的方法,您可以實例'DragShadowBuilder'並傳遞要視圖實例被拖拽到它的構造函數中。如果你想要,我可以給出一個使用它作爲答案的例子嗎? –
是的,我需要。我不知道我應該發送給DrawableDragShadowBuilder的構造函數的參數。 –
不要使用上面的例子...而是使用一個非常簡單的例子,我會發布如果你。 –