歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android Layout背景繪制不全的問題

Android Layout背景繪制不全的問題

日期:2017/3/1 9:46:44   编辑:Linux編程

功能描述

在地圖上繪制氣泡, 氣泡本身復用Android view框架的布局和繪制邏輯:一個LinearLayout 內部有一個TextView,不同氣泡設置不同文本內容。

Layout的內容

<LinearLayout
android:id="@+id/route_bubble_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/route_bubble_bus_station"
android:maxWidth="130dp"
android:textColor="@color/des"
android:id="@+id/title_5" android:layout_gravity="center_vertical"/>
</LinearLayout>

route_bubble_bus_station是一個9patch圖,會根據view大小自動拉伸。

氣泡渲染的流程

顯式調用View>::draw(Canvas) 函數將view繪制到一個canvas,取出canvas的bitmap 傳入opengl作為紋理。具體實現有些小trick:

// bubbleView 是LinearLayout
// textView 是Layout內部的TextView
textView.setText(mBubbleStr);

// 顯式調用View的layout!
bubbleView.measure(0, 0);
// 獲取氣泡 view的寬高
int bubbleWidth = bubbleView.getMeasuredWidth();
int bubbleHeight = bubbleView.getMeasuredHeight();
// 獲取畫布
Canvas canvas = GLBitmapUtil.lockCanvas(bubbleWidth, bubbleHeight);
// 顯式調用布局函數
bubbleView.layout(0, 0, bubbleWidth, bubbleHeight);
// 顯式繪制到畫布
bubbleView.draw(canvas);

// @@@ 創建紋理
Bitmap canvasBmp = GLBitmapUtil.getLockedBitmap();
int textureName = GLRenderUtil.loadTexture(gl, canvasBmp);

bubbleView是一個LinearLayout,textView是其內部的實際view。

因為要生成紋理,上面整個過程在GLThread中執行。

問題

之前這個邏輯一直都沒問題,昨天代碼微調後發現,氣泡背景隨機性地繪制不完整問題: 顯示效果右圖:氣泡下部分丟失。通過GPU神器(adreno profiler)調試發現紋理本身有問題,左圖是氣泡紋理圖像。進一步說明整個繪制邏輯是很OK的。 bubbleView是一個LinearLayout,問題出在代碼片段中的 bubbleView.draw(canvas)語句。 無語中,android的問題。。

解決思路

一位android經驗豐富的同事給了一個建議,將9patch圖改成TextView的背景!直接調用TextView::draw(Canvas)函數, 試驗結果果真很OK!

後話 氣泡背景不完整問題,猜想問題出在 ViewGroup的髒區域的控制邏輯。。。

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved