2011-08-17 88 views
2

我一直在试图找到一个可以执行seekmark.markPlace()的侦听器。该函数接受在我的布局中定义的seekBar的尺寸和填充。当我从onCreate调用函数时,它将返回0作为seekBar的值。我需要一个可以调用onCreate之外的函数的监听器。该功能只能在屏幕旋转的时候运行(当创建布局平面时)。创建侦听器onRotate()

public class seekMark { 
    private int seekLength;  // in pixels 
    private int seekLeftPad; // in pixels 
    private int seekBottomPad; // in pixels 
    private int trackLength; // in ms 

    public seekMark(){ 
     seekLength = progressBar.getWidth(); 
     seekLeftPad = progressBar.getPaddingLeft(); 
     seekBottomPad = progressBar.getPaddingBottom(); 
     trackLength = player.getDuration(); 
    } 

    private int pxPerMs(){ 
     return (seekLength/trackLength); 
    } 

    public void markPlace() throws XmlPullParserException, IOException { 
     Drawable marker = context.getResources().getDrawable(R.drawable.audio); 
     int y = seekBottomPad; 
     int x = 0; 
     int w = marker.getIntrinsicWidth(); 
     int h = marker.getIntrinsicHeight(); 
     int[] bmPos = markPxList(); // returns a list of px (from the left) read from a xml database 

     for(int i = 0; i <= bmPos.length; i++){ 
      x = bmPos[i] + seekLeftPad; 
      marker.setBounds(x, y, x + w, y + h); 
      marker.draw(canvas); 
     } 

    } 

} 

是否有可能让自己的侦听器像onRotate()或什么?

回答

0

由于onCreate将在设备旋转时被调用,为什么不使用私有变量来存储显示的方向。每一次的onCreate()被调用时,你可以比较一下,看是否显示方向改变:

Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); 
int rotation = display.getRotation(); 

if(rotation != mPreviousRotation) { 
    mPreviousRotation = rotation; 

    // Insert code for handling rotation here 
} 

我将让你来决定的onCreate的第一次调用过程中如何处理mPreviousRotation变量初始化()。

+0

我试过了。问题在于创建是我制作progressBar的地方。所以它还不存在,并且seekLength,seekLeftPad和seekBottomPad的值为零。 –

+0

如果你能解释你想要完成的是什么,这可能会有所帮助。你是否试图将寻找标记位置设置为与旋转之前的位置相匹配? – glorifiedHacker

+0

我试图将图像放置到进度条上。现在,我正在通过在按下按钮时运行它来测试seekmark.markPlace()函数。我碰到了一个问题:“Drawable marker = context.getResources()。getDrawable(R.drawable.audio);”该程序在运行时会崩溃。 –