为什么canvas = null在我的情况下? [英] Why canvas = null in my case?

查看:144
本文介绍了为什么canvas = null在我的情况下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    Button btn;
    SurfaceView sv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button)findViewById(R.id.button1);
        sv =  (SurfaceView)findViewById(R.id.surfaceView1);
        sv.setOnTouchListener( new SurfaceView.OnTouchListener(){
            private boolean moving = false;//stupid state
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch( event.getAction() ){
                    case MotionEvent.ACTION_DOWN:
                        final int x = (int)event.getX();
                        final int y = (int)event.getY();
                        final SurfaceHolder mHolder;
                        mHolder = sv.getHolder();
                        Canvas canvas = null;
                        canvas = mHolder.lockCanvas();
                        Paint mPaint = new Paint();
                        mPaint.setColor(Color.WHITE);
                        try{
                        canvas.drawText("some text", 50, 50, mPaint);
                        }catch (Exception xx)
                        {
                            xx.toString();//canvas = null
                        }
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        if( moving ){
                            final int x_new = (int)event.getX();
                            final int y_new = (int)event.getY();
                                }

                        return true;
                    case MotionEvent.ACTION_UP:
                        moving = false;
                        return true;
                }
                return false;
            }
        });
    }



我只想在触摸或移动时绘制一些信息。我不想使用OnDraw方法,因为它吃CPU。

按照建议编辑:


I just want to draw some info when I touched or moved. I don''t want to use OnDraw method because it eat CPU.
Edited as was suggested:

@Override
protected void onPause()
{
    mHolder = sv.getHolder();
    canvas = null;
    canvas = mHolder.lockCanvas();
}



但它没有帮助,这个功能没有执行,画布仍然= null。

我也试过这个,在按钮OnClick:


But it not helped, this func doesn''t executes, canvas still = null.
I also tried this, in button OnClick:

mPaint = new Paint();
mPaint.setColor(Color.WHITE);
mHolder = sv.getHolder();
canvas = null;
canvas = mHolder.lockCanvas();
try{
   canvas.drawText("some text", 100, 100, mPaint);
}catch (Exception xx)
{
  xx.toString();
}



- 当我第一次点击按钮画布!= null时,但在canvas.drawText执行后无处可去,当我第二次点击时 - canvas =空值。我的大脑腐蚀了。


- When I first click on button canvas != null, but after canvas.drawText execution go in nowhere, when I make second click - canvas = null. My brain corrode.

推荐答案

SurfaceView.getHolder()



<当您仍在 onCreate()方法中时,
无法保持有效值。它还没有在那个运行时初始化。



活动有其他方法





can not hold a valid value while you are still in the onCreate() method. It''s not yet initialized at that runtime.

Activity has other methods

protected void onResume(); // before Activity run
protected void onPause(); // after Activity run





这是与 SurfaceView 互动的更好地方。



请同时阅读有关这些生命周期控制方法的文档:



活动 [ ^ ] @Android Documentation



That''s a better place to interact with the SurfaceView.

Please also read in the documentation about these lifecycle controling methods:

Activity[^] @ Android Documentation


这篇关于为什么canvas = null在我的情况下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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