有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java找不到移动图像(或其他可单击对象)的方法

 public class FirstTest extends Activity {
        public FirstTest() {
            // TODO Auto-generated constructor stub
        }

        RelativeLayout currentLayout;

        static int[] Deck = {
            R.drawable.img1,
            R.drawable.img2,
            R.drawable.img3,
            R.drawable.img4
        };

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            currentLayout = new RelativeLayout(this);

            for (int i = 0; i < Deck.length; i++) {         
                ImageButton img = new ImageButton(this);
                img.setPadding(0, 0, 0, 0);
                img.setImageResource(Deck[i]);
                img.setAdjustViewBounds(true);
                img.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        // BUT HOW MOVE THIS BUTTON????
                    }
                });

                currentLayout.addView(img);
            }
            setContentView(currentLayout);
        }
    }

也许这对其他人来说是微不足道的,但我只是找到了宽度和高度属性修饰符。在尝试了很多例子之后,我放弃了

我怎样才能移动东西?为什么我找不到x-y属性


共 (1) 个答案

  1. # 1 楼答案

    在你的评论中,你提到了图像到达目的地时是如何“闪烁”的,然后你毫无疑问地将图像翻译设置到了新的位置

    经过多次测试,我发现移动图像(或任何视图)的最佳方法是按以下顺序执行步骤:

    点击事件: 1.找到图像要移动到的位置的坐标 2.将图像翻译(设置位置)到你希望它完成的地方 3.启动一个动画,从图像最初的位置开始,并在其当前位置(0,0)结束

    因为图像已被移动到结束位置,然后立即开始动画,所以在动画开始时不会出现任何闪烁,然后当动画结束时,它将恰好在图像已设置的位置结束,因此在动画结束时也不会出现任何闪烁

    这有点奇怪,但我相信这只是因为Android如何触发开始/结束动画事件

    我希望这是有道理的