0

I am trying to make part of my image clickable. In this case I have 24 port switch and I want when user click on a port to display the port number. I already have do the zooming and tried to insert rectangles over the image, but I am still new in the Android Development so I am not so sure how to accomplish the task.

Here is my code to create rectangles and put on the picture: (the idea is that I have one class Rectangles that keeps the port number and some text so I can retrieve them)

public class MainActivity extends Activity{

ImageView DrawingImage;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    DrawingImage = (ImageView) this.findViewById(R.id.image);

    Bitmap bitmap2 = Bitmap.createBitmap((int) getWindowManager()
        .getDefaultDisplay().getWidth(), (int) getWindowManager()
        .getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap2);
    DrawingImage.setImageBitmap(bitmap2);

    // Draw Rectangle

    Paint paint = new Paint();
    paint.setColor(Color.GREEN);
    paint.setStyle(Paint.Style.FILL);

    float left = 20;
    float top = 20;
    float right = 50;
    float bottom = 100;

    canvas.drawRect(left, top, right, bottom, paint);

    Zoom image = (Zoom) findViewById(R.id.image);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
    image.setImageBitmap(bitmap);

    int posX=(int)image.getX();
    int posY=(int)image.getY();
    double height=image.getHeight();
    double width=image.getWidth();

  }
}

But when I run the App I cannot see the Rectangle. Even if i declare the picture before the rectangle I can see only the rectangle.

Any suggestions?

Any help will be appreciated.

Thank you in advance!

Best regards, Dimtiar Georgiev

4

2 回答 2

1

xml layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

onCreate Method:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView DrawingImage = (ImageView) this.findViewById(R.id.imageView2);

    Bitmap bitmap2 = Bitmap.createBitmap((int) getWindowManager()
        .getDefaultDisplay().getWidth(), (int) getWindowManager()
        .getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap2);
    DrawingImage.setImageBitmap(bitmap2);

    // Draw Rectangle

    Paint paint = new Paint();
    paint.setColor(Color.GREEN);
    paint.setStyle(Paint.Style.FILL);

    float left = 20;
    float top = 20;
    float right = 50;
    float bottom = 100;

    canvas.drawRect(left, top, right, bottom, paint);

    ImageView image = (ImageView) findViewById(R.id.imageView1);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    image.setImageBitmap(bitmap);

    int posX=(int)image.getX();
    int posY=(int)image.getY();
    double height=image.getHeight();
    double width=image.getWidth();
}

Its not about what you draw first now. It's about the xml layout. what to put first. ofcourse you may want to play with the height and width attributes for the image view to achieve what your really want. but this is working I test it.

于 2013-09-19T06:44:47.927 回答
0

This will not work. the way you did it.

you must have two image views in your design.

use a framelayout and put two image view inside it.

set the canvas for one of them and draw.

and set the image for the other.

it will not work because you set the first bitmap:

DrawingImage.setImageBitmap(bitmap2);

then you replace it:

image.setImageBitmap(bitmap);

you got the same image layout:

R.id.image
于 2013-09-18T12:55:00.680 回答