android - 创建监听器 onRotate()

标签 android listener rotation dimensions seekbar

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

    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() 之类的?

最佳答案

既然无论如何都会在设备旋转时调用 onCreate,为什么不使用私有(private)变量来存储显示的方向。每次调用 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 变量的初始化。

关于android - 创建监听器 onRotate(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7097666/

相关文章:

Android 默认音调选择器问题(带有通知和警报的默认音调)

android - JNCryptor - RNCryptor 图像文件加密/解密

java - 如何监听 java.awt.Window 屏幕的变化?有没有办法在使用 setFullScreenWindow() 时将焦点集中到其他窗口/程序?

java - 从另一个 Activity 调用时为空监听器

javascript - jQuery Slidetoggle + 旋转

Android - 具有 boolean 错误的 ContentValue 数据库?

Android JSON 发布到 WCF 服务

单击按钮时不会调用 Android OnClickListener

CSS3 : Transition hover and animation rotation bug

ios - 旋转后 iPad 模态 Controller 被解散