Sha*_*arj 64 android android-layout android-imageview
我下载图像并使用动态设置为屏幕背景Imageview
.我试过ScaleType
,缩放图像.
如果图像高度大于宽度那么ScaleTypes
fitStart
,fitEnd
并且fitCenter
不起作用.Android缩小照片并根据高度调整它,但我看到一些额外的空白空间作为宽度的一部分.
我想根据宽度缩小照片,使其适合宽度,我不在乎是否有一些额外的空白空间作为高度的一部分,或者如果高度太长,如果它超出视图就没关系(如果可能的话?).
ScaleType.XY
缩放照片并适应所有内容ImageView
并且不关心图像高度/重量比.
<ImageView
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitStart"
/>
Run Code Online (Sandbox Code Playgroud)
Sha*_*arj 180
我最终使用了这段代码:
<ImageView
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/name"
/>
Run Code Online (Sandbox Code Playgroud)
确保使用src
而不是使用设置图像background
.
Mic*_*ert 12
该发现优雅的解决方案在这里会像为你的魅力.
基本上你只需要创建一个扩展的小类,ImageView
并简单地覆盖onMeasure
方法来根据需要调整宽度和高度.在这里使用以下方法缩放以适合宽度:
@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)
你会ImageView
像这样使用这个特殊的:
<your.activity.package.AspectRatioImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:src="@drawable/test" />
Run Code Online (Sandbox Code Playgroud)
如果你只是想填充屏幕的宽度并且高度成比例(并且知道图像的比例),你可以在OnCreate()中执行此操作,这是一种简单的方法:
setContentView(R.layout.truppview_activity);
trupImage = (ImageView) findViewById(R.id.trupImage);
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float scWidth = outMetrics.widthPixels;
trupImage.getLayoutParams().width = (int) scWidth;
trupImage.getLayoutParams().height = (int) (scWidth * 0.6f);
Run Code Online (Sandbox Code Playgroud)
其中0.6是比率调整(如果你知道图像的w&h,你当然也可以自动计算).
PS:
这是XML方面:
<ImageView
android:id="@+id/trupImage"
android:layout_width="match_parent"
android:background="@color/orange"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
Run Code Online (Sandbox Code Playgroud)
小智 5
这对我有用。
创建一个扩展ImageView的类
package com.yourdomain.utils;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class ResizableImageView extends ImageView {
public ResizableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable d = getDrawable();
if (d != null) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
setMeasuredDimension(width, height);
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在xml中使用以下内容代替ImageView
<com.yourdomain.utils.ResizableImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/test" />
Run Code Online (Sandbox Code Playgroud)
如果你想适合整个父块
的图像,它会拉伸图像
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
Run Code Online (Sandbox Code Playgroud)
如果你想用图像的原始比例来适应整个父级,
它会裁剪出图像的一部分
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
84517 次 |
最近记录: |