Android ImageView设置位图FitXY不';行不通
我已经尝试了很长一段时间来设置位图,使其符合Android ImageView设置位图FitXY不';行不通,android,image,imageview,scale,android-imageview,Android,Image,Imageview,Scale,Android Imageview,我已经尝试了很长一段时间来设置位图,使其符合ImageView的边界 通过scaleType:fitXY它就是不起作用。我的位图图像大小低于ImageView(以100X100像素保存),我希望位图图像适合并延伸到ImageView 下面是一些代码: <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/imageView" android:layout_h
ImageView
的边界
通过scaleType:fitXY它就是不起作用。我的位图图像大小低于ImageView
(以100X100像素保存),我希望位图图像适合并延伸到ImageView
下面是一些代码:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageView"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:src="@drawable/locked_image"
android:scaleType="fitXY"
/>
下面是它的外观:
如您所见,图像视图的高度可以,但我希望其宽度相同。
我无法通过活动访问位图图像,因此请不要要求我调整位图大小。我真的不明白为什么它不能正常工作
编辑:
只是为了让问题更容易理解。计算每个设备的ImageView
高度。对于每个设备,它都是不同的。我希望我的ImageView
的宽度与高度相同。我正在使用双向网格视图
库来显示图像(如果有任何区别)。使用imageView的背景属性而不是src属性。位图将被拉伸
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageView"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@drawable/locked_image"
android:scaleType="fitXY"
/>
使用scaleType
不会像您认为的那样改变ImageView的大小。它所做的只是将位图放大到ImageView的大小(从您的屏幕截图来看,它做得很好)
如果您想创建方形图像视图,您应该尝试查看
另外,我不确定我是否理解你为什么要使用这个库。它是专门设计的,允许您声明行/列的数量,并且它将拉伸项目以适应需要。这似乎与您想要的正好相反,因为您希望使它们成为正方形。正常的GridView可以工作,前提是您覆盖每个项目的onMeasure()
,就像链接的问题/答案中显示的那样。我想知道您使用什么容器小部件来保存ImageView。
例如GridView、ListView等…
如果使用GridView,则尝试按GridView.setNumColumns()设置列的计数
如果仍然不起作用,请尝试通过多种方法之一使用AspectRateLayout类。
这门课很简单。按照以下步骤操作!
1.在res/attrs.xml中添加属性
3.修改列表\u项\u image.xml
将背景与fitxy一起使用,而不是使用src。它将拉伸您的图像:
android:background="@drawable/locked_image"
android:scaleType="fitXY"
我不明白问题出在哪里。“我希望它的宽度相同”是什么意思?和什么一样?你期待一个正方形的图像吗?你的图像是方形的吗?我不明白。如果你的ImageView是100x100,那么使用fitXY会在屏幕上创建一个100x100的图像。实际上,它看起来像是一个100x200大小的ImageView,或者类似的东西。@HalR我期待一个正方形的图像,是的。我希望宽度的大小与高度相同。(更大)。我的imageview的高度是自动缩放的-这是完全好的。但是宽度太小了。我希望它和我的身高一样。我怎么做?甚至在编程上。您的布局将imageview的宽度和高度设置为其父级的宽度和高度。然后fitxy正在该框中拟合图像。如果希望imageView为100 x 100,为什么不将xml高度和Widt参数设置为100dp而不是matchparent?@HalR我从来没有说过我希望imageView为100 x 100。我说我正在100乘100保存位图。计算每个设备的imageview高度。对于每个设备,它都是不同的。我希望imageview的宽度与当前的高度相同。对于精确的100x100 imageview,您应该使用android:layout\u width=“100dp”和android:layout\u height=“100dp”。谢谢根据OP的评论:“我从来没有说过我希望imageview是100乘100。我说我正在100乘100保存位图。imageview高度是为每个设备计算的。每个设备的高度不同。”你应该使用“包裹内容”,而不是使用“匹配父项”。然后imageView的大小将根据您的位图,并且必须使用background属性而不是src。我的目标是制作与原始gallery应用程序相同的gallery。我从他们的架构中了解到,当设备处于纵向模式时,有两行。所有显示的图像都是方形的。请,如果可以的话,看看画廊,更好地了解我在找什么。谢谢:)顺便说一句,我知道一个普通的GridView可以工作,但我正在寻找一个水平的:)啊,水平的。对画廊来说是有意义的。好的,最好的方法是覆盖每个项目的onMeasure
,就像我上面所说的。它很简单,只有几行代码,在过去对我很有效。事实上,我已经尝试过了,但现在我要再尝试一次:)哇,这很有效。我真不敢相信!!!太好了。我只需要编辑代码,使高度相同,而不是宽度。:)你真的很棒。我现在很高兴。我一直在寻找这种解决方案。很好的答案android:scaleType=“fitXY”。请为你的答案添加解释。使用fitXY背景,而不是使用src。它会拉伸你的形象。
<declare-styleable name="AspectRatioFrameLayout">
<attr name="aspectRatio" format="float" />
</declare-styleable>
public class AspectRatioFrameLayout extends FrameLayout {
private static final String TAG = "AspectRatioFrameLayout";
private static final boolean DEBUG = false;
private float mAspectRatio; // width/height ratio
public AspectRatioFrameLayout(Context context) {
super(context);
}
public AspectRatioFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initAttributes(context, attrs);
}
public AspectRatioFrameLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initAttributes(context, attrs);
}
private void initAttributes(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AspectRatioFrameLayout);
mAspectRatio = typedArray.getFloat(R.styleable.AspectRatioFrameLayout_aspectRatio, 1.0f);
typedArray.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (DEBUG) Log.d(TAG, "widthMode:"+widthMode+", heightMode:"+heightMode);
if (DEBUG) Log.d(TAG, "widthSize:"+widthSize+", heightSize:"+heightSize);
if ( widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY ) {
// do nothing
} else if ( widthMode == MeasureSpec.EXACTLY ) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec((int)(widthSize / mAspectRatio), MeasureSpec.EXACTLY);
} else if ( heightMode == MeasureSpec.EXACTLY ) {
widthMeasureSpec = MeasureSpec.makeMeasureSpec((int)(heightSize * mAspectRatio), MeasureSpec.EXACTLY);
} else {
// do nothing
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
<your.package.AspectRatioFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:aspectRatio="1">
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageView"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:src="@drawable/locked_image"
android:scaleType="fitXY"
/>
</your.package.AspectRatioFrameLayout>
android:background="@drawable/locked_image"
android:scaleType="fitXY"