如何在运行时设置ImageView的最大高度

时间:2016-08-08 09:45:39

标签: android android-imageview

如何在运行时设置ImageView的最大高度?

例如,我有一个image url,其宽度大于屏幕宽度。然后我想设置ImageView height as screen's width,使ImageView成为方形。

我无法找到完美的解决方案。任何帮助将深深感激。

2 个答案:

答案 0 :(得分:1)

对于图像的大小并不重要,下面的代码将按照完美的比例进行调整,如果您不想要最大宽度,则会产生最大宽度,然后从代码中修改尺寸。你需要毕加索。

   private Dimensions getScreenDimensions()
{
    DisplayMetrics metrics = new DisplayMetrics();
    WindowManager wm = (WindowManager)    context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    display.getMetrics(metrics);
    final int widthScreen = metrics.widthPixels;
    int heightScreen = metrics.heightPixels;
    Dimensions screenDimen = new Dimensions(metrics.widthPixels,metrics.heightPixels); // custom class
    return screenDimen;
}

    private Bitmap makeCalc(Dimensions screenDimen, Dimensions imageDimen,Bitmap bmp)
{
    float widthRatio = screenDimen.getWidth() / imageDimen.getWidth();
    float scaledHeight = imageDimen.getHeight() * widthRatio;
    imageBmp = Bitmap.createScaledBitmap(bmp, (int)screenDimen.getWidth(), (int) scaledHeight, true);

    //image.setImageBitmap(resized);
    return imageBmp;
}

public class Dimensions {
float width;
float height;

public Dimensions(float width, float height) {
    this.width = width;
    this.height = height;
}

public float getHeight() {
    return height;
}

public void setHeight(float height) {
    this.height = height;
}

public float getWidth() {
    return width;
}

public void setWidth(float width) {
    this.width = width;
}
   }

    Target target = new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            int width = bitmap.getWidth(); // original width
            int height = bitmap.getHeight(); // original heigh
            Log.w(TAG, "onBitmapLoaded: " + width + " " + height);

            Bitmap newBmp = makeCalc(getScreenDimensions(),new Dimensions(width,height),bitmap);
            imageView.setImageBitmap(newBmp);
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
            Log.w(TAG, "onBitmapFailed: "+errorDrawable );
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            Log.w(TAG, "onPrepareLoad: " );
        }
    };

    Picasso.with(this).load(yourURL).into(target);
    imageView.setTag(target);

答案 1 :(得分:0)

要制作Square ImageView,您可以使用以下自定义类:

public class SquareImageView extends ImageView {

    public SquareImageView(Context context) {
        super(context);
    }

    public SquareImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //noinspection SuspiciousNameCombination
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }
}