?
?
XML?? 3个状态
previous? start next
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 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"
app:layoutDescription="@xml/activity_main_a_a_a_a_scene">
<androidx.constraintlayout.utils.widget.ImageFilterView
android:id="@+id/imageView1"
android:layout_width="80dp"
android:layout_height="200dp"
android:layout_marginEnd="16dp"
android:scaleType="centerCrop"
android:src="@drawable/bryce_canyon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:round="16dp" />
<androidx.constraintlayout.utils.widget.ImageFilterView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:scaleType="centerCrop"
android:src="@drawable/fitzgerald_marine_reserve"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/imageView3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:round="16dp" />
<androidx.constraintlayout.utils.widget.ImageFilterView
android:id="@+id/imageView3"
android:layout_width="80dp"
android:layout_height="200dp"
android:layout_marginStart="16dp"
android:scaleType="centerCrop"
android:src="@drawable/death_valley"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:round="16dp" />
<androidx.constraintlayout.utils.widget.ImageFilterView
android:id="@+id/imageView4"
android:layout_width="80dp"
android:layout_height="200dp"
android:layout_marginStart="16dp"
android:scaleType="centerCrop"
android:src="@drawable/goldengate"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView3"
app:layout_constraintTop_toTopOf="parent"
app:round="16dp" />
<androidx.constraintlayout.helper.widget.Carousel
android:id="@+id/carousel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:carousel_backwardTransition="@+id/backward"
app:carousel_firstView="@+id/imageView2"
app:carousel_forwardTransition="@+id/forward"
app:carousel_infinite="true"
app:carousel_nextState="@+id/next"
app:carousel_previousState="@+id/previous"
app:constraint_referenced_ids="imageView1,imageView2,imageView3,imageView4" />
</androidx.constraintlayout.motion.widget.MotionLayout>
scene
<?xml version="1.0" encoding="utf-8"?>
<MotionScene
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
motion:constraintSetStart="@id/start"
motion:constraintSetEnd="@+id/next"
motion:duration="1000"
android:id="@+id/forward">
<KeyFrameSet>
</KeyFrameSet>
<OnSwipe
motion:dragDirection="dragLeft"
motion:touchAnchorSide="left" />
</Transition>
<Transition
motion:constraintSetStart="@+id/start"
motion:constraintSetEnd="@+id/previous"
android:id="@+id/backward">
<OnSwipe
motion:dragDirection="dragRight"
motion:touchAnchorSide="right" />
</Transition>
<ConstraintSet android:id="@+id/previous">
<Constraint
android:id="@+id/imageView2"
android:layout_width="80dp"
android:layout_height="200dp"
motion:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="16dp"
motion:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="16dp"
motion:layout_constraintEnd_toEndOf="parent" />
<Constraint
android:id="@+id/imageView3"
android:layout_width="80dp"
android:layout_height="200dp"
motion:layout_constraintBottom_toBottomOf="parent"
android:layout_marginStart="16dp"
motion:layout_constraintTop_toTopOf="parent"
motion:layout_constraintStart_toEndOf="@id/imageView2" />
<Constraint
android:id="@+id/imageView1"
motion:layout_constraintEnd_toStartOf="@id/imageView2"
android:layout_width="0dp"
android:layout_height="200dp"
motion:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="16dp"
motion:layout_constraintTop_toTopOf="parent"
motion:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginRight="16dp" />
</ConstraintSet>
<ConstraintSet android:id="@+id/start">
<Constraint
motion:layout_constraintEnd_toEndOf="parent"
android:layout_width="80dp"
android:layout_height="200dp"
motion:layout_constraintBottom_toBottomOf="parent"
android:layout_marginStart="16dp"
motion:layout_constraintTop_toTopOf="parent"
android:id="@+id/imageView3"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp" />
<Constraint
motion:layout_constraintEnd_toStartOf="@id/imageView3"
android:layout_width="0dp"
android:layout_height="200dp"
motion:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="16dp"
motion:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="16dp"
motion:layout_constraintStart_toStartOf="parent"
android:id="@+id/imageView2"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp" />
<Constraint
motion:layout_constraintEnd_toStartOf="parent"
android:layout_width="80dp"
android:layout_height="200dp"
motion:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="0dp"
motion:layout_constraintTop_toTopOf="parent"
android:id="@+id/imageView1"
android:layout_marginRight="0dp" />
</ConstraintSet>
<ConstraintSet android:id="@+id/next">
<Constraint0
android:layout_height="200dp"
motion:layout_constraintTop_toTopOf="parent"
motion:layout_constraintBottom_toBottomOf="parent"
android:layout_width="0dp"
motion:layout_constraintEnd_toStartOf="parent"
android:id="@+id/imageView1"
android:layout_marginRight="0dp"
android:layout_marginEnd="0dp" />
<Constraint
android:layout_marginEnd="0dp"
android:layout_height="200dp"
android:layout_marginRight="0dp"
motion:layout_constraintTop_toTopOf="parent"
motion:layout_constraintBottom_toBottomOf="parent"
android:layout_width="80dp"
android:id="@+id/imageView2"
motion:layout_constraintEnd_toStartOf="parent" />
<Constraint
android:layout_height="200dp"
motion:layout_constraintStart_toStartOf="parent"
motion:layout_constraintTop_toTopOf="parent"
motion:layout_constraintBottom_toBottomOf="parent"
android:layout_width="0dp"
android:layout_marginStart="16dp"
android:id="@+id/imageView3"
motion:layout_constraintEnd_toStartOf="@id/imageView4"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp" />
<Constraint
android:layout_height="200dp"
motion:layout_editor_absoluteX="112dp"
motion:layout_constraintTop_toTopOf="parent"
motion:layout_constraintBottom_toBottomOf="parent"
android:layout_width="80dp"
android:id="@+id/imageView4"
motion:layout_constraintEnd_toEndOf="parent"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp" />
</ConstraintSet>
</MotionScene>
public class MainActivityAAAAA extends AppCompatActivity {
int images[] = {
R.drawable.bryce_canyon,
R.drawable.cathedral_rock,
R.drawable.death_valley,
R.drawable.fitzgerald_marine_reserve,
R.drawable.goldengate,
R.drawable.golden_gate_bridge,
R.drawable.shipwreck_1,
R.drawable.shipwreck_2,
R.drawable.grand_canyon,
R.drawable.horseshoe_bend,
R.drawable.muir_beach,
R.drawable.rainbow_falls,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_a_a_a_a);
Carousel carousel = findViewById(R.id.carousel);
carousel.setAdapter(new Carousel.Adapter() {
@Override
public int count() {
return images.length;
}
@Override
public void populate(View view, int index) {
if (view instanceof ImageView) {
ImageView imageView = (ImageView) view;
imageView.setImageResource(images[index]);
}
}
@Override
public void onNewItem(int index) {
Toast.makeText(MainActivityAAAAA.this, index + "", Toast.LENGTH_LONG).show();
}
});
}
}
|