Android - 在WallpaperService中实现安全性

时间:2017-06-26 12:52:44

标签: android surfaceview live-wallpaper wallpaper surfaceholder

我正在使用WallpaperService类在设备上设置动态壁纸。

我想在系统屏幕上实现安全性(以防止屏幕截图或录制)“设置壁纸”的位置。按钮由android系统显示。

到目前为止,我找到了一个SurfaceView类方法 - surfaceview.setSecure(boolean value)

但是,我无法在我的课程中获得SurfaceView的实例。 请建议一些解决方法来获取此类的实例。

我的代码 -

public class LiveWallpaperService extends WallpaperService {
    private int mDeviceWidth, mDeviceHeight;
    private int mAnimationWidth, mAnimationHeight;

    @Override
    public Engine onCreateEngine() {
        Movie movie = null;
        // Some Code here
        return new GIFWallpaperEngine(movie);
    }


    private class GIFWallpaperEngine extends Engine {
        private final int frameDuration = 24;
        private SurfaceHolder holder;
        private final Movie movie;
        private boolean visible;
        private final Handler handler;
        private final Runnable drawGIF = new Runnable() {
            public void run() {
                draw();
            }
        };

        public SurfaceView getSurfaceView(){
            // How to find the SurfaceView object here?
        }

        GIFWallpaperEngine(Movie movie) {
            this.movie = movie;
            handler = new Handler();
        }

        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);
            this.holder = surfaceHolder;
        }

        @Override
        public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            super.onSurfaceChanged(holder, format, width, height);
            mDeviceWidth = width;
            mDeviceHeight = height;
        }

        private void draw() {
            if (movie != null) {

                try {
                    if (visible) {
                        Canvas canvas = holder.lockCanvas();
                        canvas.save();
                        final float scaleFactorX = mDeviceWidth / (mAnimationWidth * 1.f);  //608 is image width
                        final float scaleFactorY = mDeviceHeight / (mAnimationHeight * 1.f);

                        // Adjust size and position to fit animation on the screen
                        canvas.scale(scaleFactorX, scaleFactorY);  // w,h  Size of displaying Item
                        movie.draw(canvas, 0, 0);  // position on x,y
                        canvas.restore();
                        holder.unlockCanvasAndPost(canvas);
                        movie.setTime((int) (System.currentTimeMillis() % movie.duration()));

                        handler.removeCallbacks(drawGIF);
                        handler.postDelayed(drawGIF, frameDuration);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            this.visible = visible;
            if (visible) {
                handler.post(drawGIF);
            } else {
                handler.removeCallbacks(drawGIF);
            }
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            handler.removeCallbacks(drawGIF);
        }
    }
}

0 个答案:

没有答案