【问题标题】:fit an image into the screen with out being distorted in android将图像放入屏幕而不会在 android 中失真
【发布时间】:2012-11-28 19:00:32
【问题描述】:

我想将900x150 的图像放入1280x800 的屏幕中而不会失真..任何帮助都将是 gr8 ..这是我的main.xml 文件

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="centerCrop"
    android:contentDescription="@string/mypicture"
    android:src="@drawable/MyImage"
        />
    </RelativeLayout>
   </ScrollView>
 </LinearLayout>

【问题讨论】:

标签: android android-layout android-imageview


【解决方案1】:

您可能应该使用fitCenter 而不是centerCrop

【讨论】:

  • 我试过900尺寸都完美贴合屏幕但不是150尺寸
  • 你想要什么结果?填充屏幕的两个维度(这会扭曲图像比例)或保持图像比例并仅填充一个维度?
  • ..如果可能的话,我想保持图像比例并适应两个尺寸
  • 这不可能...屏幕比例和图像比例不同...(屏幕比例= 1280/800 = 1.6和图像比例= 800 / 150 = 6)...
  • @xiongdi 你的意思是你想保持比例但填满屏幕,所以在这种情况下,你有垂直滚动?
【解决方案2】:

您可以根据纵横比放大和缩小图像。或者我看到您使用滚动视图简单地为图像大小包装内容,这将完全显示图片而不会失真......

编辑

private void scaleImage()
{
    // Get the ImageView and its bitmap
    ImageView view = (ImageView) findViewById(R.id.image_box);
    Drawable drawing = view.getDrawable();
    if (drawing == null) {
        return; // Checking for null & return, as suggested in comments
    }
    Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();

    // Get current dimensions AND the desired bounding box
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int bounding = dpToPx(250);
    Log.i("Test", "original width = " + Integer.toString(width));
    Log.i("Test", "original height = " + Integer.toString(height));
    Log.i("Test", "bounding = " + Integer.toString(bounding));

    // 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) bounding) / width;
    float yScale = ((float) bounding) / height;
    float scale = (xScale <= yScale) ? xScale : yScale;
    Log.i("Test", "xScale = " + Float.toString(xScale));
    Log.i("Test", "yScale = " + Float.toString(yScale));
    Log.i("Test", "scale = " + Float.toString(scale));

    // 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);
    width = scaledBitmap.getWidth(); // re-use
    height = scaledBitmap.getHeight(); // re-use
    BitmapDrawable result = new BitmapDrawable(scaledBitmap);
    Log.i("Test", "scaled width = " + Integer.toString(width));
    Log.i("Test", "scaled height = " + Integer.toString(height));

    // 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);

    Log.i("Test", "done");
}

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

【讨论】:

  • 如果我删除滚动视图,图像会失真..我想保持纵横比,但不知道如何
  • 谢谢你们 ..@Awais Tariq 让我试试 ;)
  • 我的图像视图已缩放,但现在它太小了,而不是适合整个屏幕,我从 main.xml 中删除了滚动视图 ..关于边界的任何想法
  • 它是填充父 ..schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > 。 .缩放后的图像为 250x42
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-03
  • 2012-06-17
  • 2011-11-16
  • 2011-12-24
  • 1970-01-01
相关资源
最近更新 更多