您的位置:首页 > 移动开发 > Android开发

android自定义控件画布canvas背景色失效变黑色

2017-04-13 10:10 288 查看
错误示范:

*****************************************************************************************************************************************************************************************

页面显示:

gps_view_activity.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:background="@color/satellites_bg"

    >

    <TextView android:id="@+id/gps_status_text"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"  

        android:text="状态"/>

        

    <TextView android:id="@+id/lonlat_text"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"  

        android:text="0 0"/>

    //自定义控件显示卫星分布状态图

    <ylybbs.study.mygpstest.SatellitesView

        android:id="@+id/satellitesView"

        android:layout_width="match_parent"

        android:layout_height="fill_parent" />

    

</LinearLayout>

*****************************************************************************************************************************************************************************************

自定义控件画图:

    public void run() {        

        List<GpsSatellite> list=null;

        Canvas c = null;

        

        try {

            c = surfaceHolder.lockCanvas(null);

           //设置画布背景色为白色,即自定义控件显示的背景色为白色:

            c.drawRGB(255,255,255);

            //初始化画板的中心坐标

            cx = c.getWidth() / 2;

            cy = c.getWidth()  / 2;

            synchronized (surfaceHolder) {

                doDraw(c,null);

            }

        } finally {

            if (c != null) {

                surfaceHolder.unlockCanvasAndPost(c);

            }

        }

        while (isRunning) {

            try{

                list = queue.take();                

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

            try {

                c = surfaceHolder.lockCanvas(null);

                synchronized (surfaceHolder) {

                    doDraw(c,list);

                }

            } finally {

                if (c != null) {

                    surfaceHolder.unlockCanvasAndPost(c);

                }

            }

        }

    }

    private void doDraw(Canvas canvas, List<GpsSatellite> satellites) {

        if (canvas != null) {

            // 绘制背景罗盘

            drawBackground(canvas, cx, cy, compassRadius);

            //绘制卫星分布

            if (satellites != null) {

                for (GpsSatellite satellite : satellites) {

                    drawSatellite(canvas,satellite, cx, cy, compassRadius);

                }

            }

        }

    }

结果设置的画布的颜色失效,自定义控件背景显示为黑色:



正确修改绘图java文件:

*****************************************************************************************************************************************************************************************
public void run() {        

        List<GpsSatellite> list=null;

        Canvas c = null;

        

        try {

            c = surfaceHolder.lockCanvas(null);

           //设置画布背景色为白色,即自定义控件显示的背景色为白色:

         //   c.drawRGB(255,255,255);

         //此处设置画布颜色不行,得到doDraw()设置

            //初始化画板的中心坐标

            cx = c.getWidth() / 2;

            cy = c.getWidth()  / 2;

            synchronized (surfaceHolder) {

                doDraw(c,null);

            }

        } finally {

            if (c != null) {

                surfaceHolder.unlockCanvasAndPost(c);

            }

        }

        while (isRunning) {

            try{

                list = queue.take();                

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

            try {

                c = surfaceHolder.lockCanvas(null);

                synchronized (surfaceHolder) {

                    doDraw(c,list);

                }

            } finally {

                if (c != null) {

                    surfaceHolder.unlockCanvasAndPost(c);

                }

            }

        }

    }

    private void doDraw(Canvas canvas, List<GpsSatellite> satellites) {

        if (canvas != null) {

           canvas.drawRGB(255,255,255);            // 绘制背景罗盘

            drawBackground(canvas, cx, cy, compassRadius);

            //绘制卫星分布

            if (satellites != null) {

                for (GpsSatellite satellite : satellites) {

                    drawSatellite(canvas,satellite, cx, cy, compassRadius);

                }

            }

        }

    }

经过上述修改,画布显示为其设置的颜色。



综上所述,画布背景色的设置,应该在doDraw()函数中设置,而不能直接在run()初始化时设置。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: