我正在为Android应用测试一些代码。我有2个视图,一个是ImageView,另一个是FastRenderView,都是用Java而不是Xml实现的。我要做的是使FastrenderView全屏显示,并将imageView放在屏幕较小部分的顶部。问题是只有fastrenderview出现在屏幕上。这是代码:

...
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    assMan = getAssets();

    rocketImage = new ImageView(this);
    rocketImage.setBackgroundResource(R.drawable.rocket);
    anime = (AnimationDrawable) rocketImage.getBackground();

    layout = new RelativeLayout(this);

    android.widget.RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    renderView = new FastRenderView(this);

    android.widget.RelativeLayout.LayoutParams testbox = new RelativeLayout.LayoutParams(screenWidth/4,screenHeight/4);

    layout.addView(renderView, 0, params);
    layout.addView(rocketImage, 1,  testbox);
    setContentView(layout);

    renderView.resume();
    gDetector = new GestureDetector(getBaseContext(), this);
    try{
        is=assMan.open("pot.png");
        bm = BitmapFactory.decodeStream(is);
        bm = bm.copy(Config.ARGB_8888 ,true);
        is.close();

        is = assMan.open("boo1.png");
        bm1 = BitmapFactory.decodeStream(is);
        bm1 = bm1.copy(Config.ARGB_8888 ,true);
        is.close();

        is = assMan.open("boo3.png");
        bm2 = BitmapFactory.decodeStream(is);
        bm2 = bm2.copy(Config.ARGB_8888 ,true);
        is.close();
        } catch (Exception e) {
                e.printStackTrace();
            }


    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    screenHeight = metrics.heightPixels;
    screenWidth = metrics.widthPixels;
    scaleFactorX = screenWidth/480f;
    scaleFactorY = screenHeight/800f;


    dst.set((int)(0*scaleFactorX),(int)(0*scaleFactorY),(int)(479*scaleFactorX),(int)(799*scaleFactorY));
    src.set(0, 0, 1024, 1024);

    dst1.set((int)(0*scaleFactorX),(int)(0*scaleFactorY),(int)(100*scaleFactorX),(int)(100*scaleFactorY));
    src1.set(0, 0, 1024, 1024);

    dst2.set((int)(200*scaleFactorX),(int)(200*scaleFactorY),(int)(400*scaleFactorX),(int)(400*scaleFactorY));
    src2.set(0, 0, 1024, 1024);

}

class FastRenderView extends SurfaceView implements Runnable{
    Thread renderThread = null;
    SurfaceHolder holder;
    volatile boolean running = false;

    @Override
    public boolean onTouchEvent(MotionEvent me){
        return gDetector.onTouchEvent(me);
    }

    public FastRenderView(Context context) {
        super(context);
        setKeepScreenOn(true);
        holder=getHolder();
    }

    public void resume() {
        running = true;
        renderThread = new Thread(this);
        renderThread.start();
        }

    public void pause() {
        running = false;
        while(true) {
            try {
                renderThread.join();
                break;
            } catch (InterruptedException e) {
                // retry
            }
        }
        renderThread = null;
    }

    public void run() {
        while(running) {
        if(!holder.getSurface().isValid())
        continue;
        Canvas canvas = holder.lockCanvas();
        drawSurfice(canvas);
        holder.unlockCanvasAndPost(canvas);
        }
    }


    private void drawSurfice(Canvas canvas){
            canvas.drawColor (Color.BLACK);
            canvas.drawBitmap(bm, src, dst, null);
            canvas.drawBitmap(bm1, src1, dst1, null);
            canvas.drawBitmap(bm2, src2, dst2, null);
             }

}


因此,有没有一种方法可以在不使用xml的情况下以编程方式设置视图的z索引?

最佳答案

好,找到了。问题出在这一行:

    android.widget.RelativeLayout.LayoutParams testbox = new RelativeLayout.LayoutParams(screenWidth/4,screenHeight/4);


我通过插入硬编码的坐标进行了更改:

android.widget.RelativeLayout.LayoutParams testbox = new RelativeLayout.LayoutParams(100, 100);

09-25 21:24