Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

自定义图片宽高比例实现 #31

Open
hudan2714 opened this issue Sep 23, 2017 · 1 comment
Open

自定义图片宽高比例实现 #31

hudan2714 opened this issue Sep 23, 2017 · 1 comment

Comments

@hudan2714
Copy link

首先还是感谢作者提供的开源项目!因为我的图片需要实现的图片不是正方形,而作者的图片都是正方形的。所以,修改了一下,支持可以使用宽高比例图片。大家如果有同样的需求,可以按照这样的方式处理。。也期待作者下个版本上能支持吧!

直接看代码修改吧
` private int[] mGridSize = new int[2]; // 宫格大小,即图片大小,表示宽高值
private float mRatio = 1.0f; //图片宽高比例,默认1:1.为正方形

@OverRide
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
......
if (mImgDataList.size() == 1 && mSingleImgSize != -1) {
mGridSize[0] = mSingleImgSize > totalWidth ? totalWidth : mSingleImgSize;
mGridSize[1] = calculateRatio(mGridSize[0]);
} else {
mImageViewList.get(0).setScaleType(ImageView.ScaleType.CENTER_CROP);
mGridSize[0] = (totalWidth - mGap * (mColumnCount - 1)) / mColumnCount;
mGridSize[1] = calculateRatio(mGridSize[0]);
}
.......
}

/**
根据比例,通过宽的值,计算高
**/
private int calculateRatio(int width) {
return (int) (width * mRatio);
}

/***
设置宽高比例,如宽高3/4。则传入0.75f即可
*/
public void setImageSizeRatio(float ratio) {
this.mRatio = ratio;
}

//然后修改每一个用到宽高的地方代码,如下:
private void layoutForNoSpanChildrenView(int childrenCount) {
if (childrenCount <= 0) return;
int row, column, left, top, right, bottom;
for (int i = 0; i < childrenCount; i++) {
ImageView childrenView = (ImageView) getChildAt(i);
row = i / mColumnCount;
column = i % mColumnCount;
left = (mGridSize[0] + mGap) * column + getPaddingLeft();//用到宽的地方值
top = (mGridSize[1] + mGap) * row + getPaddingTop();//用到高的地方值
right = left + mGridSize[0];//用到宽的地方值
bottom = top + mGridSize[1];//用到高的地方值
childrenView.layout(left, top, right, bottom);
if (mAdapter != null) {
mAdapter.onDisplayImage(getContext(), childrenView, mImgDataList.get(i));
}
}
}
`

说明:源码中是以前是用mGridSize表示图片宽和高的,现在宽高不一样,用一个数组表示。即可。。

目前已经在源码上修改实现了功能。期待作者下版本能加上此功能。。

开源是伟大的。。希望大家都能贡献一点力量。。。

@laobie
Copy link
Owner

laobie commented Sep 23, 2017

可以可以
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants