简体   繁体   中英

Android overlapping views

I'm new to Android and let me first tell you what I'm trying to achieve. Using sockets, I'm sending pictures from my computer's webcam every 50ms. In Android app, I've created my display that extends View. I've added this view to FrameLayout. Parallel thread is receiving images from server app (desktop) and refreshing my display.

On this image I want to display some accelerometer data that refreshes every... Well it's set to SENSOR_DELAY_FASTEST. So I also created another display that extends View, and also I add it to another FrameLayout. Now I set my main.xml to overlap those FrameLayouts..

I'm getting my image from desktop application, I'm drawing accelerometer data, and It's overlapped, but the issue is.. It's flickering. Can anyone help? Or suggest something.. As I've pointed out, I'm new with Android.

Thanks..

This is a simple override that draws an image. - And and the method that calls for a redraw.

@Override
protected void onDraw(Canvas canvas){
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    paint.setARGB(10, 255, 255, 255);
    if(Core.pic != null) {
        canvas.drawBitmap(Core.pic, 0, 0, paint);
    }       
}

Here is a different class that calls for redraw when new image is available:

    protected static volatile Bitmap pic;
    public static void refreshDisplay(Bitmap img){
    pic = img;
    if(cameraDisplay != null) {
        try{
            cameraDisplay.invalidate();
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}

And here is a threaded class that ready port every 50ms:

        while(running){

        opt.inDither = true;
        opt.inPreferredConfig = Bitmap.Config.ARGB_8888;

        if(in != null){
            byte[] recieve = new byte[7000];
            try {
                in.read(recieve, 0, 7000);
                Core.pic = BitmapFactory.decodeByteArray(recieve, 0, 7000, opt);    
            } catch (IOException e) {}
        }
        try {
            sleep(50);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

This alone works fine. When I overlap this views then it flickers. In the similar way I'm reading accelerometer data and draw it:

    public void onAccelerationChanged(float x, float y, float z) {
    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
        velocityBars.DrawVelocity(x, -z);
    }
    else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
        velocityBars.DrawVelocity(y, -z);
    }
}

velocityBars is my variable that is type of my custom View. This method DrawVelocity invokes the invalidate() method. This forces redraw.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM