简体   繁体   English

ImageView缩放不起作用

[英]ImageView scaling doesn't work

I googled this question but didn't fond answer for me. 我用Google搜索了这个问题,但并不喜欢我的回答。

This is my ImageView : 这是我的ImageView 在此输入图像描述

And this this ImageView in xml: 这个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" />

How I can scale this image like on example image? 我如何像示例图像一样缩放此图像?

PS This image I download from internet. PS这个图像我从互联网上下载。

UPD 1. ImageLoader code added. UPD 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);

Sorry, but you cannot make it bigger as you use this: 对不起,但是你使用它时不能把它变大:

  android:layout_width="match_parent"

It already means "take how much width as you can". 它已经意味着“尽可能多的宽度”。 As your width has the maximum value, your image height also takes a maximum value. 由于宽度具有最大值,因此图像高度也会占用最大值。

You can check it by using: 您可以使用以下方法进行检查:

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

It should make it bigger three times. 它应该使它变大三倍。 If it won't work use: 如果不起作用,请使用:

android:scaleX="0.2"
android:scaleY="0.2"

It should make it smaller five times. 它应该缩小五倍。 I'm pretty sure that it would work 我很确定它会起作用

Hope it help 希望它有所帮助

Set LayoutParams from code. 从代码中设置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);

It's a known issue with ImageView, which won't upscale small images. 这是ImageView的一个已知问题,它不会放大小图像。

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

You can set Bitmap as background. 您可以将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));
    } 
}); 

Reference Link 参考链接

Try using ScaleType attribute for your ImageView in your xml. 尝试在xml中为ImageView使用ScaleType属性。 For example 例如

android:scaleType="centerCrop".

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM