Draw a rectangular over an image by code

北慕城南 提交于 2019-12-04 20:42:40

My guess is, the reason why it's in the bottom of the page is because of the LinearLayout: in the end you will have 3 children: TextView, ImageView and ImageView, all below each other.

A few possible solutions:

  1. Add a FrameLayout inside the LinearLayout and add the ImageViews inside it, so the hierarchy would be:

    • LinearLayout
      • TextView
      • FrameLayout
        • ImageView
        • ImageView
  2. Use a RelativeLayout instead of the LinearLayout and align all the edges of the second ImageView with the first one

  3. Create a custom ImageView class, for example "com.foo.bar.MyImageView", which of course extends ImageView. Override onDraw:

    public void onDraw(Canvas canvas) {
        super.onDraw(canvas); // This will render the original image
        // ... and here you can have the code to render the rectangle
    }
    

In the xml you replace

 <ImageView ...

with

 <com.foo.bar.MyImageView ...

I would go for the last solution, since from a performance point of view it's the best.

try this code:

        Point left=new Point(x, y);

        paint.setColor(Color.parseColor("#00CCFF"));
        paint.setStyle(Style.FILL);
        canvas.drawCircle(left.x, left.y, 9, paint);

So you have to create a Point using pixels and then draw a rectangle around it otherwise it doesn't know where to draw

Why dont you do this in the layout file itself:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/photo0"
android:id="@+id/imagBackground">

<LinearLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:background="@android:color/darker_gray">
 <LinearLayout android:layout_width="fill_parent"
 android:layout_margin="5dip"
        android:layout_height="fill_parent"              android:background="@android:color/black">
<TextView android:layout_width="20dip" android:layout_height="20dip"
    android:layout_marginLeft="250dip" android:layout_marginTop="15dip" android:background="#FFFFFF"
    android:id="@+id/index" android:text="0" android:textColor="#000000">
    </TextView>

    <ImageView android:layout_marginTop="280dip"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:background="@drawable/anim_ctrl_panel" android:id="@+id/change">
    </ImageView>
</LinearLayout>
            </LinearLayout>
</LinearLayout>

Try to change your LinearLayout to this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:gravity="top" android:layout_gravity="top"
    android:layout_height="fill_parent" android:background="@drawable/photo0"
    android:id="@+id/imagBackground">
...
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!