【问题标题】:"ScaleType:FIT_XY" doesnt stretch the images in the Gallery"ScaleType:FIT_XY" 不会拉伸图库中的图像
【发布时间】:2011-10-07 09:19:42
【问题描述】:

我有一个横向模式的画廊,里面有很多带有图像适配器的图像。我希望图像水平拉伸到整个屏幕,并保持纵横比并根据需要垂直拉伸..
我尝试使用 FIT_XY,它不适合水平,但它不能保持垂直比例(图像被压碎)
我怎样才能做到这一点?这是自定义图像适配器:

    public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
int counter = 0;

private final Context mContext;

public String[] mImageIds;

public ImageAdapter(Context c) {
    mContext = c;
    TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
    mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
    a.recycle();
}

public void insert(String string) {
    mImageIds[counter] = string;
    counter++;
}

@Override
public int getCount() {
    return mImageIds.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView i = new ImageView(mContext);

    i.setImageBitmap(BitmapFactory.decodeFile(mImageIds[position]));
    i.setLayoutParams(new Gallery.LayoutParams(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT));
    i.setScaleType(ImageView.ScaleType.MATRIX);
    i.setBackgroundResource(mGalleryItemBackground);

    return i;
}

}

【问题讨论】:

    标签: android image gallery scale


    【解决方案1】:

    当我在 android 中执行此操作时,我正在使用适合缩放(fitCenter、fitStart、fitEnd)以及调整视图边界,xml 属性将是例如

    android:scaleType="fitCenter" android:adjustViewBounds="true" 
    

    【讨论】:

    • 现在图像没有拉伸,还有其他想法吗?
    【解决方案2】:

    您可以计算像素的宽度和高度并将它们发送到setLayoutParams

    int width = getWindowManager().getDefaultDisplay().getWidth();
    int height = width * bitmapHeight / bitmapWidth;
    imageView.setLayoutParams(new Gallery.LayoutParams(width, height));
    imageView.setPadding(6, 0, 6, 0); // set this to what you like
    // remove this line:
    //imageView.setScaleType(ImageView.ScaleType.MATRIX);
    

    设置比例类型不会做任何事情(FitXY 会水平拉伸到边界,但将高度保留为其他值,正如您所提到的)。 FILL_PARENT 似乎也没有按预期工作。其他布局似乎会影响位图大小(或者在我的情况下是 Drawable)。

    在您的布局 xml 文件中,您可能需要设置 fill_parent:

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    

    这对我下载和流式传输到 Drawable 很有用,所以我认为它适用于您的位图。

    【讨论】:

      猜你喜欢
      • 2013-07-29
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2014-02-17
      相关资源
      最近更新 更多