android divide image into sub images ,recognizing

六眼飞鱼酱① 提交于 2019-12-12 02:19:32

问题


I want to divide the image into sub images and when I click on a part of the image will give me the name of the region, for example, this is my question how to recognize a region from the image, or how to divide the image into sub-images and use it in imageViews

And thank you in advance


回答1:


In my opinion @fractalwrench's idea is quite good for your case. Basic steps are listed below.

  • Subclass Android ImageView. For example, MultiRegionImageView.
  • Override its onTouchEvent method. (This method gets called whenever user touches the view)
  • User touches the image and thereby onTouchEvent is called and provides the exact touch point (x, y).

  • Declare another method or interface which determines at which region a given point is. For example, getRegionByPoint(int x, int y)

  • If you would like to highlight that region boundaries, you could use paths. First off, you should define paths and save them into a raw file (XML, for example), then using region ID, fetch its path and finally draw that path over the main image.

  • For drawing a path over the main image, you should also override onDraw method of ImageView class and use canvas.drawPath();


public class MultiRegionImageView extends ImageView {
    RegionProvider mRegionProvider;
    int mId = -1;
    private Paint mPaint;

    public MultiRegionImageView(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mId = mRegionProvider.getRegionIdByPoint(event.getX(), event.getY());
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(mId != -1) {
            canvas.drawPath(mRegionProvider.getRegionBoundaryPath(mId), mPaint);
        }
    }

    public interface RegionProvider{
        int getRegionIdByPoint(float x, float y);
        Path getRegionBoundaryPath(int id);
    }
}



回答2:


You should only need one ImageView to display the map. If you override onTouchEvent(MotionEvent e), you can get the position which is being touched in the View. If you store the position and shape of each region in some sort of List, you can check whether a touch event is within a region (and display whatever text you need to).



来源:https://stackoverflow.com/questions/33443515/android-divide-image-into-sub-images-recognizing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!