【发布时间】: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