2013-02-28 24 views
1

我的窗口調光有點問題。 基本上,我想將我的活動的dimAmount設置爲零,只要我將其滑動到一個方向即可。設置窗口只能在onCreate()中工作

請注意,我的活動顯示爲對話框,並且可以向左或向右滑動。這工作非常好。但是,在滑動後調用setWindowDim()方法時不起作用。

如果我在我的onCreate方法中調用了setWindowDim,那麼它工作得很好。 我不知道爲什麼它不工作在其他地方。

public class FinishDialog extends Activity { 

private FrameLayout frame = null; 
private GestureDetector gd = null; 
private int displaywidth = 0; 

private int original_x = 0; 
private int dialog_width = 0; 

private final int POS_LEFT = -1; 
private final int POS_MID = 0; 
private final int POS_RIGHT = 1; 

/** the dialogs current position, POS_MID on startup */ 
private int current_pos = POS_MID; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    //this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); 

    frame = new FrameLayout(this); 
    gd = new GestureDetector(this, new TouchManager(this)); 

    // fill the challenge layout into the framelayout 
    frame.addView(LayoutInflater.from(getBaseContext()).inflate(R.layout.finishdialog, null)); 

    setContentView(frame);  

    FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) frame.getLayoutParams(); 

    GameLogic g = new GameLogic(); 

    displaywidth = g.getDisplayWidth(this); 

    lp.width = displaywidth - displaywidth/6; 
    lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL; 

    LinearLayout llFinish = (LinearLayout) findViewById(R.id.llFinishDialog); 

    LinearLayout.LayoutParams lpLL = (LinearLayout.LayoutParams) llFinish.getLayoutParams(); 
    lp.height = lpLL.height; 

    frame.setLayoutParams(lp); 

    // setWindowDim(0.0f); ---> if i set the dim here, it works perfectly fine 

    original_x = (int) frame.getX(); 
    dialog_width = lp.width; 
} 

private void setWindowDim(float dim) { 

    Log.i("FinishDialog", "Setting window dim to " + dim + "."); 

    WindowManager.LayoutParams lpWin = getWindow().getAttributes(); 
    lpWin.dimAmount = dim; 
    getWindow().setAttributes(lpWin); 
} 

/** 
* move the dialog, depending on the swipe direction and the 
* current position of the dialog 
* @param action 
*/ 
public void interpretTouch(int action) { 

    switch(action) { 
    case TouchManager.MOVE_DIALOG_RIGHT: 

     if(frame != null) { 

      if(current_pos == POS_MID) { 

       swipeDialog(600, frame, 0, dialog_width, 0, 0); 
       current_pos = POS_RIGHT; 
       setWindowDim(0.0f); // here setting the dim has no effect 

      } else if (current_pos == POS_LEFT) { 

       swipeDialog(600, frame, - dialog_width, original_x, 0, 0); 
       current_pos = POS_MID; 
       setWindowDim(0.9f); // here setting the dim has no effect 

      } else if (current_pos == POS_RIGHT) { 
       // if we are on the right and swipe right, nothing happens 
      } 
     } 

     Log.i("Challenge", "Moving dialog to the right."); 
     break; 
    case TouchManager.MOVE_DIALOG_LEFT: 

     if(frame != null) { 

      if(current_pos == POS_MID) { 

       swipeDialog(600, frame, 0, -dialog_width, 0, 0); 
       current_pos = POS_LEFT; 
       setWindowDim(0.0f); // here setting the dim has no effect 

      } else if (current_pos == POS_LEFT) { 

       // if we are on the left and swipe left, nothing happens 
      } else if (current_pos == POS_RIGHT) { 

       swipeDialog(600, frame, dialog_width, original_x, 0, 0); 
       current_pos = POS_MID; 
       setWindowDim(0.9f); // here setting the dim has no effect 
      } 
     } 

     Log.i("Challenge", "Moving dialog to the left."); 
     break; 
    } 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) {  

    return gd.onTouchEvent(event); 
} 

private void swipeDialog(int time, View framelayout, int fromDeltaX, int toDeltaX, int fromDeltaY, int toDeltaY) { 

    Log.i("FinishDialog", "Dialog swiped."); 

    TranslateAnimation trans = new TranslateAnimation(fromDeltaX, toDeltaX, 
      fromDeltaY, toDeltaY); 

    trans.setFillAfter(true); 
    trans.setFillEnabled(true); 
    trans.setDuration(time); 
    framelayout.startAnimation(trans); 
} 

}

+0

*爲什麼它只在onCreate()中工作?可能是因爲只有在調用setContentView()之前設置窗口標誌並查看結果。至於一個快速修復,我想你可以有一個刷新活動,就像[this](http://stackoverflow.com/a/7658364/1815485)回答。 – 2013-02-28 22:23:40

+1

如果您查看我的onCreate代碼,即使在設置內容視圖後,您仍然可以將窗口設置爲暗淡,並且它仍然有效。這就是爲什麼我如此困惑,在其他地方無法正常工作。 – 2013-02-28 22:26:54

回答

0

開始這個活動之後我改變窗口的朦朧終於做了工作。 不幸的是,現在屏幕在RefreshActivity開始時閃爍很「醜」,有沒有人有解決方案?

import android.app.Activity; 
import android.os.Bundle; 

public class RefreshActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 

     finish(); 
    } 
}