【问题标题】:XML image switchingXML 图像切换
【发布时间】:2020-05-21 17:48:55
【问题描述】:

我是编码新手,我正在进行文本冒险,希望 imageview 根据位置更改图像,我想知道它是如何工作的,为什么这个解决方案不工作。根据玩家的位置来做会更好吗?我在哪里可以找到有关这方面的信息,因为我遇到了一些麻烦。 谢谢

<room>
        <east>1</east>
        <description>Room 1(0)</description>
        <ImageView
            android:id="@+id/ImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/bedroom"
            />
    </room>
    <room>
        <east>2</east>
        <west>0</west>
        <description>Room 2(1)</description>
        <ImageView
            android:id="@+id/imagePlace1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/living"
            />
    </room>
    <room>
        <west>1</west>
        <description>Room 3(2)</description>
        <ImageView
            android:id="@+id/imagePlace2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/kitchen"
            />
    </room>```

【问题讨论】:

    标签: xml android-studio imageview


    【解决方案1】:

    如果您只使用 1 个图像视图并根据位置更改图像会很好

    要更改图像,请使用以下 java 代码:

    imageview_object.setImageResource(R.drawable.image_name);
    

    确保您的图片位于 res/drawable 文件夹中

    要获取纬度和经度位置,您可以点击此链接https://javapapers.com/android/get-current-location-in-android/

    【讨论】:

    • 嘿,它在一个图像视图中,但我想像这样在 xml 中处理它是错误的方式?因为它甚至不会显示 1 张图片。位置如您在 xml 文件中看到的,起始房间是 0,向东是房间 1,东又是房间 2,而西是回到房间 0,依此类推。
    • 好吧,如果您只使用一个图像视图,那么首先将您要查看的默认图像设置给用户,然后根据用户更改房间使用我给出的 java 代码来更改图像到房间。
    猜你喜欢
    • 2013-06-20
    • 1970-01-01
    • 2020-09-05
    • 2020-10-01
    • 2011-07-03
    • 2012-01-21
    • 1970-01-01
    • 2015-07-26
    • 2013-01-30
    相关资源
    最近更新 更多