【问题标题】:Touching Multiple Images With Hold In Android在 Android 中使用 Hold 触摸多个图像
【发布时间】:2011-07-03 10:24:33
【问题描述】:

我有这个main.xml 文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
>

<ImageView 
    android:id="@+id/img1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/z01"
    />
<ImageView 
    android:id="@+id/img2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/z02"
    />

</LinearLayout>

我想要实现的是:

  • 当用户长按img1时,图片变为按下的图片(即从z01.png变为z01_pressed.png)
  • 当(例如)用户在按住时从 img1 移动到 img2,img2 被按下(将其图片从 z02.png 更改为 z02_pressed.png)并且 img1 返回到其状态(到 z01.png)。李>

为了实现这一点,我在onCreate 方法中写了这个:

    final ImageView img1 = (ImageView) findViewById(R.id.img1);
    final ImageView img2 = (ImageView) findViewById(R.id.img2);

    img1.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            img1.setBackgroundResource(R.drawable.z01_pressed);
            return false;
        }
    });
    img2.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            img2.setBackgroundResource(R.drawable.z01_pressed);
            return false;
        }
    });

但是,这不起作用。我是不是搞错了?

【问题讨论】:

    标签: android imageview touch-event


    【解决方案1】:

    您可以使用按钮并将自定义图像设置为该按钮的可绘制对象。这样,您就不必自己管理已按下和未按下的状态。请参阅此链接:

    http://blog.androgames.net/40/custom-button-style-and-theme/

    【讨论】:

    • 我正在这样做。但这对我没有帮助。我不能触摸一个按钮,然后按住并移动到另一个按钮来触摸它。不起作用。我试过了。
    • 啊,我明白了。在这种情况下,您走在正确的轨道上。尝试调用 myImage.invalidate();更改可绘制对象后。
    • 还是没有解决。我想我需要一些 Listeners 而不是 TouchListener
    【解决方案2】:

    致那些可能感兴趣的人:

    这可以使用 OnDragListener 来完成。它需要一些工作来实现这一点。为了让我的生活更轻松,我放弃了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-10
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-09
      相关资源
      最近更新 更多