本文实例为大家分享了Android实现3D层叠式卡片图片展示的具体代码,供大家参考,具体内容如下
先看效果
好了效果看了,感兴趣的往下看哦!
整体实现思路
1、重写RelativeLayout 实现 锁定宽高比例的 RelativeLayout
2、自定义一个支持滑动的面板 继承 ViewGroup
3、卡片View绘制
4、页面中使用布局
首先为了更好的展示图片我们重写一下 RelativeLayout 编写一个锁定宽高比例的 RelativeLayout
AutoScaleRelativeLayout
public class AutoScaleRelativeLayout extends RelativeLayout {
//宽高比例
private float widthHeightRate = 0.35f;
public AutoScaleRelativeLayout(Context context) {
this(context, null);
}
public AutoScaleRelativeLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AutoScaleRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//通过布局获取宽高比例
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.card, 0, 0);
widthHeightRate = a.getFloat(R.styleable.card_widthHeightRate, widthHeightRate);
a.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 调整高度
int width = getMeasuredWidth();
int height = (int) (width * widthHeightRate);
ViewGroup.LayoutParams lp = getLayoutParams();
lp.height = height;
setLayoutParams(lp);
}
}
这样我们就编写好了我们想要的父布局
使用方法
android:id="@+id/card_top_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card:widthHeightRate="0.6588">
android:id="@+id/card_image_view"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
android:id="@+id/maskView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackground"<