|
1 | | -import { View, Text, StyleSheet, ActivityIndicator } from 'react-native'; |
| 1 | +import { useState } from 'react'; |
| 2 | +import { View, Text, StyleSheet, Button } from 'react-native'; |
| 3 | +import { SafeAreaView } from 'react-native-safe-area-context'; |
2 | 4 | import { Fit, RiveView, useRiveFile } from '@rive-app/react-native'; |
3 | 5 | import { type Metadata } from '../helpers/metadata'; |
4 | 6 |
|
5 | | -/** |
6 | | - * Demonstrates responsive layouts using Fit.Layout and layoutScaleFactor |
7 | | - * |
8 | | - * See https://rive.app/docs/runtimes/layout |
9 | | - */ |
10 | 7 | export default function ResponsiveLayoutsExample() { |
11 | | - const { riveFile, isLoading, error } = useRiveFile( |
| 8 | + const { riveFile } = useRiveFile( |
12 | 9 | require('../../assets/rive/layouts_demo.riv') |
13 | 10 | ); |
| 11 | + const [scaleFactor, setScaleFactor] = useState(4.0); |
| 12 | + |
| 13 | + const increaseScale = () => setScaleFactor((prev) => prev + 0.5); |
| 14 | + const decreaseScale = () => |
| 15 | + setScaleFactor((prev) => Math.max(0.5, prev - 0.5)); |
14 | 16 |
|
15 | 17 | return ( |
16 | | - <View style={styles.container}> |
17 | | - {isLoading ? ( |
18 | | - <ActivityIndicator size="large" color="#0000ff" /> |
19 | | - ) : error ? ( |
20 | | - <Text style={styles.errorText}>{error}</Text> |
21 | | - ) : riveFile ? ( |
| 18 | + <SafeAreaView style={styles.container}> |
| 19 | + {riveFile && ( |
22 | 20 | <RiveView |
23 | 21 | file={riveFile} |
24 | 22 | fit={Fit.Layout} |
25 | | - layoutScaleFactor={1} // adjust the layout scale factor to control the layout scale |
| 23 | + layoutScaleFactor={scaleFactor} |
26 | 24 | style={styles.rive} |
| 25 | + autoPlay={true} |
27 | 26 | /> |
28 | | - ) : null} |
29 | | - </View> |
| 27 | + )} |
| 28 | + <View style={styles.controls}> |
| 29 | + <Text style={styles.label}>Layout Scale Factor</Text> |
| 30 | + <View style={styles.scaleControls}> |
| 31 | + <Button title="-" onPress={decreaseScale} /> |
| 32 | + <View style={styles.scaleText}> |
| 33 | + <Text>{scaleFactor.toFixed(1)}x</Text> |
| 34 | + </View> |
| 35 | + <Button title="+" onPress={increaseScale} /> |
| 36 | + </View> |
| 37 | + </View> |
| 38 | + </SafeAreaView> |
30 | 39 | ); |
31 | 40 | } |
32 | 41 |
|
| 42 | +ResponsiveLayoutsExample.metadata = { |
| 43 | + name: 'Responsive Layouts', |
| 44 | + description: 'Interactive layout scale factor controls', |
| 45 | +} satisfies Metadata; |
| 46 | + |
33 | 47 | const styles = StyleSheet.create({ |
34 | | - // Adjust the container size and the layout will adjust based on the .riv file layout rules |
35 | 48 | container: { |
36 | | - width: '100%', |
37 | | - height: '100%', |
| 49 | + flex: 1, |
38 | 50 | }, |
39 | 51 | rive: { |
40 | | - justifyContent: 'center', |
41 | | - alignItems: 'center', |
42 | 52 | width: '100%', |
43 | | - height: '100%', |
| 53 | + flex: 1, |
44 | 54 | }, |
45 | | - errorText: { |
46 | | - color: 'red', |
47 | | - textAlign: 'center', |
48 | | - padding: 20, |
| 55 | + controls: { |
| 56 | + padding: 16, |
| 57 | + alignItems: 'center', |
| 58 | + }, |
| 59 | + scaleControls: { |
| 60 | + flexDirection: 'row', |
| 61 | + alignItems: 'center', |
| 62 | + marginVertical: 16, |
| 63 | + gap: 16, |
| 64 | + }, |
| 65 | + scaleText: { |
| 66 | + minWidth: 50, |
| 67 | + alignItems: 'center', |
| 68 | + }, |
| 69 | + label: { |
| 70 | + fontSize: 16, |
| 71 | + fontWeight: '500', |
| 72 | + marginTop: 16, |
49 | 73 | }, |
50 | 74 | }); |
51 | | - |
52 | | -ResponsiveLayoutsExample.metadata = { |
53 | | - name: 'Responsive Layouts', |
54 | | - description: 'Sample .riv file with responsive layouts', |
55 | | -} satisfies Metadata; |
|
0 commit comments