javascript - React Native - 为什么我的图像按钮在 Android 上闪烁?

标签 javascript react-native

我有一个简单的图像按钮,它会在 TouchStart 和 onRelease 上更改其图像。在 iO 上,它的工作方式与预期一致,但在 Android 上,当图像发生变化时,按钮会闪烁。 看起来,如果每次我触摸图像时都必须重新加载图像。

class MyApp extends React.Component {

    constructor(props) {
        super(props);
    }

    render() {
        return (
            <View style={ styles.container }>
                <ImageButton
                    style={ styles.button }/>
            </View>
        );
    }
}
class ImageButton extends React.Component {

    constructor(props: {}) {
        super(props);

        this.image = {
            normal: require('./img/button.png'),
            highlight: require('./img/button-touched.png')
        };

        this.state = {
            image: this.image.normal
        };
    }

    onTouchStart() {
    
        // do some stuff
        
        // set the highlighted image
        this.setState({
            image: this.image.highlight
        });
    }

    onTouchEnd() {
        this.setState({
            image: this.image.normal
        })
    }

    onTouchCancel() {
        this.setState({
            image: this.image.normal
        });
    }

    componentWillReceiveProps(nextProps) {
        this.setState({
            image: nextProps.image.normal
        });
    }

    render() {
        return (
            <View style={ this.props.style }
                  onStartShouldSetResponder={ () => true }
                  onResponderGrant={ this.onTouchStart.bind(this) }
                  onResponderRelease={ this.onTouchEnd.bind(this) }
                  onResponderTerminate={ this.onTouchCancel.bind(this) }
                  onResponderReject={ this.onTouchCancel.bind(this) }>
                <Image style={ styles.image } source={ this.state.image } resizeMode="stretch" />
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#E4E4E4',
    },
    button: {
        backgroundColor: 'transparent',
    },
});

AppRegistry.registerComponent('schwein', () => Schwein);

最佳答案

图像在每个平台上的实现略有不同,所以这确实可能会发生。我建议使用启动时加载的这些图像创建组件,然后使用样式定义的不透明度控制其可见性。它应该按您的预期工作,不会闪烁。

关于javascript - React Native - 为什么我的图像按钮在 Android 上闪烁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42299179/

相关文章:

javascript - 服务器在使用 Meteor + 不匹配的异步结果的 HTTP.call 上崩溃

javascript - WebKit/Phantomjs 为什么 getComputedStyles 的输出是那样的?

javascript - Javascript 中的突变事件不起作用

javascript - React Native Undefined 不是一个对象(评估'userData.id)

javascript - 在 Chrome 中使用 React Native 调试 ES6 import 语句

javascript - Quiz ,希望在选择的答案不正确时同时显示正确和错误的答案

javascript - DOM 何时从内存中移除?

ios - 当端口被阻止时在设备上开发 React Native

ios - 重新映射 native 组件属性的正确方法是什么

javascript - 在 Jest 测试用例中比较包含匿名函数的对象