简体   繁体   English

用户触摸时如何在recyclerview中缩放项目?

[英]How to zoom an item in a recyclerview when the user touches it?

I am developing an application in which should have a vertical Recyclerview in which the items will be enlarged when touched by users and decreased when the user releases them. 我正在开发一个应具有垂直Recyclerview的应用程序,其中用户触摸时这些项目将放大,而用户释放它们则将其减少。

In my research I have not found a way to build it. 在我的研究中,我还没有找到构建它的方法。 The most similar to what I look for are the Facebook reactions as in the GIF below. 与我所寻找的最相似的是下面GIF中的Facebook反应。

Facebook的反应

How can I do it? 我该怎么做? Thanks in advance 提前致谢

Аll items are visible to user, there is no views to recycle. 所有项目对用户可见,没有可回收的视图。 So IMHO RecyclerView here is redundant. 因此,恕我直言, RecyclerView在这里是多余的。 I suggest you to use separate View s. 我建议您使用单独的View As I understand the main difficulty here is animation. 据我了解,这里的主要困难是动画。 This type of animations can be easily done with Transition API . 使用Transition API可以轻松完成此类动画。 Just change image sizes then call TransitionManager.beginDelayedTransition with Transition which will animate all changes. 只要改变图像大小,然后调用TransitionManager.beginDelayedTransitionTransition ,这将动画的所有变化。 In this example changes are simple, default AutoTransition can animate them. 在此示例中,更改很简单,默认的AutoTransition可以对其进行动画处理。 Here is my implementation: 这是我的实现:

在此处输入图片说明

import androidx.appcompat.app.AppCompatActivity;
import androidx.transition.AutoTransition;
import androidx.transition.Transition;
import androidx.transition.TransitionManager;

public class MainActivity extends AppCompatActivity {

    private ViewGroup parent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        parent = findViewById(R.id.parent);

        for (int i = 0; i < parent.getChildCount(); i++) {
            int position = i;
            parent.getChildAt(i).setOnClickListener(v -> {
                onImageClicked(position);
            });
        }
    }

    private void onImageClicked(int position) {
        Transition transition = new AutoTransition();
        TransitionManager.beginDelayedTransition(parent, transition);

        int SIZE_MAX = dpToPx(100);
        int SIZE_MIN = dpToPx(40);

        for (int i = 0; i < parent.getChildCount(); i++) {
            View childAt = parent.getChildAt(i);
            int size = i == position ? SIZE_MAX : SIZE_MIN;
            childAt.setLayoutParams(new LayoutParams(size, size));
        }
    }

    public int dpToPx(int dp) {
        return (int) (dp * getResources().getDisplayMetrics().density);
    }
}

activity_main.xml activity_main.xml中

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eee">

    <View
        app:layout_constraintBottom_toBottomOf="@+id/parent"
        app:layout_constraintLeft_toLeftOf="@+id/parent"
        app:layout_constraintRight_toRightOf="@+id/parent"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:layout_centerInParent="true"
        android:background="#50b0" />

    <LinearLayout
        android:id="@+id/parent"
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        android:gravity="bottom"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <include layout="@layout/image" />
        <include layout="@layout/image" />
        <include layout="@layout/image" />
        <include layout="@layout/image" />
        <include layout="@layout/image" />
        <include layout="@layout/image" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

image.xml image.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="50dp"
    android:layout_height="50dp"
    android:src="@drawable/circle"
    android:scaleType="centerCrop"/>

At start each image size is 50dp. 开始时,每个图像大小为50dp。 Total width is 6*50 =300dp . 总宽度为6*50 =300dp After click selected image size is 100dp, others are 40dp so total width is 100+5*40=300dp . 单击后,所选图像大小为100dp,其他图像大小为40dp,因此总宽度为100+5*40=300dp I mean that why total width doesn't change while clicking. 我的意思是为什么单击时总宽度不会改变。

You can put GIFs into ImageView instead of my red circles with Glide or other library. 您可以使用Glide或其他库将GIF放入ImageView而不是我的红色圆圈。 Another thing is you need to move animation logic from click events to touch events. 另一件事是您需要将动画逻辑从单击事件转移到触摸事件。

<!-- This initially-hidden ImageView will hold the expanded/zoomed version of
     the images above. Without transformations applied, it takes up the entire
     screen. To achieve the "zoom" animation, this view's bounds are animated
     from the bounds of the thumbnail button above, to its final laid-out
     bounds.
     -->

<ImageView
    android:id="@+id/expanded_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="invisible"
    android:contentDescription="@string/description_zoom_touch_close" />

visit https://developer.android.com/training/animation/zoom for more detail 详情请访问https://developer.android.com/training/animation/zoom

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM