java - 在 Android 上移动 ImageView

标签 java android xml imageview

我正在尝试编写一个应用程序来玩蛇和梯子(这是我编写的第一个应用程序,我只是为了好玩...而不是为了类(class)或商业目的或任何其他目的) .

所以我的问题是,我想将玩家标记的 ImageView 从棋盘上的一个方格移动到另一个方格。我不知道如何做两件事:

  1. 如何将 token 的 ImageView 放置在布局中板 ImageView 的顶部?

  2. 如何将 token ImageView 本身移动板 ImageView 大小的特定百分比?这是可能的还是我应该以其他方式做到这一点?

我有为玩家、棋盘和方 block 编写的类(class),它们都可以正常工作。我只需要知道如何处理玩家的 token 。 (我通过在每次掷骰子时显示一条说明玩家位置的消息来测试它)。

这是布局的 xml 代码:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center" 
    android:background="@drawable/stripes_background"
    android:orientation="vertical" >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_gravity="center"
        android:layout_marginTop="0dp"
        android:layout_weight="0.1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/toGameListButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.00"
            android:background="@drawable/alternate_button_background"
            android:text="Quit"
            android:textColor="#FFFFFF"
            android:textSize="17sp" />

        <Button
            android:id="@+id/restartButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.00"
            android:background="@drawable/alternate_button_background"
            android:text="Restart"
            android:textColor="#FFFFFF"
            android:textSize="17sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/textViewTurn"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:gravity="center"
        android:layout_weight="0.1"
        android:text="TextView set to Player&apos;s turn"
        android:textColor="#FF9900"
        android:textSize="19sp"
        android:textStyle="bold" />

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="0.7" >

        <ImageView
            android:id="@+id/game_board"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:contentDescription="Board"
            android:src="@drawable/snakes_and_ladders_raw_board" />

        <ImageView
            android:id="@+id/green_indicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </FrameLayout>

    <Button
        android:id="@+id/rollButton"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_marginTop="10dp"
        android:layout_weight="0.1"
        android:background="@drawable/alternate_button_background"
        android:text="Roll!"
        android:textColor="#FFFFFF"
        android:textSize="19sp" />

</LinearLayout>

非常感谢任何建议。如果我应该更具体地说明我的问题或其他任何事情,请告诉我。

最佳答案

How can I place the imageview of the token on top of the board imageview in the layout?

您可以简单地将两个 ImageView 放入 RelativeLayout 中,然后确保 token 的 ImageView 位于(在代码中)板的 ImageView 下方。

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/game_board"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:contentDescription="Board"
            android:src="@drawable/snakes_and_ladders_raw_board" />

        <!-- views below in code will be on top -->
        <ImageView
            android:id="@+id/green_indicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </RelativeLayout>

How can I go about moving the token imageview itself by a certain % of the size of the board imageview? Is this possible or am I supposed to do this some other way?

您可以访问板的 ImageView 宽度属性并相应地为 token ImageView 设置动画。

ImageView board = (ImageView) findViewById(R.id.board);
ImageView token = (ImageView) findViewById(R.id.token);

// animates the token imageview to the right side by 50% of the boardimageviews width
token.animate().x(board.getWidth() / 2).setDuration(500);

关于java - 在 Android 上移动 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18172058/

相关文章:

java - 创建一个 DOCX 从 Oracle 数据库读取数据

android - 从远程服务器获取图像

android - 如何通过 DialogFragment 添加 View ?

php - 使用 XPath 提取 xml 数据

java - Hamcrest 或assertj : test element in list with requirements

java - 如何在 ListView 中显示格式如下 "12:30 PM"的 TextView ?

java - matcher.group() 的内存分配

javascript - 在 Android 的 WebView 中放宽 MIME 类型检查?或者强制常规javascript的模块类型?

java - Java中如何使用XPath检查节点是否具有一个或多个特定属性?

android - 样式包含错误条目 : 0x01010479 (Android Studio) 的键