如何将图像宽度设置为imageView android
我怎样才能做到 基于图像,如何将图像设置为imageview,并且背景颜色为灰色 这是我尝试过的代码如何将图像宽度设置为imageView android,android,android-layout,imageview,Android,Android Layout,Imageview,我怎样才能做到 基于图像,如何将图像设置为imageview,并且背景颜色为灰色 这是我尝试过的代码 <LinearLayout android:layout_width="match_parent" android:layout_height="210dp" android:layout_gravity="center" android:background=&q
<LinearLayout
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_gravity="center"
android:background="@color/colorAshLight"
android:padding="11dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:src="@drawable/dunkincoffee"
/>
</LinearLayout>
但是我得到的结果是:[![在这里输入图像描述][3][3]
[![在此处输入图像描述][4][4]请尝试此操作。
你需要设置
android:scaleType=“centerCrop”
到ImageView
更新您的代码如下
<LinearLayout
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_gravity="center"
android:background="@color/colorAshLight"
android:padding="11dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:src="@drawable/dunkincoffee"
/>
</LinearLayout>
使用此类,将自动调整imageView的边界及其图像宽度: 1。在项目中添加此类:
package com.example.myapplication;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class ManageHeightByWidthImageView extends ImageView {
public ManageHeightByWidthImageView(Context context) {
super(context);
}
public ManageHeightByWidthImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ManageHeightByWidthImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable d = getDrawable();
if (d != null) {
int w = MeasureSpec.getSize(widthMeasureSpec);
int h = w * d.getIntrinsicHeight() / d.getIntrinsicWidth();
try {
setMeasuredDimension(w, h);
} catch (NullPointerException ignored) {
}
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="@color/colorAshLight"
android:padding="10dp">
<!--
Set the width value you want in place of "match_parent", for this linearLayout
-->
<com.example.myapplication.ManageHeightByWidthImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/dunkincoffee" />
</LinearLayout>
<LinearLayout
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/colorAshLight"
android:padding="10dp">
2。对于第一张图片,请使用以下内容:
package com.example.myapplication;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class ManageHeightByWidthImageView extends ImageView {
public ManageHeightByWidthImageView(Context context) {
super(context);
}
public ManageHeightByWidthImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ManageHeightByWidthImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable d = getDrawable();
if (d != null) {
int w = MeasureSpec.getSize(widthMeasureSpec);
int h = w * d.getIntrinsicHeight() / d.getIntrinsicWidth();
try {
setMeasuredDimension(w, h);
} catch (NullPointerException ignored) {
}
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="@color/colorAshLight"
android:padding="10dp">
<!--
Set the width value you want in place of "match_parent", for this linearLayout
-->
<com.example.myapplication.ManageHeightByWidthImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/dunkincoffee" />
</LinearLayout>
<LinearLayout
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/colorAshLight"
android:padding="10dp">
3。对于第二个垂直图像:
package com.example.myapplication;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class ManageHeightByWidthImageView extends ImageView {
public ManageHeightByWidthImageView(Context context) {
super(context);
}
public ManageHeightByWidthImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ManageHeightByWidthImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable d = getDrawable();
if (d != null) {
int w = MeasureSpec.getSize(widthMeasureSpec);
int h = w * d.getIntrinsicHeight() / d.getIntrinsicWidth();
try {
setMeasuredDimension(w, h);
} catch (NullPointerException ignored) {
}
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="@color/colorAshLight"
android:padding="10dp">
<!--
Set the width value you want in place of "match_parent", for this linearLayout
-->
<com.example.myapplication.ManageHeightByWidthImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/dunkincoffee" />
</LinearLayout>
<LinearLayout
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/colorAshLight"
android:padding="10dp">
它将如下所示:
无法理解我需要基于上述屏幕实施设计是否需要使用灰色背景的图像部分?@Nehak yes Nehak。在回答中添加了解决方案,希望对你有所帮助。我已经尝试添加了上面的代码,但是没有相等的背景空间,下一张图片也不适合上面的代码requiremet@AliAhmed我已经更新了代码和结果图像