简单实现 流行社交APP (探探)(谁) 侧滑效果

该博客介绍如何在Android中实现类似探探应用的侧滑效果。通过`DrawerLayout`和`ObjectAnimator`,动态调整内容视图的平移,模拟侧滑抽屉的打开和关闭动画。示例代码详细展示了如何设置监听器和动画效果。
package com.fanwf.inke.testdrawablelayout;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {
    private DrawerLayout dl_layout;
    private RelativeLayout rl_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rl_content=(RelativeLayout)findViewById(R.id.rl_content);
        dl_layout= (DrawerLayout) findViewById(R.id.dl_layout);
        dl_layout.setScrimColor(getResources().getColor(R.color.traslateColor));
        dl_layout.setDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                int width= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300, getResources().getDisplayMetrics());
                ObjectAnimator animator=ObjectAnimator.ofFloat(rl_content,"translationX",drawerView.getId()==R.id.rl_right?-slideOffset*width:slideOffset*width);
                animator.setDuration(0);
                animator.start();
            }

            @Override
            public void onDrawerOpened(View drawerView) {

            }

            @Override
            public void onDrawerClosed(View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
    }

}


<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/dl_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/rl_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="ABCDEFG123456ABCDEFG123456ABCDEFG123456ABCDEFG123456ABCDEFG123456ABCDEFG123456ABCDEFG123456"
            android:id="@+id/textView"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="59dp" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_left"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        android:layout_gravity="start">

    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/rl_right"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:background="@color/colorAccent2"
        android:layout_gravity="end">

    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值