how to mask a portion of GLSurfaceView in android - android

I have a custom GLSurfaceView that I want to mask a portion of.
Lets say I only want to draw a circular portion of it on screen.
I tried to override the dispathDraw method and use the clipPath method as suggested here
I ensured that the Hardware Acceleration is turned off but the GLsurfaceView renders without any change.
EDIT 1
I realized now that in android studio, after i hit run, the preview does update to show a clipped GLSurfaceView. The output on the screen remains rectangular.
EDIT 2
I figured that this only happens when the child view of the parent FrameLayout is GlSurfaceView. WHen I replace that with a imageview (containing blue background) it works as expected on the device.
Any suggestions?

Related

Talkback with ExploreByTouchHelper doesn't show green square on SurfaceView inside dynamic loaded Fragment

I have a dynamic loaded fragment inside the activity. The fragment contains one SurfaceView in the bottom and it contains some text image. I used ExploreByTouchHelper for Talkback. It can read the context but can't draw the green square on SurfaceView. But if the activity contains some components(like buttons) below this SurfaceView then the green square can be drawn.
So it seems that the Application doesn't know that the space on screen for SurfaceView belongs to this Application. If some components added below, only then it knows that itself owns that space.
So anything I can do to get this green square back?
Many thanks.
It seems that SurfaceView by default will not enable drawing on it's surface so the talkback can't draw on it's surface. To enable this you have to call setWillNotDraw(false).

Make surface view transparent to current layout only

In my app i am using surface view for drawing perspective and some buttons on the same layout.
I am done with the drawing part but don't know how to make surface view transparent for the current layout only.
When i use holder.setFormat(PixelFormat.TRANSLUCENT); it shows my previous screen.
I have also tried the setZOrderOnTop(true); method but it makes my other buttons hidden/covered when drawing.
And using setZOrderMediaOverlay(true); put the view beyond current layout, if i am moving my finger over the screen it is drawing there as i can see the saved file, but not showing on screen.
I want to make it transparent for current screen without setting the z order.
Forks, help me to make this happen.
Thanks in advance.

Transparent SurfaceView over a transparent Activity

I have an Activity that is roughly 30-40% transparent which contains a custom View that extends SurfaceView. Everything is working as it should however the SurfaceView's background is not completely transparent (at least for the first few seconds). After 1-2 seconds (using debugger) I noticed the SurfaceView becomes completely transparent after onDraw is called from my Thread for the Canvas.
Now I'm assuming that the issue is; the canvas is selecting my activity's transparent background, creating a bitmap of it and then displaying it hence creating a double transparent overlay.
I've tried to initialize the SurfaceView first while the View is 'invisible' and enabled it after the first onDraw. No dice.
Any Suggestions or Questions?
My SurfaceView was Embedded within a RelativeLayout View Container. By applying the alpha parameter for this view to 255 resulted in what I was looking for.
I hope someone comes across the same situation, and that this has solved their problem.

Setting background colour for a SurfaceView in Android

