dohooo / react-native-reanimated-carousel

🎠 React Native swiper/carousel component, fully implemented using reanimated v2, support to iOS/Android/Web. (Swiper/Carousel)

Home Page:https://react-native-reanimated-carousel.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Images are broken on Android

rsuubinn opened this issue Β· comments

"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.

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


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.