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

Images are broken on Android #593

Open
rsuubinn opened this issue Apr 26, 2024 · 1 comment
Open

Images are broken on Android #593

rsuubinn opened this issue Apr 26, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@rsuubinn
Copy link

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

@rsuubinn rsuubinn added the bug Something isn't working label Apr 26, 2024
Copy link

dosubot bot commented Apr 26, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants