Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 通过代码在图像上绘制矩形_Android_Draw - Fatal编程技术网

Android 通过代码在图像上绘制矩形

Android 通过代码在图像上绘制矩形,android,draw,Android,Draw,我有以下xml: <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="@

我有以下xml:

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

    <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>
但是矩形在屏幕的底部。如何设置放置它们的位置?

请尝试以下代码:

        Point left=new Point(x, y);

        paint.setColor(Color.parseColor("#00CCFF"));
        paint.setStyle(Style.FILL);
        canvas.drawCircle(left.x, left.y, 9, paint);
因此,您必须使用像素创建一个点,然后在其周围绘制一个矩形,否则它不知道在何处绘制,请尝试以下代码:

        Point left=new Point(x, y);

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

因此,您必须使用像素创建一个点,然后在其周围绘制一个矩形,否则它不知道在何处绘制

为什么不在布局文件本身中执行此操作:

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

为什么不在布局文件本身中执行此操作:

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

尝试将您的
线性布局更改为:

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

...

尝试将您的
线性布局更改为:

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

...

我的猜测是,它位于页面底部的原因是因为线性布局:最终您将有3个孩子:TextView、ImageView和ImageView,它们都位于彼此下方

一些可能的解决方案:

  • 在LinearLayout内添加一个FrameLayout,并在其中添加ImageView,这样层次结构将是:

    • 线性布局
      • 文本视图
      • 框架布局
        • 图像视图
        • 图像视图
  • 使用RelativeLayout而不是LinearLayout,并将第二个图像视图的所有边与第一个对齐

  • 创建一个自定义ImageView类,例如“com.foo.bar.MyImageView”,它当然扩展了ImageView。覆盖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
    }
    
  • 在替换的xml中

     <ImageView ...
    

    我的猜测是,它位于页面底部的原因是因为线性布局:最终您将有3个子项:TextView、ImageView和ImageView,它们都位于彼此下方

    一些可能的解决方案:

  • 在LinearLayout内添加一个FrameLayout,并在其中添加ImageView,这样层次结构将是:

    • 线性布局
      • 文本视图
      • 框架布局
        • 图像视图
        • 图像视图
  • 使用RelativeLayout而不是LinearLayout,并将第二个图像视图的所有边与第一个对齐

  • 创建一个自定义ImageView类,例如“com.foo.bar.MyImageView”,它当然扩展了ImageView。覆盖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
    }
    
  • 在替换的xml中

     <ImageView ...
    


    你能解释一下吗?你的意思是在主要线性布局上使用线性布局吗?没有。它只会在背景为“draker gray”或任何其他颜色的LinearLayout上创建一个背景为“黑色”的LinearLayout。但黑色线条布局将具有任意大小的边距。在上面的代码中,布局的边距是“5dip”。这将在主线性布局周围创建一个矩形区域。我认为这不是我的解决方案……我必须这样做:你能解释一下吗?你的意思是在主要线性布局上使用线性布局吗?没有。它只会在背景为“draker gray”或任何其他颜色的LinearLayout上创建一个背景为“黑色”的LinearLayout。但黑色线条布局将具有任意大小的边距。就像上面的代码一样,布局的边距是“5dip”。这将在主线性布局周围创建一个矩形区域。我认为这不是我的解决方案……我必须这样做:使用
    drawRect
    API:),将左设置为25,顶部50,右75,底部150。更改此设置以更改矩形的位置。我设置了此尺寸(25,50,75150),但在屏幕上不是这样的…这就是问题所在。我需要在设置为背景的图像上设置矩形。您是否尝试先将imageview添加到布局,然后再绘制矩形?使用
    drawRect
    API:),将左侧设置为25,顶部设置为50,右侧设置为75,底部设置为150。更改此设置以更改矩形的位置。我设置了此尺寸(25,50,75150),但在屏幕上不是这样的…这就是问题所在。我需要在设置为背景的图像上设置矩形。您是否尝试先将imageview添加到布局中,然后绘制矩形?如果我使用第一种或第二种解决方案,我可以在xml上的imageView上设置由代码创建的imageView吗?对于最后一个解决方案,您知道一个例子可以帮助我吗?我提到我必须放置许多图像,它必须是这样的:是的,您可以通过代码创建ImageView,而不是通过xml创建ImageView。它们是在哪里创建的没有区别。重要的是布局:它们必须布置在同一位置。您可以有任意多个矩形,但如果使用解决方案1或2(即创建一个额外的ImageView),则只为所有矩形创建一个ImageView,而不是为每个矩形创建一个。我没有任何现成的解决方案3示例,但它很简单,只需尝试一下:a)创建一个新类,它是ImageView的子类,用该类替换xml中的ImageView(不要忘记添加包名)。此时,请确认图像仍然可见。如果是,继续:在调用super.onDraw(canvas)之后,覆盖onDraw并在那里绘制一些东西。对于解决方案3,我创建的所有imageview都将位于同一位置?我不想这样,正如您在我发布的链接中看到的那样。如果我使用第一个或第二个解决方案,我可以在xml上的imageView上设置由代码创建的imageView吗?对于最后一个解决方案,您知道一个例子可以帮助我吗?我提到我必须放置许多图像,它必须是这样的:是的,您可以通过代码创建ImageView,而不是通过xml创建ImageView。它们是在哪里创建的没有区别。重要的是布局:它们必须布置在同一位置。您可以有任意多个矩形,但如果使用解决方案1或2(即创建一个额外的ImageView),则只为所有矩形创建一个ImageView,而不是为每个矩形创建一个。我没有任何现成的soluti示例