android - "ScaleType:FIT_XY"不拉伸(stretch)图库中的图像

标签 android image gallery scale

我有一个横向模式的图库,其中有很多带有图像适配器的图像。我希望图像水平拉伸(stretch)到整个屏幕,并保持纵横比并根据需要垂直拉伸(stretch)..
我尝试使用 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 中执行此操作时,我使用合适的缩放比例(fitCenter、fitStart、fitEnd)和调整 View 边界,例如 xml 属性

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

关于android - "ScaleType:FIT_XY"不拉伸(stretch)图库中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7685265/

相关文章:

android - 在设备上创建 9 补丁图像

Android 平板电脑 - 触摸时隐藏/显示状态栏

html - 将中心位置应用于画廊而不是图片

android - 使用 Gmail 凭据登录

android - 为什么构建过程会忽略proguardFiles getDefaultProguardFile ('proguard-android.txt')

Mac 上的 Python : Image module cannot be installed

image - AWS API Gateway 从 base64 浏览器错误返回二进制文件

android - Android边距开始/结束和右/左有什么区别?

android - 将服务中的数据发送回我的 Activity

jquery - 使用 Jquery 和 Ajax 的动画内容