ProgressDialog dialog = ProgressDialog.show(AContext, "Test", "On the bottom");
dialog.getWindow().setGravity(Gravity.BOTTOM);
Gallery 3D UI 非常炫, 如下图所示:
需要明确的几个问题 伪 2D 还是 3D:
gallery3d 基于 android SDK OpenGL ES 接口开发,使用了 Java API,没有使用 NDK。
图片如何显示:在 OpenGL ES 中,要显示图片,需要定义一个四边形,然后把图片当作 texture 贴到四边形上。
布局及特效如何实现:这是 gallery3d 的精华所在,需认真分析。
大数据量图片/cache 如何实现和管理:gallery3d 有缓冲区的设计,非常不错,需要认真分析。
动画引擎:简单的讲,动画引擎对外可表现为一个接口:
float animate(float initVal, float *currentVal, long timeElapsed, long duration)
即,给定初始值(initVal),动画引擎根据逝去的时间(timeElapsed)和动画总时间(duration)计算下一帧对应的值 (currentVal),这个值可能是位置坐标,也可能是一个矩阵 matrix,或者是其它的属性。显示一帧就调用该函数更新actor的属性,各个帧连起来显示就成了动画。
3D坐标与2D坐标的转换:这个需要仔细分析。
使用缓冲区对象 (GL_OES_vertex_buffer_object)OpenGL ES 中的顶点数组使得几何图元的显示方便了很多,但是如果每次都要向 OPENGL 发送一大块数据,而这数据其实并没有修改过,那么这传输就是冗余的。所以 这里添加了缓冲区对象,将顶点数组存储在服务器端的缓冲区对象中。
gallery3d 使用了缓冲区对象来保存顶点数据。
参考:
Nexus One Gallery on Android
构件图gallery3d 的基本构件组成及其关系如下所示:
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import com.tlt.util.Consts;
import com.tlt.util.Util;
import com.tlt.view.AppCanvas;
import com.tlt.view.CommonObject;
public class BallList extends CommonObject {
AppCanvas appCanvas;
// private Image[] balls;
// int ballWidth;
int ballCount;//小球个数
public boolean[] selected;//选中的小球
public boolean isLastLine,isFirstLine;//红框是否在最后一行或第一行
public int selectedCount;//选中的小球个数
public int min,max;//数字从min开始到max结束
public int index=0;//总索引index=indexY*col+indexX
public int indexX,indexY;
public int col=10,row;
public boolean isFocus=false;//是否显示小红框
public BallList(AppCanvas appCanvas,int min,int max){
this.appCanvas=appCanvas;
this.min=min;
this.max=max;
this.ballCount=max-min+1;
// balls=new Image[3];
// balls[0]=Util.getImage("/ball.png");
// balls[1]=Util.getImage("/ball_red.png");
// balls[2]=Util.getImage("/ball_blue.png");
// ballWidth=balls[0].getWidth();
}
public void close() {
// TODO Auto-generated method stub
// balls=null;
}
public void init() {
// TODO Auto-generated method stub
//根据屏幕大小判断一行显示小球的个数
if(Consts.SCREEN_HEIGHT>=300){
col=10;
}
else if(Consts.SCREEN_HEIGHT>=196){
col=8;
}
else if(Consts.SCREEN_HEIGHT>=128){
col=4;
}
//计算行数
if(ballCount%col==0){
row=ballCount/col;
}else{
row=ballCount/col+1;
}
resetPosition();
selected=new boolean[ballCount];
}
/**
* 设置红框的位置
*/
public void resetPosition(){
index=5;
indexX=5;
indexY=0;
}
public void keyPressed(int keyCode) {
// TODO Auto-generated method stub
switch (keyCode) {
case Consts.KEY_LS:
break;
case Consts.KEY_OK:
selected[index]=!selected[index];
if(selected[index]){
selectedCount++;
}else{
selectedCount--;
}
break;
case Consts.KEY_RS:
break;
case Consts.KEY_UP:
index-=col;
indexY--;
if(indexY<=0){
isFirstLine=true;
}
isLastLine=false;
break;
case Consts.KEY_DOWN:
index+=col;
indexY++;
if(index>ballCount-1){
index-=col;
indexY--;
isLastLine=true;
}
isFirstLine=false;
break;
case Consts.KEY_LEFT:
if(index>0){
index--;
indexX--;
if((index+1)%col==0){
indexY--;
indexX=col-1;
// if(indexY<0){
// indexY=0;
// indexX=0;
// }
}
}
break;
case Consts.KEY_RIGHT:
if(index<ballCount-1){
index++;
indexX++;
if(index%col==0){
indexY++;
indexX=0;
}
}
break;
}
}
public void keyReleased(int keyCode) {
// TODO Auto-generated method stub
}
public void paint(Graphics g){
}
public void paint(Graphics g,int x, int y) {
// TODO Auto-generated method stub
drawBall(g,x,y);
}
public void run() {
// TODO Auto-generated method stub
}
private void drawBall(Graphics g,int x, int y){
int j=0;
int gapX=0;
if((Consts.SCREEN_WIDTH-col*ballWidth)%(col+1)==0){
gapX=(Consts.SCREEN_WIDTH-col*ballWidth)/(col+1);
}else{
gapX=(Consts.SCREEN_WIDTH-col*ballWidth)/(col+1)+1;
}
int gap=ballWidth+gapX;
for(int i=0;i<ballCount;i++){
g.drawImage(selected[i]?balls[1]:balls[0], x+gapX+gap*i-j*(col*gap), y+j*gap, Graphics.TOP|Graphics.LEFT);
g.setColor(selected[i]?0xffffff:0x0);
g.drawString((i+min)+"", x+gapX+gap*i-j*(col*gap)+(ballWidth>>1), y+j*gap+(ballWidth>>1)+8, Graphics.BASELINE|Graphics.HCENTER);
if((i+1)%col==0){
j++;
}
}
//小红框
if(isFocus){
g.setColor(0xff0000);
g.drawRect(x+gapX+indexX*gap, y+indexY*gap, ballWidth-1, ballWidth-1);
}
}
}