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

本文档详细介绍了在Android自定义控件中,如何正确设置Canvas背景色的问题。错误地在`run()`方法中设置背景色会导致颜色失效,显示为黑色。正确的做法是在`doDraw()`方法内设置背景色,从而确保颜色正常显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误示范:

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

页面显示:

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()初始化时设置。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值