在onRotate()上创建一个监听器

时间:2011-08-17 18:30:50

标签: android listener rotation dimensions seekbar

我一直在努力寻找一个可以执行seekmark.markPlace()的监听器。该函数接受我的布局中定义的seekBar的尺寸和填充。当我从onCreate调用该函数时,它返回0表示seekBar的值。我需要一个可以调用onCreate之外的函数的监听器。该功能仅在旋转屏幕时才会运行(当创建layout-land时)。

    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()那样创建自己的监听器?

1 个答案:

答案 0 :(得分: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变量的初始化。