本文介绍了如何借鉴surfaceview机器人形象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加图像surfaceview,使之为可点击。我可以使用布局通过XML做到这一点。但我不能够从code动态地做到这一点。我甚至不能用lockcanvas画在画布上的图像,因为我把我的SurfaceHolder类型SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS.So什么,我必须做绘制图像surfaceview在我的应用程序。请指导我?

i want to add image on surfaceview and make it as clickable. I could do it via xml using layout. But i am not able to do it dynamically from code. I even cannot use lockcanvas to draw image on canvas since i set my SurfaceHolder type to SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS.So what i have to do to draw image on surfaceview in my app. Pls guide me?

推荐答案

有些这样的事情

public class MSurface extends SurfaceView implements SurfaceHolder.Callback {

    public MSurface(Context context) {
        super(context);
        getHolder().addCallback(this);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(icon, 10, 10, new Paint());        
    }

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

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Canvas canvas = null;
        try {
            canvas = holder.lockCanvas(null);
            synchronized (holder) {
                onDraw(canvas);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (canvas != null) {
                holder.unlockCanvasAndPost(canvas);
            }
        }
    }

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

    }
}

这篇关于如何借鉴surfaceview机器人形象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 02:54