【问题标题】:how to draw image on surfaceview android如何在surfaceview android上绘制图像
【发布时间】:2012-10-06 09:35:28
【问题描述】:

我想在 SurfaceView 上添加图像并使其可点击。我可以通过xml使用布局来做到这一点。但我不能从代码动态地做到这一点。我什至不能使用 lockcanvas 在画布上绘制图像,因为我将 SurfaceHolder 类型设置为 SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS。所以我必须做些什么才能在我的应用程序的表面视图上绘制图像。请指导我?

【问题讨论】:

    标签: android image surfaceview


    【解决方案1】:

    类似的东西

    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的中心以及如何将其拉伸到整个surfaceview
    • 如何缩放位图或填充/拉伸以覆盖整个surfaceView?
    • 与上述类似的问题?
    猜你喜欢
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2013-05-19
    相关资源
    最近更新 更多