android - 如何以编程方式实现动态分层 ImageView?
问题描述
我想要分层图像,如下所示。
目前,我正在这样做
布局.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<View
android:id="@+id/view1"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="10dp"
android:background="@drawable/images1">
</View>
<View
android:id="@+id/view2"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:background="@drawable/images1">
</View>
<View
android:id="@+id/view3"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
android:background="@drawable/images1">
</View>
</RelativeLayout>
但我觉得这种使用 XML 的手动方法可能会根据不同的设备和屏幕类型而有所不同和反应。那么,我怎样才能以更好或最好的方式实现它呢?我怎样才能让它充满活力?
谢谢你。
解决方案
我使用以下内容以编程方式实现分层图像视图。但是,这并不是最好的解决方案。希望,它也会对其他人有所帮助。
class ThumbView extends RelativeLayout {
private ImageView vLayer1;
private ImageView vLayer2;
private ImageView vLayer3;
public ThumbView(Context context, String pathToFile) {
super(context);
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.view_thumb, this, true);
vLayer1 = view.findViewById(R.id.view1);
vLayer2 = view.findViewById(R.id.view2);
vLayer3 = view.findViewById(R.id.view3);
Drawable drawable = Drawable.createFromPath(pathToFile);
vLayer1.setImageDrawable(drawable);
vLayer2.setImageDrawable(drawable);
vLayer3.setImageDrawable(drawable);
}
}
推荐阅读
- c# - 将datagridview中的整行设置为ReadOnly = false
- docker - nginx后面的identityserver4在AKS(azure kubernetes)中使用https&letsencrypt阻止mvcclient因某种原因设置cookie
- android - Kotlin corountines :注意:结束时间超过纪元:
- php - 在mysql查询中求和多个日期范围
- angular - 在用户输入角度 2 时更改 ng-select 组件的输入值
- mysql - 检索别名的基础列名
- asp.net-core - 如何在 ASP.NET Core Web API 响应中忽略 ResponseWrapper
- maven - 用于 java spring maven 应用程序的 Gitlab CI/CD
- javascript - fabric js - 如何检查模式是否应用于 svg
- c# - 在视图模型中有条件地更改显示名称