2014-06-05 63 views
1

我正在測試android應用程序的一些代碼。我有2個視圖,一個是ImageView,另一個是FastRenderView,都是用Java實現的,而不是用Xml實現的。我想要做的是使FastrenderView全屏幕並將imageView置於屏幕的較小部分的頂部。問題是隻有fastrenderview出現在屏幕上。下面是代碼:如何以編程方式爲相對佈局設置z-index

... 
@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); 
      } 

} 

那麼,有沒有一種方法來設置視圖的Z指數編程,而無需使用XML?

回答

1

好的,找到它。問題是,在這條線:

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

我改變了插入硬編碼座標:

android.widget.RelativeLayout.LayoutParams testbox = new RelativeLayout.LayoutParams(100, 100); 
相關問題