Android Native API (getWidth etc) for determining attributes of images return wrong value
In Android while trying to get/set the attributes of an Image I am getting Image attributes as zero which I guess is not a correct value. Please help me on this. here's the excerpt of the code:
CODE
开发者_StackOverflow社区public class CumulativeGoalProgress extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cumulative_goals);
showCumulativeGoalProgress();
}
private void showCumulativeGoalProgress(){
ImageView wellnessBarIndicator = (ImageView)findViewById(R.id.iv_welness_bar_indicator);
Log.e("Ravi", "progBarImage getBaseline = " + progBarImage.getBaseline());
Log.e("Ravi", "progBarImage getHeight = " + progBarImage.getHeight());
Log.e("Ravi", "progBarImage getMeasuredHeight = " + progBarImage.getMeasuredHeight());
Log.e("Ravi", "progBarImage getMeasuredWidth = " + progBarImage.getMeasuredWidth());
Log.e("Ravi", "progBarImage getWidth = " + progBarImage.getWidth());
Log.e("Ravi", "progBarImage getBottom = " + progBarImage.getBottom());
Log.e("Ravi", "progBarImage getLeft = " + progBarImage.getLeft());
Log.e("Ravi", "progBarImage getNextFocusLeftId = " + progBarImage.getNextFocusLeftId());
Log.e("Ravi", "progBarImage getPaddingLeft = " + progBarImage.getPaddingLeft());
Log.e("Ravi", "progBarImage getPaddingTop = " + progBarImage.getPaddingTop());
}
}
The ImageView as defined in the XML
<ImageView
android:id="@+id/iv_welness_bar_indicator"
android:layout_marginTop="119px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/vis_wellness_bar_indicator"
/>
vis_wellness_bar_indicator is the source Image file in PNG format.
What am I doing wrong?
What is happening is that during the onCreate
phase the view isn't yet constructed, so the image hasn't been loaded into the ImageView yet. Call the same code later, for example on the OnDraw
or on OnSizeChanged
method.
精彩评论