是否有可能使一拖影不透明? [英] Is it possible to make a drag shadow opaque?

查看:237
本文介绍了是否有可能使一拖影不透明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于问题的状态。这是我的dragshadowbuilder仅供参考。如你所知,当你拖动时创建的阴影是半透明的。反正,使之不透明? (全固体)

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

编辑:这是我目前的onTouch code,我还将提供应用程序的背景

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

好了,所以这里就是你需要知道的。我开发一个国际象棋的应用程序。我有一个8x8的网格布局我有一个名为 currentBoardState 。所以,目前我希望能够拖动一块,这工作,但我想,使用户得到的感觉,他/她其实是拖着一块不透明的移动的一块,而不是仅仅点击然后点击一个目的地,我previously了吧。

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.

推荐答案

由于@Ernir说,有没有API获得DragShadow。但我们知道的阻力位置和拖动视图,以便我们可以得出它自己。

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.

据实施名为DragContainer一个自定义的ViewGroup,用它来包装你的布局XML原来的根视图。

It implement a custom ViewGroup named DragContainer, Use it to wrap your original root view in your 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>

调用DragContainer#startDragChild开始拖动这样(演示有更多细节):

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

在活动:

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

            }
        });


    }

您还可以扩展DragContainer绘制自定义的拖影,这是你要求的例子。

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.

这篇关于是否有可能使一拖影不透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