使用com.lorentzos.swipecards创建Tantan探探,tinder般的翻卡片效果

效果是这个样子滴

导入方法

  1. 首先在Gradle中添加一下代码
dependencies {
    compile 'com.lorentzos.swipecards:library:X.X.X@aar'
}
  1. 在activity中添加一下代码
public class MyActivity_Simple extends Activity {
    private ArrayList<String> al;
    private ArrayAdapter<String> arrayAdapter;
    private int i;

    @InjectView(R.id.frame) SwipeFlingAdapterView flingContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        ButterKnife.inject(this);

        al = new ArrayList<>();
        al.add("java");
        al.add("html");
        al.add("css");
        al.add("javascript");

        arrayAdapter = new ArrayAdapter<>(this, R.layout.item, R.id.helloText, al );

        flingContainer.setAdapter(arrayAdapter);
        flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
            @Override
            public void removeFirstObjectInAdapter() {
                // this is the simplest way to delete an object from the Adapter (/AdapterView)
                al.remove(0);
                arrayAdapter.notifyDataSetChanged();
            }
            @Override
            public void onLeftCardExit(Object dataObject) {
                //Do something on the left!
                //You also have access to the original object.
                //If you want to use it just cast it (String) dataObject
            }
            @Override
            public void onRightCardExit(Object dataObject) {
            }
            @Override
            public void onAdapterAboutToEmpty(int itemsInAdapter) {
            }
            @Override
            public void onScroll(float scrollProgressPercent) {
            }
        });
    }xml
}
  1. 在activity的XML文件中添加一下代码
<merge
    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" >

    <com.lorentzos.flingswipe.SwipeFlingAdapterView
        android:id="@+id/frame"
        android:background="#ffeee9e2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:rotation_degrees="15.5"
        tools:context=".MyActivity" />
</merge>
  1. 添加一个名为item.xml的文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_gravity="center"
    android:layout_width="250dp"
    android:layout_height="170dp">
    <TextView
        android:id="@+id/helloText"
        android:textSize="40sp"
        android:textColor="@android:color/white"
        android:background="#E339"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <View
        android:id="@+id/item_swipe_left_indicator"
        android:alpha="0"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_margin="10dp"
        android:background="#A5F" />
    <View
        android:id="@+id/item_swipe_right_indicator"
        android:alpha="0"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_margin="10dp"
        android:layout_gravity="right"
        android:background="#5AF" />
</FrameLayout>

实现方法的介绍

1. 卡片的层叠效果:使用frameLayout

- 所有添加到这个布局中的视图都是以层叠的方式显示。
- 第一个添加到布局中视图显示在最底层,最后一个被放在最顶层。
- 上一层的视图会覆盖下一层的视图,因此该布局类似于堆栈布局。
- 默认对其方式是左上角对齐
- 若要设置子视图的位置,
- 要设置layout_gravity属性值。

2. 图片移动的动画,使用OnTouchListener,MotionEvent

一个简单的移动图片的例子

public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
    private int _xDelta;
    private int _yDelta;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView j = (ImageView) findViewById(R.id.imageView);
        j.setOnTouchListener(this);
    }

    public boolean onTouch(View view, MotionEvent event) {
        final int X = (int) event.getRawX();
        final int Y = (int) event.getRawY();
        ImageView j = (ImageView) findViewById(R.id.imageView);
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                _xDelta = (int) (X - j.getTranslationX());
                _yDelta = (int) (Y - j.getTranslationY());
                break;
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                break;
            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_MOVE:
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                j.setTranslationX(X - _xDelta);
                j.setTranslationY(Y - _yDelta);
                break;
        }
        return true;
    }
}

3. 使用线性回归来预测用户是否要去掉图片(Linear Regression)

