绘制矩形上SurfaceView [英] Draw Rectangle on SurfaceView

查看:391
本文介绍了绘制矩形上SurfaceView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何绘制在特定的位置和大小的矩形。

I would like to know how to draw a rectangle in specific position and size.

我试过一对夫妇在这里试验和实例和网络,但没有为我工作。

I tried a couple of trials and examples here and on web, but nothing worked for me.

我使用的是SurfaceView和SurfaceHolder保持相机preVIEW。

I'm using a SurfaceView and SurfaceHolder to hold the camera preview.

我只是想绘制一个矩形时,屏幕上的用户联系。

I just want to draw a rectangle when the user touch on the screen.

我不想使用外部库,当我试图从我得到空的持有人在画布上。

I don't want to use external library and When I'm trying to get the canvas from the holder I'm getting null.

有关现在还没有code,因为没有什么工作对我来说,如果有人有线索如何做到这一点,我会尝试。

For now there is no code, cause nothing work for me, if someone has a clue how to do it, I will try it.

这是我已经尝试过:

<一个href=\"http://stackoverflow.com/questions/18192205/android-how-to-draw-a-rectangle-on-a-movable-image-such-that-the-drawn-rectangl\">Link 1

<一个href=\"http://www.c-sharpcorner.com/UploadFile/1e5156/learn-about-how-to-draw-rectangle-using-canvas-in-android-us/\"相对=nofollow>链接2

链接3

感谢您的帮助!

推荐答案

好吧,感谢法登,我发现最简单的(至少对我来说)。解决方案

Ok, Thanks to fadden I found the easiest solution(at least for me).

首先,我创建了另一个表面观和我的XML文件是这样的:

First I created another Surface view and my XML file look like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:opencv="http://schemas.android.com/apk/res-auto"
    android:layout_gravity="left|top">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

                    <SurfaceView
                        android:layout_alignParentTop="true"
                        android:id="@+id/CameraView"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                    <SurfaceView
                        android:layout_alignParentTop="true"
                        android:id="@+id/TransparentView"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
           </RelativeLayout>
</RelativeLayout>

和比我创建了新的表面上的另一座,这是我的Java code的一部分:

And than I'm created another holder for the new surface, this is a part of my java code:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_capture);
        // Create first surface with his holder(holder)
        cameraView = (SurfaceView)findViewById(R.id.CameraView);
        cameraView.setOnTouchListener(onTouchListner);

        holder = cameraView.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        // Create second surface with another holder (holderTransparent)
        transparentView = (SurfaceView)findViewById(R.id.TransparentView);

        holderTransparent = transparentView.getHolder();
        holderTransparent.setFormat(PixelFormat.TRANSPARENT);
        holderTransparent.addCallback(callBack); 
        holderTransparent.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

最后,我实现了一个名为DrawFocusRect

At last I implement a method called 'DrawFocusRect'

private void DrawFocusRect(float RectLeft, float RectTop, float RectRight, float RectBottom, int color)
{

    canvas = holderTransparent.lockCanvas();
    canvas.drawColor(0,Mode.CLEAR);
    //border's properties
    paint = new Paint();
    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(color);
    paint.setStrokeWidth(3);
    canvas.drawRect(RectLeft, RectTop, RectRight, RectBottom, paint);


    holderTransparent.unlockCanvasAndPost(canvas);
}

正如你所看到的,我每次清除画布,如果我不这样做,表面将在每个人除了显示更多的矩形。

As you can see, I clear the canvas every time, if I don't do this, the surface will display in addition more rectangles on each others.

这是我如何调用该方法从OnTouchListener事件:

This is how I call to this method from a 'OnTouchListener' event:

OnTouchListener onTouchListner = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
                    RectLeft = event.getX() - 100;
                    RectTop = event.getY() - 100 ;
                    RectRight = event.getX() + 100;
                    RectBottom = event.getY() + 100;
                    DrawFocusRect(RectLeft , RectTop , RectRight , RectBottom , Color.BLUE);
             }
         };

希望这将是有益的人了。

Hope this would be helpfull for someone else.

这篇关于绘制矩形上SurfaceView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