我需要在ConstraintLayout中垂直移動某些視圖,最好是使用過渡效果(以避免「跳過」)。Android View在ConstraintLayout中以編程方式設置TransitionVerticalBias
佈局只是一個約束佈局,其中包含多個小部件(無層次結構),所有小部件都受限於父級。
我的佈局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:id="@+id/myLayout"
tools:context="com.example.quant.icarus.MainActivity">
<SurfaceView
android:id="@+id/surfaceView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/button1"
android:layout_width="125dp"
android:layout_height="48dp"
android:alpha="0.5"
android:background="@drawable/button_border"
android:textColor="@color/ret_text"
app:layout_constraintVertical_bias="0.97"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="some text" />
<Button
android:id="@+id/button2"
android:layout_width="125dp"
android:layout_height="48dp"
android:alpha="0.5"
app:layout_constraintVertical_bias="0.97"
app:layout_constraintHorizontal_bias="0.8"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="some text" />
<SeekBar
android:id="@+id/prog_bar"
android:layout_width="210dp"
android:layout_height="40dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:thumb="@drawable/seekbar_thumb"
android:max="100"
android:progress="0"
android:rotation="270" />
<TextView
android:id="@+id/prog_text"
android:layout_width="wrap_content"
android:layout_height="20dp"
app:layout_constraintVertical_bias="0.1"
app:layout_constraintLeft_toLeftOf="@+id/prog_bar"
app:layout_constraintRight_toRightOf="@+id/prog_bar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="some text"/>
<View
android:id="@+id/viewToMove1"
android:layout_width="40dp"
android:layout_height="1dp"
app:layout_constraintVertical_bias="0.2"
app:layout_constraintHorizontal_bias="0.34"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@color/ret_text" />
<View
android:id="@+id/viewToMove2"
android:layout_width="40dp"
android:layout_height="1dp"
app:layout_constraintVertical_bias="0.35"
app:layout_constraintHorizontal_bias="0.34"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@color/ret_text" />
<View
android:layout_width="1dp"
android:layout_height="30dp"
app:layout_constraintVertical_bias="0.35"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@color/ret_text" />
<View
android:layout_width="1dp"
android:layout_height="30dp"
app:layout_constraintVertical_bias="0.65"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@color/ret_text" />
</android.support.constraint.ConstraintLayout>
兩個觀點vewToMove1和viewToMove2將被重新編程。但是,當我嘗試以下時,我得到2個問題。 1)某些其他元素不正確地放置在屏幕上,但視圖移動 - 跳過。 2)如果我嘗試轉型,視圖移動完全不動(和其它元素仍然位置不正確。
下面是相關的Java代碼。
public class MainActivity extends AppCompatActivity {
ConstraintSet cSet = new ConstraintSet();
ConstraintLayout cLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
//Thread which updates views.
TimerTask move_views = new TimerTask(){
@Override
public void run() {
//some code here...
runOnUiThread(new Runnable() {
@Override
public void run() {
cLayout = (ConstraintLayout) findViewById(R.id.myLayout);//can this be put in onCreate or need to wait until render complete?
cSet.clone(cLayout);
//TransitionManager.beginDelayedTransition(cLayout);//adding this removes View movement.
cSet.setVerticalBias(R.id.viewToMove1,0.43f);//updated in the above thread.
cSet.setVerticalBias(R.id.viewToMove2,0.68f);
cSet.applyTo(cLayout);
}
});
}
};
Timer move_views_timer = new Timer();
move_views_timer.scheduleAtFixedRate(move_views,10,10);//time in milliseconds
}
}
我想不通爲什麼一些其他的觀點正在受到這個操作的影響以及爲什麼這個過渡不起作用
我有一個想法:我需要等待佈局在克隆約束之前完成加載(某種回調) ?我該怎麼做?
此作品但動畫不平滑 –