IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android ConstraintLayout 2.1 功能 Carousel -> 正文阅读

[移动开发]Android ConstraintLayout 2.1 功能 Carousel

?

?

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

    }
}

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-08-26 12:13:18  更:2021-08-26 12:15:17 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年5日历 -2025/5/2 0:59:07-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码