Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何选择图像的自定义形状区域?_Android - Fatal编程技术网

Android 如何选择图像的自定义形状区域?

Android 如何选择图像的自定义形状区域?,android,Android,是否有可能检测到先前定义的图像区域上的点击 例如: 换句话说,是否可以动态定义形状的边界?还是有必要对它们进行硬编码?还有其他可能的解决方案吗?您可以使用触摸位置并映射到自定义形状的区域 @Override public boolean onTouchEvent(MotionEvent event) { int x = (int)event.getX(); int y = (int)event.getY(); switch (event.getAction()) {

是否有可能检测到先前定义的图像区域上的点击

例如:


换句话说,是否可以动态定义形状的边界?还是有必要对它们进行硬编码?还有其他可能的解决方案吗?

您可以使用触摸位置并映射到自定义形状的区域

@Override
public boolean onTouchEvent(MotionEvent event)
{
    int x = (int)event.getX();
    int y = (int)event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_UP:
    }

    return false;
}
这三种情况下,您可以对不同类型的事件做出反应,在本例中,再次轻敲、拖动或抬起手指


同样,你也可以使用不同的图像来处理相同的图像。

好吧,我考虑了一段时间,这有点像黑客,但是你可以通过修改图像来实现你想要的。使用任何图像编辑软件为每个区域添加稍微不同的颜色(这应该很容易,只需使用paint fill)。只需使用一种非常接近白色的颜色,以便观看图像的人看不到它。然后将图像放在imageview中,并添加一个触摸监听器

final Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
imageView.setOnTouchListener(new OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event){

        int x = (int)event.getX();
        int y = (int)event.getY();

        int pixel = bitmap.getPixel(x,y);

        int red = Color.red(pixel);
        int blue = Color.blue(pixel);
        int green = Color.green(pixel);     
        int alpha = Color.alpha(pixel);        

        //use these information to see which region you have selected
        //if the color is black, then you know you selected the boundary

        return false;
    }
});