我有一个ImageView,我需要在Java中获取图像宽度和高度,并将它们存储在一些变量中,并根据需要使用它一些次,但是当我想保存高度和宽度值时,它会将NULL值返回给消息字符串。如何从匿名内部类获取价值?
我创造了一些外界方法,并传递价值给它的存储值,但它没有工作过,同样我创建一个文本视图和我分配值TextView的文本,它显示在屏幕上,但是当我用getText()
对于GET文本它获得文本视图的默认文本,看看,没有任何方式从内部类获取价值,有什么办法从匿名内部类获取价值?
final String[] message = new String[2];
final ImageView iv = (ImageView)findViewById(R.id.main_item2);
final TextView tv = (TextView)findViewById(R.id.storePostions);
ViewTreeObserver vto = iv.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
iv.getViewTreeObserver().removeOnPreDrawListener(this);
int finalHeight, finalWidth;
finalHeight = iv.getMeasuredHeight();
finalWidth = iv.getMeasuredWidth();
message[0] = String.valueOf(finalHeight);
message[1] = String.valueOf(finalWidth);
return true;
}
});
Toast.makeText(MainActivity.this, "finalHeight = " + message[0] + " ,finalWidth = " + message[1], Toast.LENGTH_LONG).show();
// show "finalHeight = NULL ,finalWidth = NULL"
谢谢,那么如何运行onPreDraw(),然后运行Toast方法? – MojtabaSh
@MojtabaSh我编辑了一些建议。 – Douglas