"SurfaceView控件"-出现的问题记录

发现问题:

  • 1.如果直接onCreate就画画,那么会空指针
  • 2.如果价格点击事件,就会回得以解决
  • 3.下次和同事讨论出结果后再来更新

代码演示:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="goodjobtome.com.surfaceview.MainActivity">

    <Button
        android:text="开始画画"
        android:onClick="click"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <SurfaceView
        android:id="@+id/sv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

MianActivity.java

public class MainActivity extends Activity {

    private SurfaceView mSf;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //直接在这里调用就会报错,空指针
        //meth();
    }
    //surfaceView能子在线程画画
    //加上点击就得以解决,有待研究
    public void click(View view) {
        meth();
    }

    private void meth() {
        mSf = (SurfaceView) findViewById(R.id.sv);
        // 1.控件获取控制器;2.控制器锁定画布并得到画布;3.定义画笔;4.解锁(画布)
        for (int radius = 0; radius < 300; radius+=5) {
            // 1.
            SurfaceHolder holder = mSf.getHolder();
            // 2.
            Canvas canvas = holder.lockCanvas();
            // 3.
            Paint paint = new Paint();
            paint.setColor(Color.GREEN);

            canvas.drawCircle(350, 450, radius, paint);
            // 4.
            holder.unlockCanvasAndPost(canvas);
        }
    }

}

运行结果:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值