Creating a listener 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 开发者_如何学C= 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?
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().
精彩评论