gpt4 book ai didi

Android Studio,如何找到 View 的宽度和高度。 getWidth() 返回 0

转载 作者:太空狗 更新时间:2023-10-29 13:13:40 26 4
gpt4 key购买 nike

我遵循了 Gautier Haroun 回答的关于如何创建触摸屏并允许点击和显示坐标的线程。但我想通过分别除以 text view 宽度和高度来标准化坐标,以获得 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" />

我尝试了 final float touchWidth = touchView.getWidth();但结果是无限的,所以 touch Width 在我的例子中是 0 而不是 1500 左右。能告诉我如何获取View对象touchView的宽高吗?

谢谢

最佳答案

你可以使用这个:

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();
}
});
}

关于Android Studio,如何找到 View 的宽度和高度。 getWidth() 返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37248884/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com