how to make surfaceview transparent
Try this in the constructor:
SurfaceView sfvTrack = (SurfaceView)findViewById(R.id.sfvTrack);
sfvTrack.setZOrderOnTop(true); // necessary
SurfaceHolder sfhTrackHolder = sfvTrack.getHolder();
sfhTrackHolder.setFormat(PixelFormat.TRANSPARENT);
How to make SurfaceView with transparent background?
Yeah, i did it i solve problem
Activity code
package com.fmech.zenclock.surface;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class ZenClockSurfaceActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ZenClockSurface sfvTrack = (ZenClockSurface)findViewById(R.id.zenClockSurface1);
sfvTrack.setZOrderOnTop(true); // necessary
SurfaceHolder sfhTrack = sfvTrack.getHolder();
sfhTrack.setFormat(PixelFormat.TRANSLUCENT);
}
}
Surface Code
package com.fmech.zenclock.surface;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PixelFormat;
import android.graphics.Region;
import android.util.AttributeSet;
import android.view.Surface.OutOfResourcesException;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class ZenClockSurface extends SurfaceView implements
SurfaceHolder.Callback {
private DrawClock drawClock;
public ZenClockSurface(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
getHolder().addCallback(this);
}
public ZenClockSurface(Context context) {
super(context);
getHolder().addCallback(this);
}
public ZenClockSurface(Context context, AttributeSet attrs) {
super(context, attrs);
getHolder().addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
drawClock = new DrawClock(getHolder(), getResources());
drawClock.setRunning(true);
drawClock.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
drawClock.setRunning(false);
while (retry) {
try {
drawClock.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
class DrawClock extends Thread {
private boolean runFlag = false;
private SurfaceHolder surfaceHolder;
private Bitmap picture;
private Matrix matrix;
private Paint painter;
public DrawClock(SurfaceHolder surfaceHolder, Resources resources) {
this.surfaceHolder = surfaceHolder;
picture = BitmapFactory.decodeResource(resources,
R.drawable.ic_launcher);
matrix = new Matrix();
this.painter = new Paint();
this.painter.setStyle(Paint.Style.FILL);
this.painter.setAntiAlias(true);
this.painter.setFilterBitmap(true);
}
public void setRunning(boolean run) {
runFlag = run;
}
@Override
public void run() {
Canvas canvas;
while (runFlag) {
matrix.preRotate(1.0f, picture.getWidth() / 2,
picture.getHeight() / 2);
canvas = null;
try {
canvas = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder) {
canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
canvas.drawBitmap(picture, matrix, this.painter);
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
}
It's work.
How to make SurfaceView transparent and behind the ViewHierarchy
I think my start direction was wrong,because android.view.View hardwareAccelerator is open by default,and also I move my code from SurfaceView to android.view.View to test the performance what I found is that the performance is enough!
and in conclusion,I didn't find the method to solve this problem but change a way to solve this.
Android transparent canvas (surfaceview)
After searching with keyword surfaceview instead of canvas iI found out that it isn't possible. For more information see: how to make surfaceview transparent
Because the background of the canvas is static I've gave it the exact same background. Now it looks like it is transparent :)
Bitmap bg = BitmapFactory.decodeResource(getResources(), R.drawable.background_panel_800_480);
canvas.drawBitmap(bg, 0, 0, null);
Is it possible to make a SurfaceView transparent without setZOrderOnTop(true)?
Looks like it's impossible, but fortunately, I found the solution.
I just replaced surface view in libgdx android backend sources with texture view.
Also I added one line in my code, for make my texture view transparent:
view.setOpaque(false);
Advantages of my solution:
- Increased FPS (130 instead of 55)
- Flexibility
Now, I can use my libGDX fragment view as any ordinary views. I can place it between android views and so on.
Make a SurfaceView transparent without setZOrderOnTop(true)
Set the holder pixel format to RGBA_8888,
sv.setZOrderOnTop(true); //very much necessary
getHolder().setFormat(PixelFormat.RGBA_8888);
Related Topics
How to Send String from One Activity to Another
Android Chrome Ignoring -Webkit-Text-Size-Adjust:None Property. Text Is Being Scaled When Zoomed Out
...Have You Declared This Activity in Your Androidmanifest.Xml
How to Disable Gradle 'Offline Mode' in Android Studio
How to Hang Up Outgoing Call in Android
Android Intent for Twitter Application
How to Make an Alert Dialog Fill 90% of Screen Size
How to Center Align the Actionbar Title in Android
How to Get Uri from an Asset File
How to Get Current Time from Internet in Android
Updating Google Play Services in Emulator
Lollipop:Draw Behind Statusbar with Its Color Set to Transparent
How to Set My App as the Default Sms App