Is it possible to make a shadow with a shadow opaque?

How is the question asked. here is my dragshadowbuilder for reference. As you know, the shadow created by drag and drop is translucent. Is there any way to make this opaque? (completely solid)

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

EDIT: Here is my current onTouch code, I also provided the application background.

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

Well, here's what you need to know. I am developing a chess application. I have an 8x8 GridLayout , which I called currentBoardState . So currently I want to be able to drag a piece that works, but I want the shuffled part to be opaque, so the user feels that he / she is actually MOVING , and not just a click then click on the destination, like before.

+5
source share
3 answers

As @Ernir says, the API API does not receive DragShadow. But we know the location of the drag and drop so that we can draw it ourselves.

I am writing a simple and functional limited demo that you can download here .

It implements a custom ViewGroup called DragContainer. Use it to wrap the original root view in your xml layout.

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

call DragContainer # startDragChild to start dragging (see demo for more details):

In action:

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

You can also extend the DragContainer to draw a custom drag and drop shadow. This is an example of your requirement.

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

Hope this helps.

+9
source

I think the correct way to achieve this is:

 /* Create the shadow from a view */ View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); /* Access the view created and set the alpha of it to 1 (totally opaque)*/ shadowBuilder.getView().setAlpha(1); /* Start dragging the object */ view.startDrag(data, shadowBuilder, view, 0); 

EDIT This seems to not work in 4.4 and above :(

+3
source

No, unfortunately, this is not possible, as far as I can tell.

A Bitmap shadow is drawn on a surface that you cannot reach. The only solution is to implement Drag and Drop, as was done before API 11. It is actually not as complicated as you think.

Some links to run:

Link 1

Link 2 * updated

+2
source

Source: https://habr.com/ru/post/988160/


All Articles