如果不用线性回归的话,用户需要将图片移动到指定地点才行

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
无论你是一个程序猿还是一个程序媛,每天干的事除了coding还是coding,代码不能解决的问题(什么问题自己心里还没点abcd数嘛),探探能帮你解决。最近网上特流行一款交友软件叫探探(据说是yp软件)。作为探探曾经的一名从来只浏览图片但是没有yue过的资深玩家同时又是一位热爱前端的妹子,我决定要仿一下这个app。既然是寄几开发,那还不是寄几说了算,毫无疑问整款APP的主题风格被我改成我最爱的终极少女粉了hhh,下面让我们一起来感受下探探的魅力吧~项目整体效果项目部分功能点解析主页图片左滑右滑对应按钮变化首先我们来聊一下最让我头痛的地方,就是主页面的左右滑动事件并且对应按钮会相应变化,即我左滑一下图片下面的灰色按钮会有相应的动画效果,右滑则对应着图片下面的红色按钮。对于一个刚入小程序坑的妹子来说,没有大神指点不知道要在这里面的逻辑坑还要绕多久才能绕出来。得一高人指点,我才完美滴实现了这个功能。这里写了三个大的盒子放着图片和文字信息,再将他们放到swiper-item里面,用swiper组件实现整个盒子的左右滑动                                          K             ♂21             金牛座             文化/教育                哦盒子下面不是按钮,我是放了两张图片。             先给他们写个滑动的时候触发的动画效果.active {    animation: active 1s ease;//定义一个叫做active的动画} @keyframes active {//补充active动作脚本     0% {        transform: scale(0.8);     }     50% {        transform: scale(1.2);     }     100% {        transform: scale(1.0);     } }在page的data里面定义三个变量,将left,right变量绑定到对应图片中data: {        left: false ,       right: false,        activeIndex: 0 },在swiper绑定事件中具体判断左右滑动事件changeswiper: function(e) {         var index = e.detail.current;//当前所在页面的 index     if(index > this.data.activeIndex) {//左滑事件判断       this.setData({         left: true//若为左滑,left值为true,触发图片动画效果       })     } else if(index  {//每滑动一次,数据发生变化       this.setData({         activeIndex: index,         left:false,         right:false       })     }, 1000);   },从本地上传图片这个呀查一查小程序开发文档就好了,先在要上传图片的地方的src绑定个数据变量放入图片默认地址,就是上传图片之前的添加图片data: {     imgUrl: '../../images/addImg.png'   },通过绑定tap事件将上传的图片地址替换进去uploadImg: function(e) { var that = this; wx.chooseImage({   count: 1, //上传图片数量   sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有   sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有   success: function (res) {// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片       var tempFilePaths = res.tempFilePaths;       that.setData({           imgUrl: tempFilePaths     })       wx.showToast({//显示上传成功           title: '上传成功',           icon: 'success',           duration: 2000     })   } }),配对成功列表据通过easy-mock获取后台数据block wx:for渲染一个包含多节点的结构块                                                                                                                         {{item.nickname}}                     {{item.message}}                                            获取后台数据wx.request({       url: 'https://www.easy-mock.com/mock/5a23dbf382614c0dc1bebf04/getFriendsList/getFriendsList',       success: (res) => {         // console.log(response);         this.setData({           friendsList: res.data.data.friendsList         })       }     })其它差不多就是切页面了,个人原因用不太习惯weui的官方样式,每个页面都是我自己呕心沥血码出来的,所以大家不喜轻点喷哈,还在努力学习当中~~~项目开发用到的一些工具微信开发者工具、VScode、GithubIconfont阿里巴巴矢量图标库:各种图片logo应有尽有,前端开发必备esay-mock:模拟数据请求,实现无后端编程W3Cschool微信小程序开发教程手册文档:开发小程序要多看看哦小结emmmm目前项目功能还是很简单呀,还有很多功能后面慢慢实现吧~比如利用将上传的图片放到storage中,页面刷新之后图片依然在,slider滑动到某一处在页面上保存当前值,模拟配对成功后弹出提醒页面等等......也希望遇到热爱学习的小伙伴一起交流学习,一起在前端坑里越陷越深hhh项目地址:https://github.com/beautifulg... 求鼓励~求star呀~我的邮箱:804316947@qq.com 这里可以找到我哦作者:略略略
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值