dohooo / react-native-reanimated-carousel

🎠 React Native swiper/carousel component, fully implemented using reanimated v2, support to iOS/Android/Web. (Swiper/Carousel)
https://react-native-reanimated-carousel.vercel.app
MIT License
2.86k stars 329 forks source link

Images are broken on Android #593

Open rsuubinn opened 7 months ago

rsuubinn commented 7 months ago

"react-native": "0.72.5", "react-native-reanimated-carousel": "^3.5.1",

I am developing apps in Android and iOS environments.

IMG_5557

Images are not broken in the iOS environment. This is the state I want.

스크린샷 2024-04-26 오전 10 00 03

However, in the Android environment, the background behind the image appears as if it is stretched. This is not the state I want.

import { Linking, Dimensions } from "react-native";
import styled from "styled-components/native";
import Carousel from "react-native-reanimated-carousel";

const PromotionContainer = styled.View`
  width: 100%;
`;

const PromotionItem = styled.TouchableOpacity`
  padding: 0px 16px;
  flex: 1;
  overflow: hidden;
`;

const PromotionImage = styled.Image`
  width: 100%;
  height: 100%;
  border-radius: 10px;
  /* object-fit: fill; // 늘어져 보임  */
  object-fit: contain; // 비율 맞으나 안드로이드가 이상
`;

const Promotion = ({ promotions }) => {
  const { width, height } = Dimensions.get("window");
  const renderItem = ({ item }) => (
    <PromotionItem key={item.id} onPress={() => Linking.openURL(item.url)}>
      <PromotionImage source={{ uri: item.image }} />
    </PromotionItem>
  );

  return (
    <PromotionContainer>
      <Carousel
        width={width}
        height={width / 3}
        autoPlay={true}
        autoPlayInterval={2000}
        loop={true}
        style={{ width: "100%" }}
        pagingEnabled={true}
        data={promotions}
        renderItem={renderItem}
      />
    </PromotionContainer>
  );
};

export default Promotion;

I want to use object-fit: contain to maintain the original image proportions.

스크린샷 2024-04-26 오전 10 11 30

Changing the height of the carousel to 3.55 improves the result, but the corners of the image are not clean. I would be very grateful if you could tell me the solution.

dosubot[bot] commented 7 months ago

To address the issue with image proportions and unclean corners in your Android environment, you should replace the object-fit property with resizeMode="contain" in your PromotionImage styled component. React Native uses resizeMode instead of object-fit for controlling how images fit within their containers. Here's the adjusted code for your PromotionImage:

const PromotionImage = styled.Image`
  width: 100%;
  height: 100%;
  border-radius: 10px;
  resize-mode: contain;
`;

This change ensures that the image scales to fit within the container without altering its aspect ratio, making the entire image visible even if it doesn't cover the entire space.

The issue with unclean corners on Android when using resizeMode="contain" might be due to how Android handles rendering of borders and overflows, especially with scaling and animations. To mitigate this, you could adjust the container's styling or the image's size and resizeMode. Testing with different borderRadius values or applying a background color that matches the primary color of the images might also help reduce the visibility of any unclean edges.

For more advanced image handling, consider using custom animations to dynamically adjust image sizes based on scroll position, ensuring images retain their original proportions across different device sizes. This approach is demonstrated in the example/app/src/pages/material-3/index.tsx file in the react-native-reanimated-carousel repository.

If the issue persists or you need further assistance, please provide more details about the specific problems you're encountering.

#### Details **Sources** - [Strange streched colors lines using and resizemode='contain' in renderItem function](https://github.com/dohooo/react-native-reanimated-carousel/issues/526) - [example/bare/App.tsx](https://github.com/dohooo/react-native-reanimated-carousel/blob/main/example/bare/App.tsx) - [example/app/src/pages/blur-rotate/index.tsx](https://github.com/dohooo/react-native-reanimated-carousel/blob/main/example/app/src/pages/blur-rotate/index.tsx) - [example/website/pages/props.mdx](https://github.com/dohooo/react-native-reanimated-carousel/blob/main/example/website/pages/props.mdx) - [example/app/src/pages/material-3/index.tsx](https://github.com/dohooo/react-native-reanimated-carousel/blob/main/example/app/src/pages/material-3/index.tsx)
**About Dosu** This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.