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

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

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.

这是我如何使用 customImageView 的代码

here is my code on how i used my 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...


(来源:modality.com)

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

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...

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

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

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

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

推荐答案

 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 之前执行此操作..... :D 去年得到了这个解决方案......谢谢大家的帮助......我的应用程序快完成了 :)

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

相关文章