Android的:如何拉伸一个图像屏幕的宽度,同时保持纵横比? [英] Android: How to stretch an image to the screen width while maintaining aspect ratio?

查看:1133
本文介绍了Android的:如何拉伸一个图像屏幕的宽度,同时保持纵横比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下载的图像(未知大小的,但它始终是大致正方形),并显示它,以便它水平地充满屏幕,并且将垂直拉长,以保持所述图像的纵横比,任何屏幕大小上。这里是我的(非工作)code。它横跨所述图像水平,而不是垂直方向,因此被压扁...

  ImageView的mainImageView =新ImageView的(上下文);
    mainImageView.setImageBitmap(mainImage); //从服务器下载
    mainImageView.setScaleType(ScaleType.FIT_XY);
    //mainImageView.setAdjustViewBounds(true);
    //这一行启用,只是缩放图像下移
    addView(mainImageView,新LinearLayout.LayoutParams(
            LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
 

解决方案

我做到了这一点与一个自定义视图。设置layout_width =FILL_PARENT和layout_height =WRAP_CONTENT,并将其指向相应的绘制:

 公共类横幅扩展视图{

  私人最终绘制对象标识;

  公共横幅(上下文的背景下){
    超(上下文);
    。标志= context.getResources()getDrawable(R.drawable.banner);
    setBackgroundDrawable(标识);
  }

  公共横幅(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
    。标志= context.getResources()getDrawable(R.drawable.banner);
    setBackgroundDrawable(标识);
  }

  公共横幅(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
    超(背景下,ATTRS,defStyle);
    。标志= context.getResources()getDrawable(R.drawable.banner);
    setBackgroundDrawable(标识);
  }

  @覆盖保护无效onMeasure(INT widthMeasureSpec,
      INT heightMeasureSpec){
    INT宽度= MeasureSpec.getSize(widthMeasureSpec);
    INT高=宽* logo.getIntrinsicHeight()/ logo.getIntrinsicWidth();
    setMeasuredDimension(宽度,高度);
  }
}
 

I want to download an image (of unknown size, but which is always roughly square) and display it so that it fills the screen horizontally, and stretches vertically to maintain the aspect ratio of the image, on any screen size. Here is my (non-working) code. It stretches the image horizontally, but not vertically, so it is squashed...

ImageView mainImageView = new ImageView(context);
    mainImageView.setImageBitmap(mainImage); //downloaded from server
    mainImageView.setScaleType(ScaleType.FIT_XY);
    //mainImageView.setAdjustViewBounds(true); 
    //with this line enabled, just scales image down
    addView(mainImageView,new LinearLayout.LayoutParams( 
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

解决方案

I accomplished this with a custom view. Set layout_width="fill_parent" and layout_height="wrap_content", and point it to the appropriate drawable:

public class Banner extends View {

  private final Drawable logo;

  public Banner(Context context) {
    super(context);
    logo = context.getResources().getDrawable(R.drawable.banner);
    setBackgroundDrawable(logo);
  }

  public Banner(Context context, AttributeSet attrs) {
    super(context, attrs);
    logo = context.getResources().getDrawable(R.drawable.banner);
    setBackgroundDrawable(logo);
  }

  public Banner(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    logo = context.getResources().getDrawable(R.drawable.banner);
    setBackgroundDrawable(logo);
  }

  @Override protected void onMeasure(int widthMeasureSpec,
      int heightMeasureSpec) {
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = width * logo.getIntrinsicHeight() / logo.getIntrinsicWidth();
    setMeasuredDimension(width, height);
  }
}

这篇关于Android的:如何拉伸一个图像屏幕的宽度,同时保持纵横比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