Android模仿To圈儿个人资料界面层叠淡入淡出显示效果

所属分类: 软件编程 / Android 阅读数: 924
收藏 0 赞 0 分享

前几天做的一个仿To圈个人资料界面的实现效果

下面是To圈的效果Gif图:

做这个东西其实也花了一下午的时间,一开始思路一直没理清楚,就开始盲目的去做,结果反而事倍功半。

以后要吸取教训,先详细思考清楚其中的逻辑关系,然后再开始动手写代码,这样比较容易理顺。

可以看到实现这个效果还是不难的,得分成以下三个步骤:

1:首先要有一个可拖动的详细资料布局(下半部分)。

2:上半部分可跟随移动。

3:标题栏由隐藏到显示。

涉及到的技术点有:

1:屏幕像素密度DP转化。

2:自定义视图的OnTouchListener事件

3:透明度属性动画的使用

根据这三个要求,设计的布局如下:

userinfo_layout.xml

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@android:color/white"> 
<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="260dp"> 
<ImageView 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/colorTheme"/> 
<LinearLayout 
android:id="@+id/mainheadview" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/top"></LinearLayout> 
</RelativeLayout> 
<LinearLayout 
android:id="@+id/myscrollLinearlayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#EEEEEE" 
android:clickable="true" 
android:orientation="vertical"> 
<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="60dp" 
android:background="@android:color/white"> 
<TextView 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
android:layout_marginLeft="20dp" 
android:gravity="center_vertical" 
android:text="守护天使" 
android:textColor="#DB4E61" 
android:textSize="14sp"/> 
<TextView 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
android:layout_alignParentTop="true" 
android:layout_marginEnd="66dp" 
android:layout_toStartOf="@+id/imageView" 
android:gravity="center_vertical" 
android:text="暂时没有守护天使" 
android:textColor="#C0C0C0" 
android:textSize="14sp"/> 
<ImageView 
android:id="@+id/imageView" 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
android:layout_alignParentEnd="true" 
android:layout_alignParentTop="true" 
android:layout_marginRight="20dp" 
android:paddingBottom="34dp" 
android:paddingTop="34dp" 
android:src="@drawable/ic_more"/> 
</RelativeLayout> 
<View 
android:layout_width="match_parent" 
android:layout_height="1dp" 
android:background="#CCCCCC" 
></View> 
<View 
android:layout_width="match_parent" 
android:layout_height="1dp" 
android:layout_marginTop="15dp" 
android:background="#CCCCCC" 
></View> 
<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="50dp" 
android:background="@android:color/white" 
android:orientation="horizontal"> 
<TextView 
android:layout_width="80dp" 
android:layout_height="match_parent" 
android:layout_marginLeft="15dp" 
android:gravity="center_vertical" 
android:text="昵称" 
android:textColor="#C0C0C0" 
android:textSize="14sp"/> 
<LinearLayout 
android:layout_width="0dp" 
android:layout_height="match_parent" 
android:layout_marginLeft="10dp" 
android:layout_weight="1" 
android:orientation="vertical" 
> 
<TextView 
android:layout_width="match_parent" 
android:layout_height="0dp" 
android:layout_weight="1" 
android:gravity="center" 
android:textColor="@android:color/black" 
android:textSize="12dp"/> 
<View 
android:layout_width="match_parent" 
android:layout_height="1dp" 
android:background="#CCCCCC" 
></View> 
</LinearLayout> 
</LinearLayout> 
<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="50dp" 
android:background="@android:color/white" 
android:orientation="horizontal"> 
<TextView 
android:layout_width="80dp" 
android:layout_height="match_parent" 
android:layout_marginLeft="15dp" 
android:gravity="center_vertical" 
android:text="个性签名" 
android:textColor="#C0C0C0" 
android:textSize="14sp"/> 
<TextView 
android:layout_width="0dp" 
android:layout_height="match_parent" 
android:layout_marginLeft="10dp" 
android:layout_weight="1" 
android:background="@android:color/transparent"/> 
</LinearLayout> 
<View 
android:layout_width="match_parent" 
android:layout_height="1dp" 
android:background="#CCCCCC" 
></View> 
<View 
android:layout_width="match_parent" 
android:layout_height="1dp" 
android:layout_marginTop="15dp" 
android:background="#CCCCCC" 
></View> 
<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="100dp" 
android:background="@android:color/white" 
android:orientation="vertical"> 
<TextView 
android:layout_width="match_parent" 
android:layout_height="35dp" 
android:layout_marginLeft="15dp" 
android:gravity="center_vertical" 
android:text="个人相册" 
android:textColor="#C0C0C0" 
android:textSize="14sp"/> 
<View 
android:layout_width="match_parent" 
android:layout_height="1dp" 
android:background="#CCCCCC" 
></View> 
</LinearLayout> 
<View 
android:layout_width="match_parent" 
android:layout_height="1dp" 
android:background="#CCCCCC" 
></View> 
<View 
android:layout_width="match_parent" 
android:layout_height="1dp" 
android:layout_marginTop="15dp" 
android:background="#CCCCCC" 
></View> 
<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="120dp" 
android:background="@android:color/white" 
android:orientation="vertical"> 
<TextView 
android:layout_width="match_parent" 
android:layout_height="30dp" 
android:layout_marginLeft="15dp" 
android:gravity="center_vertical" 
android:text="私房视频" 
android:textColor="#DB4E61" 
android:textSize="14sp"/> 
<TextView 
android:layout_width="match_parent" 
android:layout_height="20dp" 
android:layout_marginLeft="15dp" 
android:gravity="start" 
android:text="别人每查看你的一个私房视频,你讲获得100朵鲜花" 
android:textColor="#C0C0C0" 
android:textSize="10sp"/> 
<View 
android:layout_width="match_parent" 
android:layout_height="1dp" 
android:background="#CCCCCC" 
></View> 
</LinearLayout> 
<View 
android:layout_width="match_parent" 
android:layout_height="1dp" 
android:background="#CCCCCC" 
></View> 
<View 
android:layout_width="match_parent" 
android:layout_height="1dp" 
android:layout_marginTop="15dp" 
android:background="#CCCCCC" 
></View> 
<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="0dp" 
android:layout_weight="1" 
android:background="@android:color/white" 
android:orientation="vertical"> 
<TextView 
android:layout_width="match_parent" 
android:layout_height="35dp" 
android:layout_marginLeft="15dp" 
android:gravity="center_vertical" 
android:text="缘分印象" 
android:textColor="#C0C0C0" 
android:textSize="14sp"/> 
<TextView 
android:layout_width="match_parent" 
android:layout_height="0dp" 
android:layout_weight="1" 
android:gravity="center" 
android:text="Ta还没有缘分印象" 
android:textColor="#C0C0C0"/> 
</LinearLayout> 
</LinearLayout> 
<RelativeLayout 
android:id="@+id/mainactionbar" 
android:layout_width="match_parent" 
android:layout_height="55dp" 
> 
<ImageView 
android:id="@+id/userinfo_topbar" 
android:visibility="invisible" 
android:background="@color/colorTheme" 
android:layout_width="match_parent" 
android:layout_height="match_parent"/> 
<ImageButton 
android:id="@+id/userinfo_returnbtn" 
style="?android:attr/borderlessButtonStyle" 
android:layout_width="55dp" 
android:layout_height="55dp" 
android:padding="15dp" 
android:scaleType="fitCenter" 
android:src="@drawable/topbar_returnbtn"/> 
</RelativeLayout> 
</RelativeLayout>

