In any case you need something else you could use also WebView for it
render(props) {
const width = 220;
const height = 135;
const borderRadius = 15;
const uri = 'https://c.tenor.com/0wj4ApfUlWUAAAAM/whatever-bank-stare.gif';
// const uri = 'https://c.tenor.com/YwsCGem_MmQAAAAC/parks-and-rec-amy-poehler.gif',;
// const uri = 'https://media.tenor.com/images/1c39f2d94b02d8c9366de265d0fba8a0/tenor.gif';
return (
<View
style={{
width,
height,
borderRadius: 15,
overflow: 'hidden',
}}>
<WebView
source={{
uri
}}
style={{
flex: 1,
width,
height,
borderRadius,
}}
showsHorizontalScrollIndicator={false}
showsVerticalScrollIndicator={false}
injectedJavaScript={`
document.body.style.width = "${width}px";
document.body.style.height = "${height}px";
document.body.style.backgroundColor = "${'#fff'}";
document.body.style.overflow = "hidden";
const img = document.querySelector("img");
img.style.position = "absolute";
img.style.top = 0;
img.style.left = 0;
img.style.margin = "auto";
img.style[img.offsetWidth > img.offsetHeight ? 'width' : 'height'] = "100%";
img.style.borderRadius = "${borderRadius}px";
`}
/>
</View>
);
}
It will preserves the gif aspect ratio
Also, I did the same with the <Image />
. For anyone need it:
function ChatImageGIF(props) {
const maxWidth = 220;
const maxHeight = 135;
const [width, setWidth] = useState(maxWidth);
const [height, setHeight] = useState(maxHeight);
const borderRadius = 15;
Image.getSize(props.currentMessage.video, (w, h) => {
const minWidth = Math.min(maxWidth, w);
const minHeight = Math.min(maxHeight, h);
const aspectRatio = (w < h ? w : h) / (w < h ? h : w);
setWidth(w < h ? minHeight * aspectRatio : minWidth);
setHeight(w > h ? minWidth * aspectRatio : minHeight);
});
return (
<View
style={{
width,
height,
borderRadius: 15,
overflow: 'hidden',
}}>
<Image
source={{
uri: props.currentMessage.video,
}}
style={{
width,
height,
borderRadius,
resizeMode: 'contain',
}}
/>
</View>
);
}