Today i want to make a progress bar with a image on the left. Unfortunately I can't position the image in the right place. For now it looks like that enter image description here

I want to looks something like that - enter image description here

到目前为止我的代码-

<View
  style={{
    flexDirection: "row",
    alignSelf: "center",
    marginBottom: "20%",
    marginTop: "10%",
  }}
>
  <View
    style={{
      width: "90%",
      height: 30,
      padding: 2.5,
      backgroundColor: "#00000020",
      borderRadius: 30,
    }}
  >
    {/* <Animated.View
      style={[
        {
          width: "100%",
          height: 25,
          borderRadius: 15,
          backgroundColor: "#f5de41",
        },
        {
          width: progressAnim,
        },
      ]}
    ></Animated.View> */}
    <Image
      source={require("./assets/lion.png")}
      style={{
        height: 44,
        height: 44,
        position: "absolute",
      }}
      resizeMode="contain"
    />
  </View>
</View>

我try 在iamge的样式中添加left: '-62%',但它不起作用.我不知道怎么把狮子移到左边?

推荐答案

One approach将删除绝对位置,并使用flexbox将图像与行的末尾对齐:

const ProgressBar = ({imgSource,imgStyle,imgSize,style,progress,color})=>{
  let loadingAnim = useRef(new Animated.Value(progress)).current;
  const [{width,height},setViewDimensions] = useState({});
  // get parent view size
  const onLayout=({nativeEvent})=>{
    setViewDimensions({
      width:nativeEvent.layout.width,
      height:nativeEvent.layout.height
    })
  }
  const animatedWidth =loadingAnim.interpolate({
    inputRange:[0,1],
    outputRange:[0,width-imgSize],
    extrapolate:'clamp'
  })
  const containerAnimation = {
    margin:0,
    padding:0,
    width:Animated.add(animatedWidth,imgSize),
    backgroundColor:color,
    height:'100%',
    justifyContent:'center',
    overflow:'hidden'
  }
  useEffect(()=>{
    Animated.timing(loadingAnim,{
      toValue:progress,
      duration:100
    }).start()
  },[progress])
  return(
    <View style={[styles.loadingContainer,{height:imgSize*1.5||null},style]} onLayout={onLayout}>
      <Animated.View style={[styles.loadingContainer,containerAnimation]}>
        <Animated.Image
          source={imgSource}
          style={[{height:imgSize,width:imgSize,alignSelf:'flex-end'},imgStyle,{}]}
        />
      </Animated.View>
    </View> 
  )
}

我发现这种方法有点不顺畅.我认为这是因为图像的父视图的宽度正在设置动画,而不是图像的实际位置.

Another approach将为图像设置动画:

const ProgressBar = ({imgSource,imgStyle,imgSize,style,progress,color})=>{
  let widthAnim = useRef(new Animated.Value(progress)).current;
  const [{width,height},setViewDimensions] = useState({});
  // get parent view width to determine progress view size
  const onLayout=({nativeEvent})=>{
    setViewDimensions({
      width:nativeEvent.layout.width,
      height:nativeEvent.layout.height
    })
  }
  const animatedWidth = widthAnim.interpolate({
      inputRange:[0,1],
      outputRange:[0,width-imgSize],
      extrapolate:'clamp'
    })
  const containerAnimation = {
    // min width will be imgSize
    width:Animated.add(animatedWidth,imgSize),
    backgroundColor:color,  
  }
  const imgAnimation = {
    left:animatedWidth
  }
  // animate progress changess
  useEffect(()=>{
    Animated.timing(widthAnim,{
      toValue:progress,
      duration:100
    }).start()
  },[progress])
  return(
    <View>
      <View style={[styles.loadingContainer,{height:imgSize*1.25||null},style]} onLayout={onLayout}>
        <Animated.View style={[styles.progressBar,containerAnimation]}/>
      </View> 
          <Animated.Image
            source={imgSource}
            style={[styles.image,{height:imgSize,width:imgSize},imgStyle,imgAnimation]}
            resizeMode='contain'
          />
    </View> 
  )
}

React-native相关问答推荐

如何在Reaction Native中将身体分成三部分

如何从 onCacheEntryAdded 生命周期回调向 RTK 查询 useQuery() 挂钩抛出错误

在 React Native 中打开后日期 Select 器不关闭

React Native 应用程序没有在之前的工作代码中执行任何操作就无法运行

SignalR 与 React Native Expo:: 什么都没有发生

从 .apk 到 .aab 的本地构建版本react,如何将应用程序发送给客户端?

react-native (expo)加载markdown 文件

React Native 入门元素Bundle 失败,出现 Unexpected Token 错误

react-native app.js index.js

在 CircleCI 上 ReactNative 0.59.x build fails on CircleCI with exit value 137

React-native 解码 base64 编码字符串

Property body[41] of BlockStatement expected node to be of a type ["Statement"] but instead got "AssignmentExpression"

XCode AppIcon 基于方案

如何将 Crashlytics 与 React Native 应用程序集成

FlatList vs map react-native

[React-Native][Jest]SyntaxError: Unexpected token import

在 `../node_modules/react-native/React` 中找不到 `React-Core` 的 podspec

如何在 React Native 中禁用图像淡入效果?

React Native 中的 CSS 三角形

React Native - 导航问题undefined is not an object (this.props.navigation.navigate)