【问题标题】:Android divide an image in multiple region for onClick eventsAndroid为onClick事件划分多个区域的图像
【发布时间】:2019-02-20 21:07:58
【问题描述】:

在这个应用程序中,我有一个代表德国地图的图像。我希望能够点击一个特定的州,例如,巴伐利亚州和应该发生的事情(点击功能)。

我可以在填充空白图像的图像顶部放置一个表格布局,并仅在覆盖状态的图像上激活点击方法,但这可能是糟糕的编码,我认为与其他类型的兼容性很差设备、平板电脑或更大/更小的屏幕。

另一种解决方案是创建两个地图图像。一种具有不同颜色的状态,另一种具有要显示的所需布局。将彩色的不可见的放在第二个的顶部。

XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/my_frame"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:background="@color/background" >
 <ImageView 
     android:id="@+id/image_areas"
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:scaleType="fitCenter"
     android:visibility="invisible"
     android:src="@drawable/mapcolor" />

 <ImageView
     android:id="@+id/image"
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:scaleType="fitCenter"
     android:src="@drawable/mapdisplay"/>
</FrameLayout>

Java:

public boolean onTouch (View v, MotionEvent ev) {
     final int action = ev.getAction();
     // (1) 
     final int evX = (int) ev.getX();
     final int evY = (int) ev.getY();
     switch (action) {
     case MotionEvent.ACTION_DOWN :
       if (currentResource == R.drawable.mapdisplay) { 
       } 
       break;
     case MotionEvent.ACTION_UP :
       // Switch to a different image, depending on what color was touched.
       int touchColor = getHotspotColor (R.id.image_areas, evX, evY);
       // Switch to a different image, depending on what color was touched.
       ColorTool ct = new ColorTool ();
       int tolerance = 25;
       nextImage = R.drawable.mapdisplay;
       // (3)
       if (ct.closeMatch (Color.RED, touchColor, tolerance)) {
          // Do the action associated with the RED region
          // onClick function here ?
       } else {
         //...
       }
       break;
      } // end switch
      return true;
}

颜色工具:

public class ColorTool {
    public boolean closeMatch (int color1, int color2, int tolerance) {
        if ((int) Math.abs (Color.red (color1) - Color.red (color2)) > tolerance ) return false;
        if ((int) Math.abs (Color.green (color1) - Color.green (color2)) > tolerance ) return false;
        if ((int) Math.abs (Color.blue (color1) - Color.blue (color2)) > tolerance ) return false;
        return true;
    } // end match
} // end class

正如预期的那样,这对我不起作用。有人可以向我解释一下这种方法还是一种有多个区域可供点击的图像“地图”的好方法?

【问题讨论】:

    标签: java android split onclick imageview


    【解决方案1】:

    我正在考虑使用具有状态图的 2D 矩阵。应该有一个初始版本的矩阵,它根据不同区域的位置映射不同的数字。对于更大或更小的屏幕尺寸,矩阵需要是动态的并在像素之间重新映射。让我们以下面的网格为例。

    --------------
    00111122224444
    01111222222444
    11122222444444
    11222244444433
    12222444444333
    22224444444433
    22255444444333
    22555554443333
    25555555433333
    --------------
    

    我们可以在这里看到 5 个状态,它们在地图后面的矩阵中分别编号。如果屏幕尺寸翻倍,则需要将值转换为更大的段,反之亦然。

    我正在考虑绘制这样的矩阵,它可以根据屏幕尺寸转换成更大或更小的尺寸。现在您需要将地图的图像设置为屏幕背景并获取onTouchListener 以获取触摸区域的像素以确定正在单击的区域。

    这只是为了说明如何解决此问题。我发现了一个类似的想法here in the MapChart。希望有帮助!

    【讨论】:

      猜你喜欢
      • 2020-09-04
      • 2018-05-08
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      相关资源
      最近更新 更多