功能描述
Layout的内容
android:id="@+id/route_bubble_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal">
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"/>
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。
一位android经验丰富的同事给了一个建议,将9patch图改成TextView的背景!直接调用TextView::draw(Canvas)函数, 试验结果果真很OK!
后话 气泡背景不完整问题,猜想问题出在 ViewGroup的脏区域的控制逻辑。。。