Is it possible to make a drag shadow opaque?

前端 未结 5 1055
粉色の甜心
粉色の甜心 2020-12-16 06:14

As question states. here is my dragshadowbuilder for reference. As you know, the shadow that is created when you drag is translucent. Is there anyway to make it opaque? (ful

5条回答
  •  孤城傲影
    2020-12-16 06:39

    As @Ernir says, there no API get DragShadow. But we know the drag location and drag view so we can draw it ourselves.

    I write a simple and function limited demo you can download it here.

    It implement a custom ViewGroup named DragContainer, Use it to wrap your original root view in your layout xml.

    
    
        
        
             
        
    
    
    

    call DragContainer#startDragChild to start drag like that(the demo have more detail):

    in Activity:

    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 also can extend DragContainer to draw custom drag shadow, This is a example for 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);
        }
    }
    

    I hope it will help.

提交回复
热议问题