Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

android - Is it possible to make a drag shadow opaque?

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? (fully 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'll also provide a background of the app.

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

Ok so here's what you need to know. I am developing a chess application. I have an 8x8 GridLayout which I have named currentBoardState. So currently I want to be able to drag the piece, which works, but I want the dragged piece to be opaque so the user gets the feeling that he/she is actually MOVING the piece rather than just clicking then clicking on a destination as I previously had it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Dont know if someone is aware of it or not, We can achieve the same using flag DRAG_FLAG_OPAQUE, So the correspondind startDrag method will look like as below:

    View.DragShadowBuilder dragShadowBuilder = new View.DragShadowBuilder(view);
    view.startDrag(clipData, dragShadowBuilder, view, DRAG_FLAG_OPAQUE);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...