有没有办法在没有XML文件的情况下进行动画过渡,我想将按钮1移到按钮2怎么做?
我的按钮
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".Activities.Home">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/btn1"/>
</androidx.constraintlayout.widget.ConstraintLayout>
答案 0 :(得分:0)
您可以使用View.setTranslationX()中的setTranslationX
和setTranslationY
并使用View.getLocationOnScreen()计算位移。
根据您的情况,您可以执行以下操作:
public class MainActivity extends AppCompatActivity {
private Button btn1, btn2;
private int[] pos_btn1 = new int[2];
private int[] pos_btn2 = new int[2];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Compute translation values in X and Y
int transX = pos_btn2[0] - pos_btn1[0];
int transY = pos_btn2[1] - pos_btn1[1];
// Move button
btn1.setTranslationX(transX);
btn1.setTranslationY(transY);
}
});}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// Get absolute location of your buttons here because
// getLocationOnScreen return 0 in onCreate
btn1.getLocationOnScreen(pos_btn1);
btn2.getLocationOnScreen(pos_btn2);
}
}
您可以添加循环以递增移动按钮以创建动画。
最佳
答案 1 :(得分:0)
我对布局进行了一些调整,以使button2不能随button1动画一起移动,这是因为当您尝试更改button1的约束时,这将直接影响button2约束的位置就像您发布的布局一样,您将两个按钮的顶部彼此关联。
新布局
<?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:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="196dp"
android:layout_marginBottom="196dp"
app:layout_constraintBottom_toTopOf="@+id/btn2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="243dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
行为:
public class MainActivity extends AppCompatActivity {
Button mButton1;
Button mButton2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton1 = findViewById(R.id.btn1);
mButton2 = findViewById(R.id.btn2);
mButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
moveButton1();
}
});
}
void moveButton1() {
ConstraintLayout rootLayout = findViewById(R.id.root);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // Transition available for API level 19
// Simulating transition delay
TransitionManager.beginDelayedTransition(rootLayout);
}
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(rootLayout);
constraintSet.clear(R.id.btn1, ConstraintSet.BOTTOM);
constraintSet.clear(R.id.btn1, ConstraintSet.TOP);
constraintSet.connect(R.id.btn1, ConstraintSet.BOTTOM, R.id.btn2, ConstraintSet.TOP);
constraintSet.applyTo(rootLayout);
}
}
结果
希望有帮助。