简体   繁体   English

如何在Android Studio中将图像添加到位图?

[英]How do I go about adding images to a bitmap in Android Studio?

I want to be able to add a "Point of interest" onto a bitmap, something like a google map pin which can be clicked on to bring up information. 我希望能够在位图上添加“兴趣点”,例如可以单击以显示信息的google map pin。

I just need to know how to go about adding the pin into the space and keep it on the right point of the bitmap (I can pan and zoom on the bitmap) 我只需要知道如何将图钉添加到空间中并将其保持在位图的正确点上(我可以在位图上平移和缩放)

Sorry, I'm fairly new to Android Studio and thanks in advance! 抱歉,我是Android Studio的新手,在此先感谢您! :) :)

You could draw an object on top of your bitmap and implement a touch callback that verifies if you have indeed pressed on your marker. 您可以在位图上绘制一个对象,并实现触摸回调,以验证是否确实按下了标记。 Here's a simple example: 这是一个简单的例子:

<com.myproject.RedImageView
    android:id="@+id/imageView"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_centerHorizontal="true"
    android:layout_centerInParent="true"
    android:layout_centerVertical="true"
    android:background="@android:color/holo_red_dark" />

package com.myproject;

public class RedImageView extends View {

    private Paint mPaint;
    private Rect mRect;

    public RedImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    //Draw a small white rectangle on top of our red ImageView
    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);

        mRect = new Rect(20, 20, 120, 120);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawRect(mRect, mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //Simple touch gesture has started
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            int x = (int) event.getX();
            int y = (int) event.getY();
            //Match touch coordinates vs "Point of Interest" aka small white rectangle
            if (mRect.contains(x, y)) {
                Snackbar.make(this, "Clicked white", Snackbar.LENGTH_SHORT).show();
            }
        }

        return super.onTouchEvent(event);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM