卡片横向层叠android,Android实现3D层叠式卡片图片展示

本文实例为大家分享了Android实现3D层叠式卡片图片展示的具体代码,供大家参考,具体内容如下

先看效果

83e8feb04c1623292583846480586dd6.gif

好了效果看了,感兴趣的往下看哦!

整体实现思路

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"<

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 HorizontalScrollView 和 LinearLayout 实现横向滚动图片的效果。具体实现步骤如下: 1. 在布局文件中,使用 HorizontalScrollView 包裹 LinearLayout,并设置 LinearLayout 的 orientation 属性为 horizontal,如下所示: ```xml <HorizontalScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/ll_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <!-- 在这里添加图片 --> </LinearLayout> </HorizontalScrollView> ``` 2. 在代码中,获取 LinearLayout 并动态添加图片,如下所示: ```java LinearLayout llHorizontal = findViewById(R.id.ll_horizontal); for (int i = 0; i < imageList.size(); i++) { ImageView imageView = new ImageView(this); imageView.setImageResource(imageList.get(i)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( getResources().getDimensionPixelSize(R.dimen.image_width), getResources().getDimensionPixelSize(R.dimen.image_height)); layoutParams.setMargins(0, 0, getResources().getDimensionPixelSize(R.dimen.image_margin), 0); imageView.setLayoutParams(layoutParams); llHorizontal.addView(imageView); } ``` 其中,imageList 是存放图片资源 ID 的列表,R.dimen.image_width、R.dimen.image_height 和 R.dimen.image_margin 是在 dimens.xml 文件中定义的图片宽度、高度和间距。 这样就可以实现横向滚动图片的效果了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值