hyochan / react-native-masonry-list

The Masonry List implementation which has similar implementation as the `FlatList` in React Native
MIT License
393 stars 55 forks source link

Wondering how to dynamic define the height of the item? The screenshot looks fancy, is there code share? #7

Closed dongdyang closed 3 years ago

dongdyang commented 3 years ago

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like A clear and concise description of what you want to happen.

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context Add any other context or screenshots about the feature request here.

hyochan commented 3 years ago

Yes you can see the MaonsryList Example. There is also Youtube

soly2014 commented 3 years ago

Hello @hyochan , thank u very much for ur awesome lib i wonder if i have dynamic height with the real image dimension instead of random height

hyochan commented 3 years ago

@soly2014 I don't think this is the role of the library itself. However, I already tried this implementation on my side and I'll share this shortly. Hope it helps. However, when I applied the below, I realized that random height looks much nicer so I've reverted it. Since most crawled images have the dimension of 1:1 which is 1024x1024.

  1. Create getOriginalImageSize function.
export const getOriginalImageSize = async (
  imageUri: string,
): Promise<ImageSize> =>
  new Promise<ImageSize>((resolve, reject) =>
    Image.getSize(
      imageUri,
      (width: number, height: number) =>
        resolve({
          width,
          height,
        }),
      reject,
    ),
 );
  1. Get image aspectRatio during rendering.

    const [imgAspectRatio, setImgAspectRatio] = useState<number>(0);
    
    const getImageRatio = useCallback(async (): Promise<void> => {
    const imageSize = await getOriginalImageSize(imageURL);
    
    if (imageSize) setImgAspectRatio(imageSize.height / imageSize.width);
    }, [imageURL]);
    
    useEffect(() => {
    getImageRatio();
    }, [getImageRatio]);
  2. Put aspectRatio to Image.

    <Image
    progressiveRenderingEnabled
    source={{uri: itemImageURL}}
    resizeMode="cover"
    style={{
    alignSelf: 'stretch',
    width: '100%',
    aspectRatio: imgAspectRatio,
    }}/>
soly2014 commented 3 years ago

@hyochan nice man how we can handle performance in case of large datasets ?? i mean virtualization