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

11

我想请问如何在ImageView上实现或添加标记。我使用svglib渲染了SVG并使用了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;
    }

现在我想添加一个像下面图片中一样的图钉...

My problem
(来源:modality.com)

但我的问题是,当我围绕标记点进行移动时,添加的标记点没有与图像SVG绑定,因此当平移时它会保持在某个位置落后于图像...

这是我的代码... 注意:还不是最终版本... 我仍在寻找方法使其正常工作,并且我正在使用缩放的ImageView作为地图...

@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);
} 

谢谢……我是新手 :) 希望你能帮助我。

我的问题


1
还没…… 我目前正在处理程序的某些部分…… 找到解决方案后,我会发布答案。 - dicenice
@dicenice,你有解决方案吗?我有同样的问题(我想要实现类似的东西)。 - Deepak Swami
@dicenice,你是如何将infoWindow添加到这个标记上的? - osrl
你能否发送一个类似的示例项目,因为我也想将图钉图片添加到楼层平面图中..请帮帮我。 - reegan29
@dicenice 我需要你的帮助,兄弟。我需要完全相同的功能,但是当我缩放我的图像时,标记位置会改变。你能帮我吗? - Pratik Dasa
3个回答

6

如何向SubsamplingScaleImageView对象添加标记? - Vulovic Vukasin
看看他提供的PinView示例。这是我发现的最好的库之一,感谢@Singed指出! - ovidiu

4
 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);
}   

在canvas.restore之前需要这样做……去年我得到了这个解决方案……感谢大家的帮助……我的应用程序快要完成了 :)

1
如果您能发布一些与此相关的代码,特别是GestureImageView类,那将是非常有帮助的。我已经尝试了很长时间来解决这个问题。所以请帮忙。 - roy mathew
兄弟,你能帮忙回答这个问题吗?https://dev59.com/74jca4cB1Zd3GeqP0K7O - reegan29

2
在Android视图中实现类似HTML地图元素的一个实现:
  • 支持在布局中作为可绘制或位图的图像
  • 允许在xml中使用区域标签列表
  • 使得可以将剪切和粘贴的HTML区域标签用于资源xml(即,能够以最小编辑量使用HTML地图和图像)
  • 如果图像大于设备屏幕,则支持平移
  • 支持捏合缩放
  • 支持在区域被点击时回调
  • 支持显示注释文本作为气泡,并提供回调函数,如果气泡被点击则执行回调
请尝试此链接,您将找到解决方案:https://github.com/catchthecows/AndroidImageMap

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接