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

发布于 2024-12-10 16:14:44 字数 1917 浏览 0 评论 0原文

我有这个 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>

在此之上我必须通过代码绘制一些矩形。如何做到这一点?

我尝试了这个:

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

        layout = (LinearLayout) findViewById(R.id.imagBackground);
        changeImage = (ImageView) findViewById(R.id.change);
        index = (TextView) findViewById(R.id.index);

        draw();
    }

        private void draw() {
        System.out.println("desenam");
        int width = 50;
        int height = 100;

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

        Paint paint = new Paint();
        paint.setColor(Color.BLUE);


        Canvas canvas = new Canvas(bitmap);

        canvas.drawColor(Color.RED);

        canvas.drawRect(25, 50, 75, 150, paint);
         ImageView imageView = new ImageView(this);


        imageView.setImageBitmap(bitmap);

         layout.addView(imageView);

        setContentView(layout);
    }

但矩形位于屏幕底部。如何设置放置它们的位置?

I have this 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>

and over this I must draw some rectangular by code. How to do this ?

I tried this :

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

        layout = (LinearLayout) findViewById(R.id.imagBackground);
        changeImage = (ImageView) findViewById(R.id.change);
        index = (TextView) findViewById(R.id.index);

        draw();
    }

        private void draw() {
        System.out.println("desenam");
        int width = 50;
        int height = 100;

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

        Paint paint = new Paint();
        paint.setColor(Color.BLUE);


        Canvas canvas = new Canvas(bitmap);

        canvas.drawColor(Color.RED);

        canvas.drawRect(25, 50, 75, 150, paint);
         ImageView imageView = new ImageView(this);


        imageView.setImageBitmap(bitmap);

         layout.addView(imageView);

        setContentView(layout);
    }

but the rectangulars are on the bottom on screen. How to set the location where to put them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绅士风度i 2024-12-17 16:14:44

我的猜测是,它之所以位于页面底部是因为 LinearLayout:最后你将有 3 个子视图:TextView、ImageView 和 ImageView,它们都在彼此的下方。

一些可能的解决方案:

  1. 在 LinearLayout 中添加一个 FrameLayout 并在其中添加 ImageView,因此层次结构将是:

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

  3. 创建一个自定义的ImageView类,例如“com.foo.bar.MyImageView”,它当然扩展了ImageView。重写 onDraw:

    public void onDraw(Canvas canvas) {
        super.onDraw(画布); // 这将渲染原始图像
        // ...在这里您可以使用渲染矩形的代码
    }
    

xml 中,

 <ImageView ...

在您替换的

 <com.foo.bar.MyImageView ...

我会选择最后一个解决方案,因为从性能的角度来看,它是最好的。

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.

感性不性感 2024-12-17 16:14:44

尝试这个代码:

        Point left=new Point(x, y);

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

所以你必须使用像素创建一个点,然后在它周围绘制一个矩形,否则它不知道在哪里绘制

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

沩ん囻菔务 2024-12-17 16:14:44

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

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

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>
变身佩奇 2024-12-17 16:14:44

尝试将您的 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">
...

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">
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文