0

在 Windows 上做 C# 多年后,我刚刚回到 Android,并想将我的通用应用程序移植到 Android。这是基于纸牌的纸牌接龙版本。本质上,我所做的是定义了一个包含许多预定义 ImageView 位置的相对布局。我最初使用卡片的虚拟值填充所有内容,以确保布局看起来不错。完成后,我开始实现游戏逻辑。当我从卡组“drawCard”中拖动一张卡片时,如果用户将其释放到一个空的(且有效的)ImageView 对象上,则 drawCard 的值应分配给它被释放的空 ImageView 对象,而 drawCard应该回到原来的位置,但是根据牌组中的下一张牌有一个新的值。我的问题是计算抽奖卡的位置以确定它是否悬停在有效的 ImageView 目标上。这是 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/gamePage"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.cardgames.tony.solitairecribbage.GamePageActivity"
tools:showIn="@layout/activity_game_page"
android:background="#041a01">


    <ImageView
        android:layout_width="90dp"
        android:layout_height="106dp"
        android:id="@+id/hand1Card1"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>
        <!--android:src="@drawable/aofc"/>-->

    <ImageView
        android:layout_width="90dp"
        android:layout_height="106dp"
        android:id="@+id/hand1Card2"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="36dp"/>
        <!--android:src="@drawable/c3ofs"/-->

    <ImageView
        android:layout_width="90dp"
        android:layout_height="106dp"
        android:id="@+id/hand1Card3"
        android:layout_alignBottom="@+id/hand2Card4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="34dp" />

    <ImageView
        android:layout_width="90dp"
        android:layout_height="106dp"
        android:id="@+id/hand1Card4"
        android:layout_below="@+id/hand1Card1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

(另外 3 手也有类似的声明。下面是卡片组和 drawCard,即用户拖动的那张):

    <ImageView
        android:layout_width="90dp"
        android:layout_height="106dp"
        android:id="@+id/cardDeck"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="113dp"
        android:src="@drawable/b1fv"/>

    <ImageView
        android:layout_width="90dp"
        android:layout_height="106dp"
        android:id="@+id/drawCard"
        android:layout_alignTop="@+id/cardDeck"
        android:layout_toRightOf="@+id/cardDeck"
        android:layout_toEndOf="@+id/cardDeck"/>

我过去所做的是使用绝对位置来计算卡片的有效“放置区”。使用相对布局,我不确定如何执行此操作。理想情况下,我会计算我正在拖动的卡片的死点坐标以及有效“放置区”的左上角和右下角坐标。如果坐标在这些值之间,则卡片将被释放,并且还会在原始位置绘制一张新卡片。

这是我正在做的一个片段,但它并没有给我准确的值:

    case MotionEvent.ACTION_UP: {                        

                    //Getting X and Y coordinates for event position
                    float tempX = event.getX();
                    float tempY = event.getY();

                    //Attempt to get dead center point for card being dragged:
                    int height = finalTempCardPicture.getDrawable().getIntrinsicHeight();
                    int width = finalTempCardPicture.getDrawable().getIntrinsicWidth();
                    tempX += width/2;
                    tempY += height/2;

尝试计算有效的放置区:

   //fixCardPosition tries to place the card in a valid empty ImageView
                    if (!fixCardPosition(tempX, tempY))
                    {
                        //Return the draw card to the deck if the move is invalid
                        layoutParams.topMargin = drawOriginY;
                        layoutParams.leftMargin = drawOriginX;
                        v.setLayoutParams(layoutParams);
                    }

这是尝试确定位置的逻辑:

        public boolean isDraggedToFirstHand(int xCoord, int yCoord)
{
    int topLeftExtentX;
    int topLeftExtentY;
    int bottomRightExtentX;
    int bottomRightExtentY;
    ImageView tempImage;
    tempImage = (ImageView)findViewById(R.id.hand1Card1);
    topLeftExtentX = tempImage.getLeft();
    topLeftExtentY = tempImage.getTop();
    tempImage = (ImageView)findViewById(R.id.hand1Card4);
    bottomRightExtentX = tempImage.getRight();
    bottomRightExtentY = tempImage.getBottom();
    return (xCoord >= topLeftExtentX && yCoord >= topLeftExtentY && xCoord < bottomRightExtentX && yCoord < bottomRightExtentY);

布局应该是这样的:

纸牌布局

很抱歉这篇冗长的帖子,但我想尝试确保我在这里有足够的信息让我的问题有意义。

4

0 回答 0