Android Studio,如何查找视图宽度和高度。getWidth()返回0
我遵循了Gautier Haroun关于如何创建触摸屏并允许点击和显示坐标的回答。但是我想通过分别用Android Studio,如何查找视图宽度和高度。getWidth()返回0,android,Android,我遵循了Gautier Haroun关于如何创建触摸屏并允许点击和显示坐标的回答。但是我想通过分别用文本视图宽度和高度分割来规范化坐标,以获得0~1之间的范围 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Too
文本视图
宽度和高度分割来规范化坐标,以获得0~1
之间的范围
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TextView textView = (TextView)findViewById(R.id.textView);
// this is the view on which you will listen for touch events
final View touchView = findViewById(R.id.touchView);
final float touchWidth = touchView.getWidth();
final float touchHeight = touchView.getHeight();
touchView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
textView.setText("Touch coordinates : " +
String.valueOf(event.getX()/touchWidth) + " x " + String.valueOf(event.getY()/touchHeight));
return true;
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
然后是布局:
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, TestActivity"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="@+id/touchView"
android:background="#f00"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:alpha="0.5" />
我尝试了最后的floattouchWidth=touchView.getWidth()代码>
但结果是无限的,所以触摸宽度是0,而不是我的例子中的1500左右。你能告诉我如何获取视图对象触摸视图的宽度和高度吗
谢谢您可以使用:
touchView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
touchView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
else {
touchView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
final float touchWidth = touchView.getWidth();
final float touchHeight = touchView.getHeight();
...
}
});
您的代码如下所示:
private float touchWidth;
private float touchHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TextView textView = (TextView)findViewById(R.id.textView);
// this is the view on which you will listen for touch events
final View touchView = findViewById(R.id.touchView);
touchView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(touchWidth == 0 || touchHeight == 0)
return false;
textView.setText("Touch coordinates : " +
String.valueOf(event.getX()/touchWidth) + " x " + String.valueOf(event.getY()/touchHeight));
return true;
}
});
touchView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
touchView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
else {
touchView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
touchWidth = touchView.getWidth();
touchHeight = touchView.getHeight();
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
是获得测量值的一种方法
ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < 16) {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
viewWidth = view.getWidth();
viewHeight = view.getHeight();
}
});
}
请注意,removeOnGlobalLayoutListener(此)
仅在启动SDK 16(4.1果冻豆)时起作用,谢谢。但我不确定如何返回touchWidth和touchHeight,并将它们传递给onCreate()中的touchWidth和touchHeight。只需在类中将touchWidth
和touchHeight
变量声明为全局变量,即可查看我的更新答案
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
viewWidth = view.getWidth();
viewHeight = view.getHeight();
}