根据另一个 ImageView 的属性缩放 ImageView
Scale ImageView based on properties of another ImageView
我正在尝试将一个 Imageview 放置在另一个 Imageview 之上,如下所示:
这是我用来实现此目的的布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<ImageView
android:id="@+id/list_item_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_gravity="center" />
<ImageView
android:id="@+id/list_item_logo"
android:layout_width="40dp"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:layout_gravity="top|start" />
</FrameLayout>
这样做是为了将徽标放在图像的顶部,但这里的问题是我还应该根据图像的属性缩放徽标(原始大小与图像大致相同) .更具体地说,我想在图像宽度的四分之一处渲染徽标,纵横比为 1:1,并添加图像宽度的 10% 填充。
目前,我只是在调整徽标的 layoutwidth、layoutheight、paddingLeft 和 paddingTop 的值,直到获得所需的结果。有没有一种方法可以根据图像的属性动态更改徽标的属性?
非常感谢任何帮助。
考虑使用 PercentRelativeLayout
来实现您想要的布局,其中您的徽标具有 10% 的顶部和开始边距,以及 10% 的宽度(或您需要的任何宽度百分比)。请注意,SquareImageView
是目前在包 com.example
中的自定义视图,它将忽略任何给定的高度值,并将重置为计算出的宽度(即图像宽度的 10%)。
示例XML:
<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.support.percent.PercentRelativeLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000" />
<com.example.SquareImageView
android:id="@+id/logo"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#0000ff"
app:layout_heightPercent="10%"
app:layout_marginStartPercent="10%"
app:layout_marginTopPercent="10%"
app:layout_widthPercent="10%" />
</android.support.percent.PercentRelativeLayout>
</RelativeLayout>
SquareImageView.java :
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); // Snap to width
}
}
我正在尝试将一个 Imageview 放置在另一个 Imageview 之上,如下所示:
这是我用来实现此目的的布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<ImageView
android:id="@+id/list_item_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_gravity="center" />
<ImageView
android:id="@+id/list_item_logo"
android:layout_width="40dp"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:layout_gravity="top|start" />
</FrameLayout>
这样做是为了将徽标放在图像的顶部,但这里的问题是我还应该根据图像的属性缩放徽标(原始大小与图像大致相同) .更具体地说,我想在图像宽度的四分之一处渲染徽标,纵横比为 1:1,并添加图像宽度的 10% 填充。
目前,我只是在调整徽标的 layoutwidth、layoutheight、paddingLeft 和 paddingTop 的值,直到获得所需的结果。有没有一种方法可以根据图像的属性动态更改徽标的属性?
非常感谢任何帮助。
考虑使用 PercentRelativeLayout
来实现您想要的布局,其中您的徽标具有 10% 的顶部和开始边距,以及 10% 的宽度(或您需要的任何宽度百分比)。请注意,SquareImageView
是目前在包 com.example
中的自定义视图,它将忽略任何给定的高度值,并将重置为计算出的宽度(即图像宽度的 10%)。
示例XML:
<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.support.percent.PercentRelativeLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000" />
<com.example.SquareImageView
android:id="@+id/logo"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#0000ff"
app:layout_heightPercent="10%"
app:layout_marginStartPercent="10%"
app:layout_marginTopPercent="10%"
app:layout_widthPercent="10%" />
</android.support.percent.PercentRelativeLayout>
</RelativeLayout>
SquareImageView.java :
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); // Snap to width
}
}