使用RecyclerView实现点赞头像叠加效果

概要:点赞头像效果使用的地方很多,实现的方式也很多,下面通过使用RecyclerView实现一下

1、创建布局文件 一个recyclerview,一个点赞图片

<?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:layout_marginTop="50dp"
    android:orientation="horizontal">

  <androidx.recyclerview.widget.RecyclerView
      android:id="@+id/rv_like_header"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="15dp"
      android:layoutAnimation="@anim/rv_slide_in_anim"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent" />

  <ImageView
      android:id="@+id/iv_like"
      android:layout_width="16dp"
      android:layout_height="16dp"
      android:layout_marginTop="15dp"
      android:layout_marginEnd="15dp"
      android:src="@drawable/img_explore_like"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_goneMarginEnd="15dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

recyclerview添加了动画效果,动画文件如下

rv_slide_in_anim

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
  android:animation="@anim/slide_in_left"
  android:animationOrder="normal"
  android:delay="20%" />

slide_in_left

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

  <translate
    android:duration="350"
    android:fromXDelta="-100%p"
    android:toXDelta="0" />

  <alpha
    android:fromAlpha="0.8"
    android:toAlpha="1.0"
    android:duration="350"
    />

</set>

2、Activity逻辑代码

val headerUrl=R.drawable.dog_01
    rv_like_header.layoutManager=LinearLayoutManager(this).apply { orientation=LinearLayoutManager.HORIZONTAL }
    val headerList= arrayListOf(headerUrl)
    val mAdapter= LikeMemberHeaderAdapter(this,headerList)
    rv_like_header.adapter=mAdapter
    iv_like.setOnClickListener {
      mAdapter.addData(headerUrl)
    }

3、Adapter代码

/**
 * @date 2020/7/24
 * @author peter
 * @desc 点赞头像adapter
 */
class LikeMemberHeaderAdapter(context: Context, data: ArrayList<Int>) : RecyclerView.Adapter<LikeMemberHeaderAdapter.HeaderHolder>() {
  private var mDatas: ArrayList<Int> = data
  private val mContext = context

  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HeaderHolder {
    return HeaderHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_member_header, parent, false))
  }

  override fun getItemCount(): Int {
    return mDatas.size
  }

  fun addData(headerUrl: Int) {
    // 从左边添加
    mDatas.add(0, headerUrl)
    notifyDataSetChanged()
  }

  override fun onBindViewHolder(holder: HeaderHolder, position: Int) {
    Glide.with(mContext).load(mDatas[position]).into(holder.ivMemberHeader)
    // 最后一个显示全部
    if (position == mDatas.size - 1) {
      setMargins(holder.headerRoot, 0, 0, 0, 0)
    } else {
      setMargins(holder.headerRoot, 0, 0, -12, 0)
    }
  }

  private fun setMargins(v: View, l: Int, t: Int, r: Int, b: Int) {
    if (v.layoutParams is MarginLayoutParams) {
      val p = v.layoutParams as MarginLayoutParams
      p.setMargins(l, t, r, b)
      v.requestLayout()
    }
  }

  class HeaderHolder(view: View) : RecyclerView.ViewHolder(view) {
    val headerRoot: FrameLayout = view.findViewById(R.id.lin_header_root)
    val ivMemberHeader: RoundImageView = view.findViewById(R.id.iv_member_header)
  }

}

核心代码就是通过设置item的margin值实现叠加效果,通过添加数据,刷新adapter实现,示例代码中默认从索引0添加的,可以根据需求自行调整,RoundImageView是一个自定义的圆形图片,此处不再赘述

4、 Adapter item布局文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="@+id/lin_header_root"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <com.example.myapplication.likeHeader.RoundImageView
    android:id="@+id/iv_member_header"
    android:layout_width="17dp"
    android:layout_height="17dp"
    android:scaleType="centerCrop"
    app:maskDrawable="@drawable/circle_mask"
    tools:src="@drawable/member_default_header" />
</FrameLayout>

