我们可以在一个活动的两个画布? (OR)具有的OnDraw(外的画布)不工作 [英] Can we have two canvases in an activity ? (OR) Having a canvas outside the onDraw() is not working

查看:163
本文介绍了我们可以在一个活动的两个画布? (OR)具有的OnDraw(外的画布)不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @覆盖
        保护无效的OnDraw(帆布油画){
        //绘制图形对象
         .....
             }

         公共无效displayCalc(){

           //做一些计算和放大器;接近这些图形对象的显示效果
            字符串结果=将String.valueOf(值);

             //做一些计算

              //显示计算值
             帆布C =新的Canvas();
             涂料粉刷=新的油漆();
             paint.setStyle(Paint.Style.FILL);
             paint.setAntiAlias​​(真正的);
             paint.setColor(Color.WHITE);
             c.drawText(结果,200300,油漆);
                     }
 

但是,如果我有在功能上同样的事情OnDraw中它工作正常。 我想知道为什么或什么样的变化,我必须让得到它的工作

  @覆盖
    保护无效的OnDraw(帆布油画){

    //绘制图形对象
    //做一些计算和放大器;接近这些图形对象的显示效果
     .....
     字符串结果=将String.valueOf(值);

        //显示计算值
         涂料粉刷=新的油漆();
         paint.setStyle(Paint.Style.FILL);
         paint.setAntiAlias​​(真正的);
         paint.setColor(Color.WHITE);
         canvas.drawText(结果,200300,油漆);
}
 

解决方案

如果你想实现某种形式的双缓冲的,你可能想看看的这个

我觉得你的问题是,你需要创建一个位图,然后在画布附加到它,是这样的:

 位图位= Bitmap.createBitmap(宽度,高度,Config.RGB_565);
帆布C =新的Canvas(位);

//再画到画布上..
//当你感到快乐,绘制位图到提供给OnDraw的画布。
 

刚刚创建的画布,不让它出现在屏幕上。

您可能也想看看:的本教程

如果你有一个surfaceView,那么你可以做这样的事情(没有一个编译器,但希望你得到的要点):

  SurfaceView视图=(SurfaceView)findViewById(R.id.view);

SurfaceHolder支架= view.getHolder(); //保存此它可以通过你的函数进行访问

帆布Ç
尝试  {
    C = holder.lockCanvas();
    //画的东西
}
最后 {
    如果(NULL!= C){
        holder.unlockCanvasAndPost(C);
    }
}
 

@Override
        protected void onDraw(Canvas canvas) {
        // Draw graphic objects
         .....
             }

         public void displayCalc(){

           //Do some calculation & display results near these graphic objects
            String result = String.valueOf(value);

             //Do some calculation

              //Display Calculated values
             Canvas c =new Canvas();
             Paint paint = new Paint();
             paint.setStyle(Paint.Style.FILL);
             paint.setAntiAlias(true);
             paint.setColor(Color.WHITE);
             c.drawText(result,200,300,paint);
                     }

But if i have the same thing in the function onDraw it works fine. I would like to know why or what changes i have to make to get it working

   @Override
    protected void onDraw(Canvas canvas) {

    // Draw graphic objects 
    //Do some calculation & display results near these graphic objects
     .....
     String result = String.valueOf(value);

        //Display Calculated values
         Paint paint = new Paint();
         paint.setStyle(Paint.Style.FILL);
         paint.setAntiAlias(true);
         paint.setColor(Color.WHITE);
         canvas.drawText(result,200,300,paint);
}

解决方案

If you're trying to implement some kind of double buffering, you might want to take a look at this

I think your problem is that you need to create a bitmap, then attach the canvas to it, something like:

Bitmap bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
Canvas c = new Canvas(bitmap);

// then draw to the canvas..
// and when you're happy, draw the bitmap onto the canvas supplied to onDraw.

Just creating a canvas, doesn't make it appear on the screen.

You may also want to take a look at: this tutorial

If you have a surfaceView, then you can do something like this (don't have a compiler, but hopefully you get the gist):

SurfaceView view = (SurfaceView)findViewById(R.id.view);

SurfaceHolder holder = view.getHolder(); // save this where it can be accessed by your function

Canvas c 
try  {
    c = holder.lockCanvas();
    // draw stuff
}
finally {
    if(null != c) {
        holder.unlockCanvasAndPost(c);
    }
}

这篇关于我们可以在一个活动的两个画布? (OR)具有的OnDraw(外的画布)不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