Android 缩放与宽度相关的图像
我有一个主要的线性布局,方向设置为垂直。它包含两个线性布局,第一个布局权重为0.25,第二个布局权重为0.75。第一个布局具有水平方向,并且还有两个对象,其值布局权重=0.5,值布局权重=0.5 以下是完整的代码:Android 缩放与宽度相关的图像,android,Android,我有一个主要的线性布局,方向设置为垂直。它包含两个线性布局,第一个布局权重为0.25,第二个布局权重为0.75。第一个布局具有水平方向,并且还有两个对象,其值布局权重=0.5,值布局权重=0.5 以下是完整的代码: <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- top layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:orientation="horizontal" >
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
app:srcCompat="@drawable/samurai" />
<EditText
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_weight="0.5"
android:background="@android:color/transparent"
android:gravity="bottom|right"
android:inputType="none"
android:maxLines="1"
android:text=""
android:textColor="@color/text"
android:textSize="50sp" />
</LinearLayout>
<!-- bottom layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.75"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
我的问题是如何缩放图像以匹配宽度,但如何自动缩放高度
要根据预期结果裁剪图像,您可能必须为imageview使用自定义矩阵。我认为这是TOP_CROP最简单的实现,您可以根据自己的需要进行修改:
要根据预期结果裁剪图像,您可能必须为imageview使用自定义矩阵。我认为这是TOP_CROP最简单的实现,您可以根据自己的需要进行修改:
问题是在某些布局宽度和布局高度中添加0。更改为0dp,它应该可以工作。 在这种情况下,您应该使用ConstraintLayout,因为它可以减少嵌套布局,并肯定会提高布局性能 使用ConstraintLayout时,代码将是
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@drawable/samurai"
app:layout_constraintHeight_percent="0.25"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.5"/>
<EditText
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_percent="0.25"
app:layout_constraintStart_toEndOf="@id/img"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.5"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/img"/>
</android.support.constraint.ConstraintLayout>
问题是在某些布局宽度和布局高度中添加了0。更改为0dp,它应该可以工作。 在这种情况下,您应该使用ConstraintLayout,因为它可以减少嵌套布局,并肯定会提高布局性能 使用ConstraintLayout时,代码将是
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@drawable/samurai"
app:layout_constraintHeight_percent="0.25"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.5"/>
<EditText
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_percent="0.25"
app:layout_constraintStart_toEndOf="@id/img"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.5"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/img"/>
</android.support.constraint.ConstraintLayout>
我知道这种方法,但是有更好的方法吗?我认为有些库可以提供类似这样的功能:。我自己没有使用过它,所以我不确定。我知道这种方法,但是有更好的方法吗?我想有一些库可以提供这样的功能:。我自己没有用过,所以我不确定。