简体   繁体   中英

SurfaceView shows black screen except icon

I used the old guide according to which the icon should appear in surfaceview.

Unfortunately the app dispalys the entire black surfaceview except icon. I do not know how to fix this because android studio does not display errors. What i should add or change?

public class mySurfaceView extends SurfaceView {

        private SurfaceHolder surfaceHolder;
        private Bitmap bmpIcon;

        public mySurfaceView(Context context) {
            super(context);
            init();
        }

        public mySurfaceView(Context context,
                             AttributeSet attrs) {
            super(context, attrs);
            init();
        }

        public mySurfaceView(Context context,
                             AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init();
        }

        private void init(){
            surfaceHolder = getHolder();
            bmpIcon = BitmapFactory.decodeResource(getResources(),
                    R.drawable.icon);
            surfaceHolder.addCallback(new SurfaceHolder.Callback(){

                @Override
                public void surfaceCreated(SurfaceHolder holder) {
                    Canvas canvas = holder.lockCanvas(null);
                    drawSomething(canvas);
                    holder.unlockCanvasAndPost(canvas);
                }

                @Override
                public void surfaceChanged(SurfaceHolder holder,
                                           int format, int width, int height) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void surfaceDestroyed(SurfaceHolder holder) {
                    // TODO Auto-generated method stub

                }});
        }

        protected void drawSomething(Canvas canvas) {
            canvas.drawColor(Color.BLACK);
    if (bmpIcon != null){        canvas.drawBitmap(bmpIcon,
                    getWidth()/2, getHeight()/2, null);
        }


        }

    }

ScreenShot

my icon

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <path
        android:fillColor="#FF020000"
        android:pathData="M6,18c0,0.55 0.45,1 1,1h1v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L11,19h2v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L16,19h1c0.55,0 1,-0.45 1,-1L18,8L6,8v10zM3.5,8C2.67,8 2,8.67 2,9.5v7c0,0.83 0.67,1.5 1.5,1.5S5,17.33 5,16.5v-7C5,8.67 4.33,8 3.5,8zM20.5,8c-0.83,0 -1.5,0.67 -1.5,1.5v7c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5v-7c0,-0.83 -0.67,-1.5 -1.5,-1.5zM15.53,2.16l1.3,-1.3c0.2,-0.2 0.2,-0.51 0,-0.71 -0.2,-0.2 -0.51,-0.2 -0.71,0l-1.48,1.48C13.85,1.23 12.95,1 12,1c-0.96,0 -1.86,0.23 -2.66,0.63L7.85,0.15c-0.2,-0.2 -0.51,-0.2 -0.71,0 -0.2,0.2 -0.2,0.51 0,0.71l1.31,1.31C6.97,3.26 6,5.01 6,7h12c0,-1.99 -0.97,-3.75 -2.47,-4.84zM10,5L9,5L9,4h1v1zM15,5h-1L14,4h1v1z"/>
</vector>

You gave black color in drawSomething method, If you change the color your background will change.

protected void drawSomething(Canvas canvas) {
        //canvas.drawColor(Color.BLACK);
        canvas.drawColor(Color.RED); // here i changed black to red(add you expected color here)
        if (bmpIcon != null) {
            canvas.drawBitmap(bmpIcon,
                    getWidth() / 2, getHeight() / 2, null);
        }

    }

As your drawble is vector drawable so use the following function to convert into bitmap

public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
        Drawable drawable = ContextCompat.getDrawable(context, drawableId);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            drawable = (DrawableCompat.wrap(drawable)).mutate();
        }

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }

and create your bitmap like this

bmpIcon = getBitmapFromVectorDrawable(getContext(), R.drawable.icon);

it will work fine.

and your final code will be like this,

public class MySurface extends SurfaceView {

    private SurfaceHolder surfaceHolder;
    private Bitmap bmpIcon;

    public MySurface(Context context) {
        super(context);
        init();
    }

    public MySurface(Context context,
                     AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MySurface(Context context,
                     AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
        Drawable drawable = ContextCompat.getDrawable(context, drawableId);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            drawable = (DrawableCompat.wrap(drawable)).mutate();
        }

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }

    private void init() {
        surfaceHolder = getHolder();

        bmpIcon = getBitmapFromVectorDrawable(getContext(), R.drawable.icon);
        surfaceHolder.addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                Canvas canvas = holder.lockCanvas(null);
                drawSomething(canvas);
                holder.unlockCanvasAndPost(canvas);
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder,
                                       int format, int width, int height) {
                // TODO Auto-generated method stub

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                // TODO Auto-generated method stub

            }
        });
    }

    protected void drawSomething(Canvas canvas) {
        canvas.drawColor(Color.RED);
        if (bmpIcon != null) {
            canvas.drawBitmap(bmpIcon,
                    getWidth() / 2, getHeight() / 2, null);
        }

    }

}

屏幕截图

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