I am building a paint application for android. Extending the FingerPaint application sample provided with the SDK. However, unlike FingerPaint, I'm using SurfaceView with a separate rendering thread to draw to the surface. All this is very standard and straightforward. Working well. I want to, however, give a White coloured background to the painting surface, so it's like drawing on a white paper. The default background is black coloured.
The thread of my surface view, calls the onDraw() method of the view.
The problem I'm facing here is, if I set a background colour or background resource to my SurfaceView, this background overwrites the previous drawing of the surface, when the view is rendered the next time. I will explain this with an example:
Suppose I set the background to white colour. Now, the application starts, with my SurfaceView having the white colour. Ok, so far good. Now, I draw a red coloured line on this surface, with my finger. The line is now shown on the white surface. Good. Now, this is supposed to be a painting app, and suppose I want to draw a car. So, then, I draw my 2nd red line, with my finger. The 2nd line is drawn on the screen, but the 1st one, which I drew previously, vanishes. That is, because I have set some background to my SurfaceView, the background is drawn again, thus overwriting the 1st line that was drawn. Now the screen shows only the 2nd line drawn.
Obviously, I do not want this to happen. The code works perfectly, when I don't attempt to change the background (that is, both lines are shown, on a default black background, no background effectively). But when I set some desired background, this thing happens. Is there a way to somehow have a static background, that is not drawn every time? I want the background to be drawn only once, all the subsequent drawings should happen on this background. I don't want the android runtime , to draw the background, every time it draws my view, thus overwriting all drawing present on that view, from previous renderings. Any way to work around this?
The things I have tried, to achieve this are:
Setting the background colour of my SurfaceView using android:background in XML.
Doing the above, using the concept of style. (specifying a style value and referencing it in the layout file). The style just defines the background colour as #FFFFFFFF (white).
Setting the style of 2, to the parent view of the SurfaceView (a RelativeLayout).
Setting the style of 2, to the entire application, as a theme,using android:theme in my manifest file.
Setting the background drawable of my SurfaceView to an image which is plain and white-coloured.
Calling the setBackgroundColor for my SurfaceView from the code as, this.setBackgroundColor(Color.WHITE).
Thanks.
according to android's doc:
The surface is Z ordered so that it is behind the window holding its
SurfaceView; the SurfaceView punches a hole in its window to allow its
surface to be displayed. The view hierarchy will take care of
correctly compositing with the Surface any siblings of the SurfaceView
that would normally appear on top of it. This can be used to place
overlays such as buttons on top of the Surface, though note however
that it can have an impact on performance since a full alpha-blended
composite will be performed each time the Surface changes
so if you change the z order of surface view via:
setZOrderOnTop(true);
this will work, but then you cannot put any other view on top of surface view.
Now I also tried overlaying my SurfaceView over another View with a white background, and setting the SurfaceView to be fully transparent. I used FrameLayout for this. That is, a View with a white background, overlayed upon which is my SurfaceView set to fully transparent.
This also failed to work. So now I have come to conclusion that the background colour of SurfaceView cannot be changed. So now I have dropped SurfaceView, and using a normal view, instead for my drawings. So far, there has not been any performance loss observed. But, it feels kind of wrong to drop an efficient implementation , just because SurfaceView doesn't let me change it's background colour.

Rotating an Android View

I want to rotate an Android View with API level 8. For example, I want to rotate an EditText by 90 degrees so that when the user enters text into a left justified EditText, the first character is at the bottom (rotated 90 degrees) and subsequent characters are entered upwards.
I first tried using an animation with duration of 0, but you still see the field rotate. Unfortunately, this is a non-starter. If I could find a way to hide the animation completely, this method looks to be the simplest.
I then tried rotating the canvas in onDraw which works great for square Views but not so great for ones that aren't square (and I don't control the dimensions of the EditText). I tried various attempts at clipping and translating the canvas, but while I could get the cursor to come into view at the start of text input, it would do weird things once somebody started entering more content (usually the content would disappear out of view).
I also tried making the View square in onMeasure, then rotating the canvas in onDraw, then putting the View dimensions back in a subsequent onMeasure. The first two steps worked great. But the third step produced similar results as described above: things looked ok until the user started entering more text at which point the field text did strange things (usually disappearing).
Has anybody been able to successfully rotate a non-square Android View (an EditText for example) without animation, and with API level 8 or lower?
I've spent a pretty good amount of time trying to rotate a view exactly on API level 8 and I think it's impossible to make it work properly. FYI, it doesn't work correctly on android 3.2 either although there's a setRotation(int angle) method in the View class. This won't work correctly at all for VideoView for example.
P.S back then it seemed that I've managed to make the rotation thing work for anything other than VideoView though. Have you overriden the onTouchEvent method? If you have not your view won't be receiving the touch event since rotating the canvas will just make the view draw rotated but it will still receive the touch events in its old area. You have to manually apply rotation matrix to the touch event coordinates in order to offset them to the proper location.

Categories

Resources