java 如何在 ImageView Android 上添加标记/图钉?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13679189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:34:54  来源:igfitidea点击:

How to add a Marker/Pin on an ImageView Android?

javaandroidimageviewandroid-canvasmarker

提问by dicenice

I would like to ask on how to implement or add a marker on an imageView. I rendered an SVG using svglib and used a customImageView so that I can zoom and pan around the image.

我想问一下如何在imageView上实现或添加标记。我使用 svglib 渲染了一个 SVG 并使用了 customImageView 以便我可以缩放和平移图像。

here is my code on how i used my customImageView

这是我如何使用 customImageView 的代码

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        SVG svg;
        switch (mNum) {

        case 1:
            svg = SVGParser.getSVGFromResource(getResources(), R.raw.t1);
            break;
        case 2:
            svg = SVGParser.getSVGFromResource(getResources(), R.raw.t2);
            break;
        case 3:
            svg = SVGParser.getSVGFromResource(getResources(), R.raw.t3);
            break;
        case 4:
            svg = SVGParser.getSVGFromResource(getResources(), R.raw.t4);
            break;
        default:
            svg = SVGParser.getSVGFromResource(getResources(),
                    R.raw.android);

        }

        View v = inflater.inflate(R.layout.hello_world, container, false);
        View tv = v.findViewById(R.id.text);
        imageView = (GestureImageView) v.findViewById(R.id.imageView1);
        imageView.setStrict(false);
        imageView.setStartingScale(lastScale);
        // if(lastXPosition!=0 && lastYPosition!=0)
        imageView.setStartingPosition(lastXPosition, lastYPosition);
        // Log.i("tag",
        // "lastXPosition" + lastXPosition);
        // Log.i("tag",
        // "lastYPosition" + lastYPosition);
        // Log.i("tag",
        // "lastScale" + lastScale);
        // imageView.setRotation(45);
        // imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        if (Build.VERSION.SDK_INT > 15)
            imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        imageView.setImageDrawable(svg.createPictureDrawable());
        ((TextView) tv).setText("Floor number: " + mNum);
        imageView.setBackgroundColor(Color.WHITE);
        // tv.setBackgroundDrawable(getResources().getDrawable(
        // android.R.drawable.gallery_thumb));
        // imageView.setScaleType(ScaleType.CENTER);
        // ((GestureImageView)imageView).setScale(x);
        return v;
    }

Now I would like to add a pin just like the image below...

现在我想添加一个如下图所示的图钉...

My problem
(source: modality.com)

我的问题
(来源:modality.com

But my problem is that when I pan around the marker I added is not bonded with the image SVG thus left behind at a certain position when panning...

但我的问题是,当我在我添加的标记周围平移时,我添加的标记没有与图像 SVG 结合,因此在平移时留在某个位置......

Here's my code... NOTE: Not yet final... I'm still looking for a way to get this working and I am using a zoomed in imageview as a map...

这是我的代码......注意:尚未最终确定......我仍在寻找一种方法来让它工作,我正在使用放大的图像视图作为地图......

@Override
protected void onDraw(Canvas canvas) {

    if (layout) {
        if (drawable != null && !isRecycled()) {
            canvas.save();

            float adjustedScale = scale * scaleAdjust;

            canvas.translate(x, y);

            if (rotation != 0.0f) {
                canvas.rotate(rotation);
            }

            if (adjustedScale != 1.0f) {
                canvas.scale(adjustedScale, adjustedScale);
            }

            drawable.draw(canvas);

            canvas.restore();
        }

        if (drawLock.availablePermits() <= 0) {
            drawLock.release();
        }
    }

    // ---add the marker---
    Bitmap marker = BitmapFactory.decodeResource(getResources(),
            R.drawable.search_marker_icon);
    canvas.drawBitmap(marker, 40, 40, null);
    Paint mPaint = new Paint();
    mPaint.setColor(Color.RED);
    canvas.drawCircle(60, 60, 5, mPaint);
    super.onDraw(canvas);
} 

Thanks.... I'm new to android :) hope you can help me....

谢谢.... 我是 android 新手 :) 希望你能帮助我....

My problem

我的问题

采纳答案by dicenice

 drawable.draw(canvas);

// ---add the marker---
Bitmap marker = BitmapFactory.decodeResource(getResources(),
        R.drawable.search_marker_icon);
canvas.drawBitmap(marker, 40, 40, null);
Paint mPaint = new Paint();
mPaint.setColor(Color.RED);
canvas.drawCircle(60, 60, 5, mPaint);


        canvas.restore();
    }

    if (drawLock.availablePermits() <= 0) {
        drawLock.release();
    }
}
 super.onDraw(canvas);
}   

You need to do this before canvas.restore..... :D got this solution last year...... thnx for the help guys.... my app is almost done :)

您需要在 canvas.restore 之前执行此操作..... :D 去年得到了这个解决方案......感谢帮助家伙......我的应用程序快完成了:)

回答by Singed

This is a nice library for displaying images, which supports zooming/panning and adding pins over the image https://github.com/davemorrissey/subsampling-scale-image-view

这是一个很好的显示图像的库,它支持缩放/平移和在图像上添加图钉 https://github.com/davemorrissey/subsampling-scale-image-view

回答by Deepak Swami

An implementation of an HTML map like element in an Android View:

Android 视图中类似 HTML 地图的元素的实现:

  • Supports images as drawable or bitmap in layout
  • Allows for a list of area tags in xml
  • Enables use of cut and paste HTML area tags to a resource xml (ie, the ability to take an HTML map - and image and use it with minimal editing)
  • Supports panning if the image is larger than the device screen
  • Supports pinch-zoom
  • Supports callbacks when an area is tapped.
  • Supports showing annotations as bubble text and provide callback if the bubble is tapped
  • 在布局中支持图像作为可绘制或位图
  • 允许在 xml 中列出区域标签
  • 允许使用剪切和粘贴 HTML 区域标签到资源 xml(即,能够获取 HTML 地图 - 和图像并以最少的编辑使用它)
  • 如果图像大于设备屏幕,则支持平移
  • 支持双指缩放
  • 支持点击区域时的回调。
  • 支持将注释显示为气泡文本并在点击气泡时提供回调

try this link you will find your solution https://github.com/catchthecows/AndroidImageMap

试试这个链接你会找到你的解决方案https://github.com/catchthecows/AndroidImageMap