React Native: плохая производительность, анимирующая высоту представления с помощью response-native-reanimated - PullRequest
1 голос
/ 30 марта 2020

Анимация высоты представления при прокрутке списка очень медленная и прерывистая, она отлично работает для IOS, но не для Android:

import * as React from "react";
import { StyleSheet, Text } from "react-native";
import Animated from "react-native-reanimated";
const { Value, View, ScrollView, interpolate, Extrapolate } = Animated;
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "black"
  },
  listView: {
    flex: 1
  }
});

const IMAGE_MIN_HEIGHT = 300;

export default () => {
  const animation = new Value(0);

  const height = interpolate(animation, {
    inputRange: [0, 125],
    outputRange: [IMAGE_MIN_HEIGHT, 125],
    extrapolate: Extrapolate.CLAMP
  });

  return (
    <View style={styles.container}>
      <View
        style={{
          backgroundColor: "red",
          height: height,
          width: "100%"
        }}
      ></View>
      <ScrollView
        onScroll={Animated.event([
          {
            nativeEvent: {
              contentOffset: {
                y: animation
              }
            }
          }
        ])}
        scrollEventThrottle={1}
        style={[styles.listView]}
      >
        {[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0].map((elem, index) => (
          <View
            style={{ width: "100%", height: 100, marginBottom: 20 }}
            key={index}
          >
            <Text style={{ color: "white", fontSize: 30 }}>{elem}</Text>
          </View>
        ))}
      </ScrollView>
    </View>
  );
};

Есть ли что-нибудь, что может быть сделано, чтобы сделать его более гладким?

Ответы [ 2 ]

0 голосов
/ 30 марта 2020

решение состоит в том, чтобы установить абсолютную позицию заголовка, я не знаю почему, но она работает нормально:

import * as React from "react";
import { StyleSheet, Text, Platform } from "react-native";
import Animated from "react-native-reanimated";
const { Value, View, ScrollView, interpolate, Extrapolate } = Animated;
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "black"
  },
  listView: {
    flex: 1
  }
});

const IMAGE_MIN_HEIGHT = 300;

export default () => {
  const animation = new Value(0);

  const height = interpolate(animation, {
    inputRange: [0, 125],
    outputRange: [IMAGE_MIN_HEIGHT, 125],
    extrapolate: Extrapolate.CLAMP
  });

  return (
    <View style={styles.container}>
      <ScrollView
        contentContainerStyle={{ paddingTop: IMAGE_MIN_HEIGHT }}
        onScroll={Animated.event([
          {
            nativeEvent: {
              contentOffset: {
                y: animation
              }
            }
          }
        ])}
        scrollEventThrottle={1}
        style={[styles.listView]}
      >
        {[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0].map((elem, index) => (
          <View
            style={{ width: "100%", height: 100, marginBottom: 20 }}
            key={index}
          >
            <Text style={{ color: "white", fontSize: 30 }}>{elem}</Text>
          </View>
        ))}
      </ScrollView>

      <View
        style={{
          backgroundColor: "red",
          height: height,
          width: "100%",
          position: "absolute",
          top: Platform.OS == "ios" ? 20 : 0,
          left: 0,
          right: 0
        }}
      ></View>
    </View>
  );
};

0 голосов
/ 30 марта 2020

Попробуйте использовать NativeDriver для Android.

Кроме того, для повышения производительности вы должны использовать FlatList или SectionList здесь вместо ScrollView.

 <Animated.ScrollView
    onScroll={Animated.event([
      {
        nativeEvent: {
          contentOffset: {
            y: animation
          }
        }
      }
    ], 
     { useNativeDriver: true } // Add this line
    )}        
    scrollEventThrottle={1}
    style={[styles.listView]}
  >
    //
  </Animated.ScrollView>

Подробнее см. this

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...