我一直在努力寻找可以执行 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/