5、效果图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android手机拍照或选取图库图片作为头像

    package zhangpgil.photo; import java.io.File; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import android.content.Intent; import

  • Android头像上传功能的实现代码(获取头像加剪切)

    因为项目中需要用到头像上传的功能,所以就下个Ddmo先来实现下. demo我是类似仿微信的,在一个GridView中展示所有的图片,其中第一个item可以去照相:获取到图片后再进行剪切. 图片的剪切是从网上找的感觉不错就用,暂时也没有测试. 获取图片可以用:https://github.com/lovetuzitong/MultiImageSelector来实现 这里的圆形图像是用https://github.com/hdodenhof/CircleImageView来实现的 Demo写的比较粗

  • Android实现用户头像更换操作

    你以为头像更换很容易?或许对于用户来讲,在微信上更换一个头像只是点击头像,选择拍照或相册,裁剪返回而已.但是对于程序员来说,要实现其实也挺吃力的(小火柴花了一个下午整理~_~). 正如用户使用那样,代码的实现也是按照操作的顺序而逐步展开.如下图: 接下来主要来讲解一下代码: 1. 弹框选择相册或拍照 比较简单的方式就是直接使用AlertDialog弹出选项供用户进行选择 public static void showImagePickDialog(final Activity activity)

  • Android Studio实现带边框的圆形头像

    本文实例为大家分享了Android Studio实现带边框的圆形头像的具体代码,供大家参考,具体内容如下 效果显示: (没有边框的) (有边框的) 1.创建自定义ImagView控件 (1).没有边框的 package chenglong.activitytest.pengintohospital.utils; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitma

  • Android根据电话号码获得联系人头像实例代码

    在日常Android手机的使用过程中,根据电话号码获得联系人头像,是经常会碰到的问题.本文即以实例形式讲述了Android根据电话号码获得联系人头像是实现代码.分享给大家供大家参考之用.具体方法如下: 首先,通过ContentProvider,可以访问Android中的联系人等数据.常用的Uri有: 联系人信息Uri:content://com.android.contacts/contacts 联系人电话Uri:content://com.android.contacts/data/phone

  • Android实现个人资料页面头像背景模糊显示包(状态栏)

    最近要实现这样一个效果,然后拿出来与大家分享一下主要的几段代码,希望大家能够用到,与人方便自己方便嘛! 首先: 要实现的是浮动状态栏效果,通过在Activity的onCreate方法中调用这个方法,然后就可以让整个布局浮现在整个手机屏幕之下了,这是我觉着最简单的一种方法了. public static void alphaTask(Activity context) { context.getWindow().requestFeature(Window.FEATURE_NO_TITLE); if

  • Android实现本地上传图片并设置为圆形头像

    先从本地把图片上传到服务器,然后根据URL把头像处理成圆形头像. 因为上传图片用到bmob的平台,所以要到bmob(http://www.bmob.cn)申请密钥. 效果图: 核心代码: 复制代码 代码如下: public class MainActivity extends Activity {         private ImageView iv;         private String appKey="";                //填写你的Applicatio

  • Android使用CircleImageView实现圆形头像的方法

    有时我们在应用中会用到圆形头像,下面是利用CircleImageView实现圆形头像的演示,下面效果和代码,效果如图 实现起来也比较简单,先在项目中建一个circleimageview包用来存放CircleImageView类,待会直接把CircleImageView类复制到包里就可以使用了 然后,再建一个attrs.xml,其代码相当简单,定义了圆形边框宽度和颜色 <?xml version="1.0" encoding="utf-8"?> <r

  • Android一行代码实现圆形头像

    效果图 在开发APP中,经常要实现圆形头像,那么该如何实现呢? 要裁剪吗,要重写draw函数吗?不用,只用一行代码就可以实现 Glide实现圆形图像 Glide.with(mContext) .load(R.drawable.iv_image_header) .error(R.drawable.ic_error_default) .transform(new GlideCircleTransform(mContext)) .into(mImage); 其中load后为载入的图像,error后为出

  • Android实现从本地图库/相机拍照后裁剪图片并设置头像

    玩qq或者是微信的盆友都知道,这些聊天工具里都要设置头像,一般情况下大家的解决办法是从本地图库选择图片或是从相机拍照,然后根据自己的喜爱截取图片.上述过程已经实现好了,最后一步我加上了把截取好的图片在保存到本地的操作,来保存头像.为了大家需要,下面我们小编把完整的代码贴出来供大家参考. 先给大家展示效果图: 代码部分: 布局代码(其实就是两个按钮和一个ImageView来显示头像) <LinearLayout xmlns:android="http://schemas.android.co

随机推荐