1- import { View , Text , StyleSheet , ActivityIndicator } from 'react-native' ;
2- import { Fit , RiveView , useRiveFile } from '@rive-app/react-native' ;
1+ import { useState , useRef , useEffect } from 'react' ;
2+ import {
3+ View ,
4+ Text ,
5+ StyleSheet ,
6+ Button ,
7+ ActivityIndicator ,
8+ useWindowDimensions ,
9+ } from 'react-native' ;
10+ import {
11+ Fit ,
12+ RiveView ,
13+ useRiveFile ,
14+ type RiveViewRef ,
15+ } from '@rive-app/react-native' ;
316import { type Metadata } from '../helpers/metadata' ;
417
518/**
@@ -11,6 +24,22 @@ export default function ResponsiveLayoutsExample() {
1124 const { riveFile, isLoading, error } = useRiveFile (
1225 require ( '../../assets/rive/layouts_demo.riv' )
1326 ) ;
27+ const [ scaleFactor , setScaleFactor ] = useState ( 4.0 ) ;
28+ const riveRef = useRef < RiveViewRef > ( null ) ;
29+ const { width, height } = useWindowDimensions ( ) ;
30+
31+ useEffect ( ( ) => {
32+ riveRef . current ?. playIfNeeded ( ) ;
33+ } , [ width , height ] ) ;
34+
35+ const increaseScale = ( ) => {
36+ setScaleFactor ( ( prev ) => prev + 0.5 ) ;
37+ riveRef . current ?. playIfNeeded ( ) ;
38+ } ;
39+ const decreaseScale = ( ) => {
40+ setScaleFactor ( ( prev ) => Math . max ( 0.5 , prev - 0.5 ) ) ;
41+ riveRef . current ?. playIfNeeded ( ) ;
42+ } ;
1443
1544 return (
1645 < View style = { styles . container } >
@@ -20,36 +49,64 @@ export default function ResponsiveLayoutsExample() {
2049 < Text style = { styles . errorText } > { error } </ Text >
2150 ) : riveFile ? (
2251 < RiveView
52+ hybridRef = { { f : ( ref ) => ( riveRef . current = ref ) } }
2353 file = { riveFile }
2454 fit = { Fit . Layout }
25- layoutScaleFactor = { 1 } // adjust the layout scale factor to control the layout scale
55+ layoutScaleFactor = { scaleFactor }
2656 style = { styles . rive }
57+ autoPlay = { true }
2758 />
2859 ) : null }
60+ < View style = { styles . controls } >
61+ < Text style = { styles . label } > Layout Scale Factor</ Text >
62+ < View style = { styles . scaleControls } >
63+ < Button title = "-" onPress = { decreaseScale } />
64+ < View style = { styles . scaleText } >
65+ < Text > { scaleFactor . toFixed ( 1 ) } x</ Text >
66+ </ View >
67+ < Button title = "+" onPress = { increaseScale } />
68+ </ View >
69+ </ View >
2970 </ View >
3071 ) ;
3172}
3273
74+ ResponsiveLayoutsExample . metadata = {
75+ name : 'Responsive Layouts' ,
76+ description : 'Interactive layout scale factor controls' ,
77+ } satisfies Metadata ;
78+
3379const styles = StyleSheet . create ( {
34- // Adjust the container size and the layout will adjust based on the .riv file layout rules
3580 container : {
36- width : '100%' ,
37- height : '100%' ,
81+ flex : 1 ,
3882 } ,
3983 rive : {
40- justifyContent : 'center' ,
41- alignItems : 'center' ,
4284 width : '100%' ,
43- height : '100%' ,
85+ flex : 1 ,
86+ } ,
87+ controls : {
88+ padding : 16 ,
89+ alignItems : 'center' ,
90+ } ,
91+ scaleControls : {
92+ flexDirection : 'row' ,
93+ alignItems : 'center' ,
94+ marginVertical : 16 ,
95+ gap : 16 ,
96+ } ,
97+ scaleText : {
98+ minWidth : 50 ,
99+ alignItems : 'center' ,
100+ } ,
101+ label : {
102+ fontSize : 16 ,
103+ fontWeight : '500' ,
104+ marginTop : 16 ,
44105 } ,
45106 errorText : {
46107 color : 'red' ,
47108 textAlign : 'center' ,
48109 padding : 20 ,
110+ flex : 1 ,
49111 } ,
50112} ) ;
51-
52- ResponsiveLayoutsExample . metadata = {
53- name : 'Responsive Layouts' ,
54- description : 'Sample .riv file with responsive layouts' ,
55- } satisfies Metadata ;
0 commit comments