android - 缩放图像以完全适合屏幕

标签 android scale

我正在使用 Horizo​​ntalScrollView,我想将图像放入其中,但图像会比屏幕大,并且在我使用 setScaleType(ImageView.ScaleType .CENTER_CROP) 或其他属性。 如何让图像完全适合屏幕?如果可能,我更喜欢使用 XML 属性。

activity_main.xml

<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
    android:id="@+id/main"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="horizontal">

</LinearLayout>

MainActivity.java

ImageView image = new ImageView(this);
image.setImageResource(getResources().getIdentifier('file_name', "drawable", getPackageName()));
image.setAdjustViewBounds(true);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
image.setLayoutParams(layoutParams);
parent.addView(image);

最佳答案

您可以在 ImageView 中使用此 xml 属性,

android:scaleType="fitXY"

或者使用这个动态代码 fragment 来适应 ImageView 相应的屏幕比例,

private void scaleImage(ImageView view, int boundBoxInDp)
{
    // Get the ImageView and its bitmap
    Drawable drawing = view.getDrawable();
    Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();

    // Get current dimensions
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    // Determine how much to scale: the dimension requiring less scaling is
    // closer to the its side. This way the image always stays inside your
    // bounding box AND either x/y axis touches it.
    float xScale = ((float) boundBoxInDp) / width;
    float yScale = ((float) boundBoxInDp) / height;
    float scale = (xScale <= yScale) ? xScale : yScale;

    // Create a matrix for the scaling and add the scaling data
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);

    // Create a new bitmap and convert it to a format understood by the ImageView
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    BitmapDrawable result = new BitmapDrawable(scaledBitmap);
    width = scaledBitmap.getWidth();
    height = scaledBitmap.getHeight();

    // Apply the scaled bitmap
    view.setImageDrawable(result);

    // Now change ImageView's dimensions to match the scaled image
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
    params.width = width;
    params.height = height;
    view.setLayoutParams(params);
}

private int dpToPx(int dp)
{
    float density = getApplicationContext().getResources().getDisplayMetrics().density;
    return Math.round((float)dp * density);
}

以及用于测试的示例代码:

ImageView image = new ImageView(this);
scaleImage(image , 250); // in dp

这是输出,

之前,

enter image description here

之后,

enter image description here

来源:Scale image into ImageView, then resize ImageView to match the image

关于android - 缩放图像以完全适合屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30639990/

相关文章:

html - CSS 调整图像大小

javascript - 在 jQuery 中更改图像的尺寸

service - 在使用Pod之前是否有kubernetes配置参数(服务,rc或其他)要延迟

Android OpenGL ES 纹理动画

android - 如何在 Android 中隐藏视频 View 的 Seekbar 或 Progressbar

iphone - iPhone 应用程序的 iPad 内容比例因子 x2

css - 相同高度图像和文本

java - 如何从android中的联系人读取电子邮件字段?

android - 如何创建类似 gmail 的收件人(用户)自定义 View

android - 如何确定具有特定 token 的设备是否与 firebase android 在线?