How to make SurfaceView with transparent background?

前端 未结 2 1811
终归单人心
终归单人心 2020-12-30 10:19

I have simple layout




        
2条回答
  •  春和景丽
    2020-12-30 10:36

    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.

提交回复
热议问题