《Android自定义控件》讨论组圆形头像堆叠、叠加控件DiscussionAvatarView,显示多个参与讨论的人的头像

转载请标明出处:https://blog.csdn.net/m0_38074457/article/details/86589825

一、效果图

效果图

 二、实现步骤

三、代码实现

1、attrs.xml中添加自定义控件的属性

  <declare-styleable name="DiscussionAvatarView">
        <!--头像的半径,dp为单位-->
        <attr name="radius" format="reference|integer" />
        <!--头像间的距离,为头像直径的长度的百分比,dp为单位-->
        <attr name="space" format="reference|float" />
        <!--最多显示多少个头像-->
        <attr name="maxCount" format="reference|integer" />
        <!--是否最后一个显示完全,默认是true-->
        <attr name="isLastComplete" format="reference|boolean" />
        <!--是否显示动画效果-->
        <attr name="isShowAnimation" format="reference|boolean" />
    </declare-styleable>

2、控件代码

1、构造方法中初始化

   private void initView(Context context, AttributeSet attrs) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.DiscussionAvatarView);
        if (array != null) {
            int radius = array.getInteger(R.styleable.DiscussionAvatarView_radius, 13);
            mSpace = array.getFloat(R.styleable.DiscussionAvatarView_space, (float) 0.5);
            mMaxCount = array.getInteger(R.styleable.DiscussionAvatarView_maxCount, 6);
            isLastComplete = array.getBoolean(R.styleable.DiscussionAvatarView_isLastComplete, true);
            isShowAnimation = array.getBoolean(R.styleable.DiscussionAvatarView_isShowAnimation, true);

            mRadius = DensityUtil.dip2px(context, radius);

            array.recycle();
        }
    }

2、onMeasure中设置控件的宽高

 @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int heiMeasure = MeasureSpec.getSize(heightMeasureSpec);
        int heiMode = MeasureSpec.getMode(heightMeasureSpec);
        int widMode = MeasureSpec.getMode(widthMeasureSpec);
        int widMeasure = MeasureSpec.getSize(heightMeasureSpec);

        int wid = 0;
        int hei = 0;
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            ViewGroup.LayoutParams lp = child.getLayoutParams();
            lp.width = 2 * mRadius;
            lp.height = lp.width;
            child.setLayoutParams(lp);
            // 测量子View的宽和高,系统提供的measureChild
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            // 子View占据的宽度
            int childWidth = child.getMeasuredWidth();
            // 子View占据的高度
            int childHeight = child.getMeasuredHeight();

            if (i < mMaxCount) {
                if (i == 0) {
                    wid = wid + childWidth;
                } else {
                    wid = (int) (wid + childWidth * mSpace);
                }
            }
            hei = Math.max(hei, childHeight);
        }
        //如果是exactly使用测量宽和高,否则使用自己设置的宽和高
        setMeasuredDimension((widMode == MeasureSpec.EXACTLY) ? widMeasure : wid,
                (heiMode == MeasureSpec.EXACTLY) ? heiMeasure : hei);
    }

3、onLayout对头像排版

@Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count = getChildCount();

        int left = -mCurrentOffset;
        int top = 0;
        int right = -mCurrentOffset;
        for (int i = 0; i < count; i++) {
            View child;
            if (isLastComplete) {
                child = getChildAt(i);
            } else {
                child = getChildAt(count - i - 1);
            }
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();

            if (i == 0) {
                right = right + childWidth;
            } else {
                right = (int) (right + childWidth * mSpace);
            }
            child.layout(left, top, right, childHeight);
            left = (int) (left + childWidth * mSpace);
        }
    }

三、项目中如何引用

步骤1.将JitPack存储库添加到构建文件中

项目的根build.gradle中添加以下代码:

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

步骤2.build.gradle添加依赖项

dependencies {
    implementation 'com.github.hnsycsxhzcsh:DiscussionAvatarView:v1.3'
}

 步骤3. 布局中引用控件

    <com.discussionavatarview.DiscussionAvatarView
        android:id="@+id/daview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        app:isLastComplete="true"
        app:isShowAnimation="true"
        app:maxCount="6"
        app:radius="30"
        app:space="0.5">
    </com.discussionavatarview.DiscussionAvatarView>

步骤4. initDatas初始化数据(也可以直接addData),新增头像使用addData方法,DiscussionAvatarListener(可以不加)为动画效果的监听

备注:

可以在github上下载我的项目:https://github.com/hnsycsxhzcsh/DiscussionAvatarView,如果我的博客对你有帮助的话,欢迎博客点赞支持,并在github右上角star支持!

 

 

 

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Android定义件组合是指通过将现有的多个件组合起来,形成一个新的自定义件,以实现特定的功能或界面效果。通过组合现有的件,我们可以更灵活地满足项目需求,并减少重复编写代码的工作量。 在Android中,我们可以使用布局文件XML来定义定义件的组合。首先,我们需要创建一个新的布局文件,其中包含多个现有的件。然后,我们可以通过在Java代码中引用这个布局文件,并对其中的件进行操作和设置属性。 以下是一个简单的示例,演示如何创建一个自定义件组合: 1. 创建一个新的布局文件,例如"custom_view.xml",在该文件中添加需要组合的多个件,如TextView、Button等。例如: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me" /> </LinearLayout> ``` 2. 在Java代码中引用该布局文件,并进行相应的操作。例如,在一个Activity中,我们可以通过setContentView方法将该布局文件设置为当前Activity的布局。 ```java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.custom_view); // 对自定义件进行操作 TextView textView = findViewById(R.id.textView); Button button = findViewById(R.id.button); // 设置监听器等其他操作... } } ``` 通过上述步骤,我们就可以将多个现有的件组合成一个新的自定义件,实现特定的功能或界面效果。当然,在实际应用中,可能还需要对组合件进行进一步的自定义和功能扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值