gpt4 book ai didi

java - 在 Android 中移动可绘制对象时遇到问题

转载 作者:行者123 更新时间:2023-12-01 11:48:02 24 4
gpt4 key购买 nike

我在使用我设置的一些代码时遇到了问题,这些代码允许我只需连续单击两个 imageView(一个包含源图像,然后是目标 imageView)即可将 imageView 的可绘制对象移动到另一个 imageView。

目标是模拟棋盘。

我的起始显示如下:enter image description here

点击最左边的棋子,然后点击它前面的方 block ,结果如下。您将看到目标 imageView(第一列第三行)正在失去其水平边界定义并扩展到整个 tableRow:

现在,我还重置标签以及每个方 block 中的可绘制对象,但我想问题源于我获取和设置可绘制对象的方式。

我的代码如下:

int selectedId = getResources().getIdentifier(selectedSquare, "id", getPackageName());

ImageView sourceSquare = (ImageView) findViewById(selectedId);

Drawable sourceDrawable = sourceSquare.getDrawable();

ImageView targetSquare = (ImageView) findViewById(R.id.s20);
targetSquare.setImageDrawable(sourceDrawable);

我的猜测是,我正在创建的 sourceDrawable 变量以某种方式获取了太多或太少的信息,当我将该可绘制对象移动到新的 imageView targetSquare 中时,这会破坏我的格式化。

最佳答案

您可以为新的 ImageView 提供原点的源 LayoutParams:

targetSquare.setLayoutParams(sourceSquare.getLayoutParams);

关于java - 在 Android 中移动可绘制对象时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29001145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com