该布局由内而外嵌套了三层,其中上半部分我直接截了To圈的图。

android:background="@drawable/top"

代码实现:

package com.whale.nangua.toquan; 
import android.animation.ObjectAnimator; 
import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.RelativeLayout; 
import android.widget.ScrollView; 
import android.widget.Toast; 
public class MainActivity extends Activity { 
LinearLayout myscrollLinearlayout; 
LinearLayout mainheadview; //顶部个人资料视图 
RelativeLayout mainactionbar; //顶部菜单栏 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.userinfo_layout); 
initView(); 
} 
int Y; 
int position = 0; //拖动Linearlayout的距离Y轴的距离 
int scrollviewdistancetotop = 0; //headView的高 
int menubarHeight = 0; 
int chufaHeight = 0; //需要触发动画的高 
float scale; //像素密度 
int headViewPosition = 0; 
ImageView userinfo_topbar; 
static boolean flag = true; 
static boolean topmenuflag = true; 
private void initView() { 
userinfo_topbar = (ImageView) findViewById(R.id.userinfo_topbar); 
//获得像素密度 
scale = this.getResources().getDisplayMetrics().density; 
mainheadview = (LinearLayout) findViewById(R.id.mainheadview); 
mainactionbar = (RelativeLayout) findViewById(R.id.mainactionbar); 
menubarHeight = (int) (55 * scale); 
chufaHeight = (int) (110 * scale); 
scrollviewdistancetotop = (int) ((260 )*scale); 
position = scrollviewdistancetotop; 
myscrollLinearlayout = (LinearLayout) findViewById(R.id.myscrollLinearlayout); 
myscrollLinearlayout.setY( scrollviewdistancetotop); //要减去Absolote布局距离顶部的高度 
myscrollLinearlayout.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
} 
}); 
myscrollLinearlayout.setOnTouchListener(new View.OnTouchListener() { 
@Override 
public boolean onTouch(View v, MotionEvent event) { 
switch (event.getAction()) { 
case MotionEvent.ACTION_DOWN: 
//按下的Y的位置 
Y = (int) event.getRawY(); 
break; 
case MotionEvent.ACTION_MOVE: 
int nowY = (int) myscrollLinearlayout.getY(); //拖动界面的Y轴位置 
int tempY = (int) (event.getRawY() - Y); //手移动的偏移量 
Y = (int) event.getRawY(); 
if ((nowY + tempY >= 0) && (nowY + tempY <= scrollviewdistancetotop)) { 
if ((nowY + tempY <= menubarHeight)&& (topmenuflag == true) ){ 
userinfo_topbar.setVisibility(View.VISIBLE); 
topmenuflag = false; 
} else if ((nowY + tempY > menubarHeight) && (topmenuflag == flag)) { 
userinfo_topbar.setVisibility(View.INVISIBLE); 
topmenuflag = true; 
} 
int temp = position += tempY; 
myscrollLinearlayout.setY(temp); 
int headviewtemp = headViewPosition += (tempY/5); 
mainheadview.setY(headviewtemp); 
} 
//顶部的动画效果 
if ((myscrollLinearlayout.getY() <= chufaHeight) && (flag == true)) { 
ObjectAnimator anim = ObjectAnimator.ofFloat(mainheadview, "alpha", 1, 0.0f); 
anim.setDuration(500); 
anim.start(); 
flag = false; 
} else if ((myscrollLinearlayout.getY() > chufaHeight + 40) && (flag == false)) { 
ObjectAnimator anim = ObjectAnimator.ofFloat(mainheadview, "alpha", 0.0f, 1f); 
anim.setDuration(500); 
anim.start(); 
flag = true; 
} 
break; 
} 
return false; 
} 
}); 
} 
}

代码实现思路:

主要是对以下三个Layout的操作:

LinearLayout myscrollLinearlayout; //底部可拖动的详细界面
LinearLayout mainheadview; //顶部个人资料视图
RelativeLayout mainactionbar; //顶部菜单栏

首先初始化各个布局部分的位置,然后主要是底部拖动布局的动态事件监听。

需要在这个监听方法中处理与另外两个布局的交互,这一点比较麻烦,不过代码里都有详细的注释。

然后这是我实现的效果图:

可以看到跟To圈的效果几乎一模一样哦。

以上所述是小编给大家介绍的Android模仿To圈儿个人资料界面层叠淡入淡出显示效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多