【问题标题】:React Native Animated to scale an imageReact Native Animated 缩放图像
【发布时间】:2018-04-26 16:27:11
【问题描述】:

Animated API 有 2 个问题。

第一个:我可以使用以下代码从左到右显示图像。我想将图像从位置X=40 (leftPadding), Y=100(topPadding), height:20, width:20 缩放到X=20, Y=10, height:250, width:300。我如何做到这一点?

我的代码:

import React, { Component } from 'react';
import { StyleSheet, Text, Image, Animated, Easing, View, Button } from 'react-native';

class MyTestComp extends Component {
  componentWillMount() {
    this.animatedValue = new Animated.Value(0);
  }
  buttonPress(){
  this.animatedValue.setValue(0);
    Animated.timing(this.animatedValue,{
      toValue:1,
      duration:1000,
      Easing: Easing
    }).start()
  }

  render() {

    const translateX = this.animatedValue.interpolate({
      inputRange: [0, 1],
      outputRange: [-500, 1]
    })

    const transform = [{translateX}];

    return (
      <View>
        <Text>MyTestComp</Text>
        <Animated.View style={transform}>
        <Image
          source={require('./assets/17.jpg')}
          style={{width:300, height:250}}
        />
        </Animated.View>
        <View style={{marginTop:10}}>
          <Button title="Click Me" onPress={()=> this.buttonPress()} />
        </View>
      </View>
    );
  }
}


export default MyTestComp;

第二次:每次运行动画时,我都会遇到异常:

我找不到任何关于此的文档。如何使用transform 属性。

非常感谢。

【问题讨论】:

  • 如果您的代码与您在此处发布的代码相同,那么我认为您的问题只是语法错误。 style={transform} 应该是 style={{ transform }}

标签: reactjs react-native react-animated react-animations


【解决方案1】:

我想这就是你想要的:

动画实际上非常流畅,在 GIF 中看起来不是这样,因为 GIF 是每秒 4 帧。这是代码(由于您的数字都是常数,我只是在下面的代码中对它们进行了硬编码):

import React, { Component } from 'react'
import { Animated, View, TouchableOpacity, Easing,Text} from 'react-native'

const backgroundImage = require('....')

class App extends Component {
    constructor(props) {
        super(props)
        this.animatedValue = new Animated.Value(0)
    }

    handleAnimation = () => {
        Animated.timing(this.animatedValue, {
            toValue: 1,
            duration: 1000,
            easing: Easing.ease
        }).start()
    }

    render() {
        return (
            <View style={{ flex: 1 }}>
                <TouchableOpacity onPress={this.handleAnimation}>
                    <Text>
                       Transform Image
                    </Text>
                </TouchableOpacity>
                <Animated.Image
                    source={backgroundImage}
                    resizeMode='cover'
                    style={{
                        position: 'absolute',
                        left: 40,
                        top: 100,
                        height: 20,
                        width: 20,
                        transform: [
                            {
                                translateX: this.animatedValue.interpolate({
                                    inputRange: [0, 1],
                                    outputRange: [0, 120]
                                })
                            },
                            {
                                translateY: this.animatedValue.interpolate({
                                    inputRange: [0, 1],
                                    outputRange: [0, 25]
                                })
                            },
                            {
                                scaleX: this.animatedValue.interpolate({
                                    inputRange: [0, 1],
                                    outputRange: [1, 15]
                                })
                            },
                            {
                                scaleY: this.animatedValue.interpolate({
                                    inputRange: [0, 1],
                                    outputRange: [1, 12.5]
                                })
                            }
                        ]
                    }}
                />
            </View>
        )
    }
}

export default App

一些解释:

  1. 动画结束后,图像的宽度变为300,比280像素大,因为图像从中心放大,因此图像的x坐标左移140px,或-140px , 而我们希望 x 坐标只左移@​​987654328@ px,因此,我们应该右移它120 px,这就是为什么x 的输出范围是[0, 120]

  2. y 的输出范围为[0, 25] 的原因相同

  3. 现在的宽度是 300 与之前的 20 相比,是 15 的两倍

  4. 现在的高度是 250,与之前的 20 相比,是 12.5 的两倍

【讨论】:

  • 宽度未定义怎么办?假设我希望宽度适合整个屏幕,我可以从Dimensions 找到宽度。但是我如何计算ScaleXoutputRange?谢谢。
  • 我得到了使用((width-150)/2)-20 where width => let width = Dimensions.get('window').width
猜你喜欢
  • 2020-05-01
  • 2017-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
  • 2020-06-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多