Pet*_*ter 51 android-layout android-imageview
我需要在屏幕顶部并排显示三个相同尺寸的图像(200 X 100)(无间隙).它们应占据屏幕的整个宽度并保持纵横比.是否可以仅使用布局xml文件来实现,或者我需要使用java代码?
解决方案应该是独立的解决方案......任何人都可以针对此(或类似)问题发布解决方案或链接吗?谢谢!
Pat*_*oos 143
搞定了!但正如我上面所说,你需要创建自己的类.但它很小.我在这篇文章中借助Bob Lee的答案创建了它:Android:如何在保持纵横比的同时将图像拉伸到屏幕宽度?
package com.yourpackage.widgets;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
public class AspectRatioImageView extends ImageView {
public AspectRatioImageView(Context context) {
super(context);
}
public AspectRatioImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AspectRatioImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth();
setMeasuredDimension(width, height);
}
}
Run Code Online (Sandbox Code Playgroud)
现在在XML中使用它:
<com.yourpackage.widgets.AspectRatioImageView
android:id="@+id/image"
android:src="@drawable/yourdrawable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true" />
Run Code Online (Sandbox Code Playgroud)
玩得开心!
=====================================
通过使用android:adjustViewBounds ="true"找到另一种只在XML中执行相同操作的方法.这是一个例子:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:src="@drawable/image1" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:src="@drawable/image2" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:src="@drawable/image2" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
mdi*_*imo 30
只有一个小的升级,以考虑可能出错的地方:null Drawable或0宽度.
package com.yourpackage.yourapp;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class AspectRatioImageView extends ImageView {
public AspectRatioImageView(Context context)
{
super(context);
}
public AspectRatioImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public AspectRatioImageView(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
Drawable drawable = getDrawable();
if (drawable != null)
{
int width = MeasureSpec.getSize(widthMeasureSpec);
int diw = drawable.getIntrinsicWidth();
if (diw > 0)
{
int height = width * drawable.getIntrinsicHeight() / diw;
setMeasuredDimension(width, height);
}
else
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
else
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
Run Code Online (Sandbox Code Playgroud)
Jake Wharton改进了AspectRatioImageView类,您可以通过xml设置dominantMeasurement和aspectRatio.您可以在下面的链接中找到它.
https://gist.github.com/JakeWharton/2856179
我不了解 XML 布局和 android API,但数学很简单;找到屏幕的宽度并除以三。这是每个图像的宽度。现在将宽度乘以原始图像的高宽比。这是每个图像的高度。
int imageWidth = screenWidth / 3;
float ratio = originalImage.Height / (float)originalImage.Width;
int imageHeight = (int)(imageWidth * ratio);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21188 次 |
| 最近记录: |