创建监听器 onRotate()
我一直在尝试找到一个可以执行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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
既然无论如何,当设备旋转时 onCreate 都会被调用,为什么不直接使用私有变量来存储显示器的方向呢?每次调用 onCreate() 时,您都可以比较以查看显示方向是否更改:
我将让您决定如何在第一次调用 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:
I'll leave it to you to decide how to handle initialization of the mPreviousRotation variable during the first invocation of onCreate().