gpt4 book ai didi

Android - 如何使用 DragShadowBuilder?

转载 作者:行者123 更新时间:2023-11-29 01:29:18 30 4
gpt4 key购买 nike

我正在使用 DragShadowBuilder.java,这个类是一个示例。

我不知道如何在我的 Activity 中使用,我应该将哪些参数发送到 DrawableDragShadowBuilderconstructor:

public class DrawableDragShadowBuilder extends DragShadowBuilder {
private Drawable mDrawable;

public DrawableDragShadowBuilder(View view, Drawable drawable) {
super(view);
// Set the drawable and apply a green filter to it
mDrawable = drawable;
mDrawable.setColorFilter(new PorterDuffColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY));
}

@Override
public void onProvideShadowMetrics(Point shadowSize, Point touchPoint) {
// Fill in the size
shadowSize.x = mDrawable.getIntrinsicWidth();
shadowSize.y = mDrawable.getIntrinsicHeight();
// Fill in the location of the shadow relative to the touch.
// Here we center the shadow under the finger.
touchPoint.x = mDrawable.getIntrinsicWidth() / 2;
touchPoint.y = mDrawable.getIntrinsicHeight() / 2;

mDrawable.setBounds(new Rect(0, 0, shadowSize.x, shadowSize.y));
}

@Override
public void onDrawShadow(Canvas canvas) {
//Draw the shadow view onto the provided canvas
mDrawable.draw(canvas);
}
}

非常感谢。

最佳答案

您必须在 DragShadowBuilder 的构造函数中传递要拖动的 View

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;
}
}
}

关于Android - 如何使用 DragShadowBuilder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32198394/

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