[英]How to show the only part of the image
UPDATE Since the question was complicating and unclear, I'm rewriting my question to make it much simpler.更新由于问题复杂且不清楚,我正在重写我的问题以使其更简单。
Given给定
what I want我想要的是
Here is my example code这是我的示例代码
// render the part of the image
console.log(left); // 50
console.log(thumbSize); // 300
return (
<Image
source={{uri: image}}
style={selectedStyle(left, top, thumbSize)}/>
);
...
function selectedStyle(left, top, thumbSize) {
return {
left,
top,
width: thumbSize,
height: thumbSize
};
}
UPDATE from zvona's working demo, what I want is this.更新来自 zvona 的工作演示,我想要的是这个。
Here is a working example: https://snack.expo.io/@zvona/cropped-image这是一个工作示例: https ://snack.expo.io/@zvona/cropped-image
The idea is to have "cropped" View
where Image
is positioned inside it with custom dimensions.这个想法是“裁剪” View
,其中Image
位于其中并具有自定义尺寸。 I use constants in my example to clarify the case.我在示例中使用常量来说明情况。
<View style={styles.cropped}>
<Image
style={styles.image}
source={{uri: 'https://upload.wikimedia.org/wikipedia/en/0/02/Homer_Simpson_2006.png'}} />
</View>
And on styles:关于样式:
image: {
marginLeft: -OFFSET_LEFT,
marginTop: -OFFSET_TOP,
width: IMAGE_WIDTH,
height: IMAGE_HEIGHT,
},
cropped: {
width: 150,
height: 150,
overflow: 'hidden',
position: 'absolute',
left: OFFSET_LEFT,
top: OFFSET_TOP,
},
Note that ImageBackground
is only for example purposes and it's not needed in the actual implementation.请注意, ImageBackground
仅用于示例目的,实际实现中不需要它。
Let me understand here.让我在这里了解一下。 You want to show only part of an image(x: 50, y:10, width: 300, height 300) and then resize this part to 70x70, right?您只想显示图像的一部分(x:50,y:10,宽度:300,高度 300),然后将此部分调整为 70x70,对吗? So 70x70 is the size of the final container.所以 70x70 是最终容器的大小。 First calculate the the x and y coords.首先计算 x 和 y 坐标。 with respect to the final size关于最终尺寸
var ar = 70 / 300
var x = 50 * ar
var y = 100 * ar
Then the width in percent:然后是百分比宽度:
var pc = (600 / 300) * 100
Assuming you show the cropped and resized image as a div background you do like this:假设您将裁剪和调整大小的图像显示为 div 背景,您可以这样做:
div.style.background = 'no-repeat ' + -x + 'px' + ' ' + -y + 'px/' + pc + '% url("' + imgAbsPath + '")'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.