React Native Animated 缩放图像

React Native Animated to scale an image

Animated API 我有 2 个问题。

1st: 我可以使用以下代码从左到右显示图像。我想将图像从位置 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 prop.

非常感谢。

我想这就是你想要的:

动画实际上非常流畅,在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坐标已经离开shifted 140 px, or -140 px,而我们希望x坐标只左移20 px,因此,我们应该右移120 px,这就是为什么x 的输出范围是 [0, 120]

  2. y的输出范围是[0, 25]

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

  4. 身高现在 250 与之前 20 相比,是 12.5