ImageView缩放不起作用

4

我在谷歌上搜索了这个问题,但没有找到适合我的答案。

这是我的ImageViewenter image description here

这是xml中的ImageView:

<ImageView
      android:id="@+id/stock_cover"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:adjustViewBounds="true"                        
      android:src="@drawable/place_holder16_9" />

如何像示例图片那样缩放此图片?

附注:我从互联网上下载了此图片。

更新 1. 已添加ImageLoader代码。

DisplayImageOptions options = new DisplayImageOptions.Builder()
            .showImageForEmptyUri(R.drawable.place_holder16_9)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .build();
ImageLoader.getInstance().displayImage(promo.getImageBigUrl(), mCoverImageView, options);

你需要动画吗? - pskink
请查看此链接。http://developer.android.com/reference/android/widget/ImageView.ScaleType.html - Shoeb Siddique
我尝试了centerCrop、fitXY、matrix和其他方法,但这并没有帮助我。 - MyNameIs
@ShoebSiddique 使用ImageLoader - MyNameIs
是的,我知道了。问题就在那里,请展示你的Loader代码。 - Shoeb Siddique
显示剩余3条评论
5个回答

1
抱歉,由于您使用的内容,您不能使其更大。
  android:layout_width="match_parent"

这已经意味着“尽可能地占用宽度”。由于您的宽度具有最大值,因此您的图像高度也将达到最大值。

您可以使用以下方法进行检查:

android:scaleX="3"
android:scaleY="3"

它应该将其放大三倍。如果不起作用,请使用:
android:scaleX="0.2"
android:scaleY="0.2"

它应该能使其变小五倍。我非常确定它会起作用。

希望这有所帮助。


1

从代码中设置LayoutParams

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相关的已知问题,它无法放大小图像。

1

Try this code:

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

1
您可以将Bitmap设置为背景。
// Load image, decode it to Bitmap and return Bitmap to callback 
imageLoader.loadImage(imageUri, new SimpleImageLoadingListener() { 
    @Override 
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        // Do whatever you want with Bitmap 
        mCoverImageView.setBackground(new BitmapDrawable(getResources(), bitmap));
    } 
}); 

参考链接


0

尝试在您的xml中为ImageView使用ScaleType属性。例如

android:scaleType="centerCrop".

你能发布你完整的XML吗? - Mukesh Rana

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接