自定义LinerLayout实现IPhone动态照片“实况”视图封面帧堆叠效果

package com.hfengxiang.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

public class OverlappingLinearLayout extends LinearLayout {

    public OverlappingLinearLayout(Context context) {
        super(context);
    }

    public OverlappingLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public OverlappingLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 测量子视图
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        int totalWidth = 0;
        int totalHeight = 0;
        int maxWidth = 0;
        int maxHeight = 0;

        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            totalWidth += child.getMeasuredWidth();
            totalHeight += child.getMeasuredHeight();
            maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
            maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
        }

        if (getOrientation() == HORIZONTAL) {
            int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
            setMeasuredDimension(Math.min(totalWidth, parentWidth), maxHeight);
        } else { // VERTICAL
            int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
            setMeasuredDimension(maxWidth, Math.min(totalHeight, parentHeight));
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int left = 0;
        int top = 0;
        int totalWidth = 0;
        int totalHeight = 0;
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();

        if (getOrientation() == HORIZONTAL) {
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                totalWidth += child.getMeasuredWidth();
            }
            int overlaps = totalWidth - measuredWidth;

            // 布局子View(横向)
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                int childWidth = child.getMeasuredWidth();
                int childHeight = child.getMeasuredHeight();
                int subOverlaps = Math.max(0, overlaps / (getChildCount() - 1));
                child.layout(left, top, left + childWidth, top + childHeight);
                left += (childWidth - subOverlaps); // 下一个子View的左边位置
            }
        } else { // VERTICAL
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                totalHeight += child.getMeasuredHeight();
            }
            int overlaps = totalHeight - measuredHeight;

            // 布局子View(纵向)
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                int childWidth = child.getMeasuredWidth();
                int childHeight = child.getMeasuredHeight();
                int subOverlaps = Math.max(0, overlaps / (getChildCount() - 1));
                child.layout(left, top, left + childWidth, top + childHeight);
                top += (childHeight - subOverlaps); // 下一个子View的顶部位置
            }
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://sch
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值