简体   繁体   中英

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.

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! :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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