我有兩個視圖v1和v2。我的目標是,當用戶按下並保存v1時,v2將使用動畫進行擴展。當用戶放開v1時,v2停止擴展。按住按鈕修改視圖屬性
這是我的onCreate是什麼樣子:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
root = findViewById(R.id.root);
shape = findViewById(R.id.shape);
root.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
isExpanding = true;
expandView(shape);
} return true;
case MotionEvent.ACTION_UP: {
isExpanding = false;
shrinkView(shape);
} return true;
default: return false;
}
}
});
}
對於expandView()和shrinkView(),我嘗試添加含有isExpanding while循環,並增加在那裏的動畫:
private void shrinkView(final View view) {
while (!isExpanding) {
view.animate().scaleXBy(0.1f).scaleYBy(0.1f);
}
}
我也嘗試更改循環內的LayoutParams:
private void expandView(final View view) {
while (isExpanding) {
final ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
currWidth = layoutParams.width;
currHeight = layoutParams.height;
currWidth = +GROWTH_FACTOR;
currHeight = +GROWTH_FACTOR;
layoutParams.width += currWidth;
layoutParams.height += currHeight;
view.setLayoutParams(layoutParams);
view.invalidate();
}
}
但是這兩個塊都會e ui線程如預期。
Ui線程由於無限循環而被阻止 –
@AbhishekPatel正確,因此爲什麼我需要幫助:) –