正如问题所述.这是我的dragshadowbuilder供参考.如您所知,拖动时创建的阴影是半透明的.反正有没有让它变得不透明?(完全稳固)
public class MyDragShadowBuilder extends View.DragShadowBuilder {
View view;
int width;
int height;
Bitmap bitmap;
public MyDragShadowBuilder(View v) {
super(v);
view = v;
ImageView b = (ImageView)view;
int x = Character.getNumericValue(b.getContentDescription().charAt(0));
int y = Character.getNumericValue(b.getContentDescription().charAt(1));
bitmap = InGameActivity.currentBoardState[x][y].getPicture();
width = bitmap.getWidth();
height = bitmap.getHeight();
}
@Override
public void onDrawShadow(Canvas canvas) {
Rect source = new Rect(0, 0, InGameActivity.chessSquareHeightWidth, InGameActivity.chessSquareHeightWidth);
canvas.drawBitmap(bitmap, null, source, null);
}
@Override
public void onProvideShadowMetrics(Point shadowSize, Point touchPoint) {
shadowSize.set(InGameActivity.chessSquareHeightWidth, InGameActivity.chessSquareHeightWidth);
touchPoint.set(InGameActivity.chessSquareHeightWidth/2, InGameActivity.chessSquareHeightWidth/2);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:这是我目前的onTouch代码,我还将提供该应用程序的背景.
OnTouchListener myOnTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
System.out.println(event.toString());
if (event.getAction() == MotionEvent.ACTION_DOWN) {
ImageView b = (ImageView)view;
int x = Character.getNumericValue(b.getContentDescription().charAt(0));
int y = Character.getNumericValue(b.getContentDescription().charAt(1));
if (!currentBoardState[x][y].isEmpty()) {
((ImageView)view).setImageBitmap(null);
DragShadowBuilder shadowBuilder = new MyDragShadowBuilder(view);
view.startDrag(null, shadowBuilder, view, 0);
}
moveChessPiece;
return true;
}
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
好的,这就是你需要知道的.我正在开发一个象棋应用程序.我有一个8x8 GridLayout,我已经命名了currentBoardState.所以目前我希望能够拖动这件作品,但是我希望被拖动的作品是不透明的,这样用户就会觉得他/她实际上正在移动这件作品,而不是仅仅点击然后点击一个目的地就像我一样以前有它.
正如@Ernir所说,没有API获得DragShadow.但我们知道拖动位置和拖动视图,因此我们可以自己绘制它.
我写了一个简单的功能有限的演示,你可以在这里下载.
它实现了一个名为DragContainer的自定义ViewGroup,使用它将原始根视图包装在layout xml中.
<info.dourok.android.demo.DragContainer xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- original root view -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- ... -->
</RelativeLayout>
</info.dourok.android.demo.DragContainer>
Run Code Online (Sandbox Code Playgroud)
调用DragContainer #startDragChild来开始拖动(演示有更多细节):
在活动中:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDragContainer = (DragContainer) findViewById(R.id.root);
View drag = mDragContainer.findViewById(R.id.drag);
View drop = mDragContainer.findViewById(R.id.drop);
drag.setTag(IMAGEVIEW_TAG);
drag.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence) v
.getTag());
ClipData dragData = new ClipData((CharSequence) v.getTag(),
new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN },
item);
return mDragContainer.startDragChild(v, dragData, // the data to
// be
// dragged
null, // no need to use local data
0 // flags (not currently used, set to 0)
);
}
});
}
Run Code Online (Sandbox Code Playgroud)
您还可以扩展DragContainer以绘制自定义拖动阴影,这是您的需求的示例.
public class MyDragContainer extends DragContainer {
public MyDragContainer(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyDragContainer(Context context) {
super(context, null);
}
public MyDragContainer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
int width;
int height;
Bitmap bitmap;
@Override
protected void onSetDragTarget(View v) {
ImageView b = (ImageView) v;
int x = Character.getNumericValue(b.getContentDescription().charAt(0));
int y = Character.getNumericValue(b.getContentDescription().charAt(1));
bitmap = InGameActivity.currentBoardState[x][y].getPicture();
width = bitmap.getWidth();
height = bitmap.getHeight();
}
@Override
protected void drawDragShadow(Canvas canvas) {
Rect source = new Rect(0, 0, InGameActivity.chessSquareHeightWidth, InGameActivity.chessSquareHeightWidth);
int w = InGameActivity.chessSquareHeightWidth;
int h = InGameActivity.chessSquareHeightWidth;
canvas.translate(getDragX() - w / 2, getDragY() - h / 2);
canvas.drawBitmap(bitmap, null, source, null);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望它会有所帮助.
小智 5
不知道是否有人知道它,我们可以使用标志DRAG_FLAG_OPAQUE来实现相同的功能,因此对应的startDrag方法如下所示:
View.DragShadowBuilder dragShadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(clipData, dragShadowBuilder, view, DRAG_FLAG_OPAQUE);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5392 次 |
| 最近记录: |