创建监听器 onRotate()

发布于 2024-11-30 03:10:57 字数 1333 浏览 3 评论 0原文

我一直在尝试找到一个可以执行seekmark.markPlace() 的监听器。该函数接受我的布局中定义的seekBar 的尺寸和填充。当我从 onCreate 调用该函数时,它会返回 0 作为seekBar 的值。我需要一个可以调用 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() 之类的?

I've been trying to find a listener that can execute seekmark.markPlace(). The function takes in the dimensions and padding for a seekBar defined in my layout. When I call the function from onCreate it returns 0 for the values of the seekBar. I need a listener that can call the function outside of onCreate. The function should only run if the screen is rotated (when layout-land is created).

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

    }

}

Is it possible to make your own listener like onRotate() or something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小苏打饼 2024-12-07 03:10:57

既然无论如何,当设备旋转时 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 变量的初始化。

Since onCreate is going to be called when the device is rotated anyways, why not just use a private variable to store the orientation of the display. Each time onCreate() is called, you can compare to see if the display orientation is changed:

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

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

    // Insert code for handling rotation here
}

I'll leave it to you to decide how to handle initialization of the mPreviousRotation variable during the first invocation of onCreate().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文