How to design a gridview in gl
Ok I couldn't figure out how to word this in a decent fashion so I will propose it as a "How would you?".
I have an app that connects to a service that interfaces 开发者_如何学Call of the data to one main activity. The activity is split in two by a sliding drawer. To top part of the drawered activity is just some buttons that help set up the app and act as a main menu to quickly get anywhere.
The bottom (revealed by drawer click) is the heart of the app, a gl window that display 9 benches that hold 6 widgets each. The widgets display useful data and must be able to be moved around freely and longclicked to open an options menu.
This is where my problem is, I was using a gridview to hold the widgets, but now that I'm using GL for the benches and the touch listener, I don't know how to overlay the gridview to the benches and still keep the touch movement alive for the the GL window AND still be able to interact with the gridview.
This leaves me asking for anyone's oppinion on how to go about this. At this point the only option I have is to figure out how to make ALL of the widgets (even though some may even be a bar graph) in gl. I would rather not do that since gl and I don't work nicely with one another. But I really can't figure out how to integrate these features and still make it functional. Any Ideas?!
CONVERTING GRIDVIEW TO BITMAP
This method is in my extension of gridview (WorkBench). Essentially what I'm doing is walking through an array of gridviews collecting the bitmaps of each view and then setting the bitmap as the texture to the panels.
The toBitmap from the WorkBench class
public Bitmap toBitmap() {
int x = getWidth();
int y = getHeight();
Bitmap b = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas();
canvas.setBitmap(b);
draw(canvas);
return b;
}
And here is where I init the WorkBenches
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
for (int i = 0; i < mWB.length; i++) {
mWB[i] = mBoundService.createBench(ARMHomescreen.this, i+"");
mWB[i].setLayoutParams(lp);
}
Whats happening is the WorkBenches dimensions are never being set, regardless of whether I use a LayoutParam or set the size in the toBitmap method...
You can use conventional layout to design your widgets. If your widgets can be at all complex this is certainly the best thing to do.
But OpenGL can't do anything with Android layouts, so what you do for each widget is :
- Inflate a private View containing your widget layout.
- Fill the your widget with the values to be displayed (text etc)
- The crucial bit: draw your widget to a private Bitmap (using
View.draw(Canvas)
). - Convert your bitmap to a GL texture for GL rendering
Obviously you only need to do #1 once, and #2 and #3 every time your widget changes some aspect of its display.
I have used this approach for an Augmented Reality app which has complex layouts floating on a camera preview and it works very well indeed.
ADDED LATER:
Here is a screen capture of my OpenGL overlay on a camera preview background:
The overlay items are drawn to private bitmaps using conventional layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:orientation="vertical"
android:layout_width="200dp"
android:layout_height="80dp"
android:background="@drawable/ar_regus_frame">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:singleLine="false"
android:textColor="@android:color/black"
android:textSize="16sp"
android:textStyle="bold"
android:shadowColor="@android:color/white"
android:shadowDx="0"
android:shadowDy="1"
/>
<TextView
android:id="@+id/distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="6dp"
android:background="@drawable/bkgnd_red_field"
android:textColor="@android:color/white"
android:textSize="16sp"
android:textStyle="bold"
/>
</RelativeLayout>
With this technique you enjoy the best of both worlds... you get to use Android's amazingly powerful layout system to design complex UI elements, and render them with the speed and fluidity of hardware-accelerated OpenGL.
ADDED 3rd FEB 2011 11:56am ish
Here is how you draw a view to a private bitmap:
// Inflate the view
View view = getLayoutInflater().inflate(R.layout.whatever, null);
// HERE YOU SET ALL YOUR VIEW AND CHILD VIEW PROPERTIES, TEXT ETC
// eg view.findViewById(R.id.foo).setSomeProperty(etc);
// Now we set the desired pixel size of our view. This is something the framework
// would normally do for you, as a result of the view being attached to the
// Window's view hierarchy. Since *this* view is *not* attached to the Window, we
// have to do it manually
// NB: omitted DP to pixels conversion for clarity
view.setLayoutParams(new LayoutParams(160, 160)));
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int width = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
view.layout(0, 0, width, height);
// Finally we draw our view to a private bitmap
Canvas canvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
view.draw(canvas);
// The bitmap can now be converted to a GL texture in the normal way.
精彩评论