[ 卡卡 DAY 17 ] - React Native 用 Animated 来做简单骨架屏

上一章节讲了 Animated 的使用
我们运用 start() 来做个骨架屏唷!

制作 banner 骨架屏

  1. 引入 Animated
import { Animated } from 'react-native';
  1. 建立初始值
thumbnailAnimated = new Animated.Value(0); // 骨架屏的初始值
imageAnimated = new Animated.Value(0); // 真正banner的初始值
  1. 写入 Animated.Image
// 骨架屏
<Animated.Image
    {...props}
    source={thumbnailSource}
    style={[style, { opacity: this.thumbnailAnimated }]}
    onLoad={this.handleThumbnailLoad}
    blurRadius={1}
/>
// banner
<Animated.Image
    {...props}
    source={source}
    style={[styles.imageOverlay, { opacity: this.imageAnimated }]}
    onLoad={this.onImageLoad}
/>
  1. 写 handleThumbnailLoad 及 onImageLoad 的 function
// 骨架屏
handleThumbnailLoad = () => {
    Animated.timing(this.thumbnailAnimated, {
    toValue: 1
    }).start();
};
// banner
onImageLoad = () => {
    Animated.timing(this.imageAnimated, {
    toValue: 1,
    duration: 1000
    }).start();
};
  1. 做一个有 banner 的 screen 引入 ProgressiveImage
    <ProgressiveImage
        thumbnailSource={require('../asset/version-1.png')} // 骨架屏图片
        source={img} // banner
        style={styles.bannerImg}
        resizeMode="cover"
        text="文字文字"
    />

ProgressiveImage.js

import React from 'react';
import { View, Animated, StyleSheet, Text } from 'react-native';

class ProgressiveImage extends React.Component {
    thumbnailAnimated = new Animated.Value(0);
    imageAnimated = new Animated.Value(0);
    handleThumbnailLoad = () => {
        Animated.timing(this.thumbnailAnimated, {
        toValue: 1
        }).start();
    };
    onImageLoad = () => {
        Animated.timing(this.imageAnimated, {
        toValue: 1,
        duration: 1000
        }).start();
    };
    render() {
        const { thumbnailSource, source, style, ...props } = this.props;
        return (
            <View style={styles.container}>
                <Animated.Image
                    {...props}
                    source={thumbnailSource}
                    style={[style, { opacity: this.thumbnailAnimated }]}
                    style={style}
                    onLoad={this.handleThumbnailLoad}
                    blurRadius={1}
                />
                <Animated.Image
                    {...props}
                    source={source}
                    style={[styles.imageOverlay, { opacity: this.imageAnimated }]}
                    onLoad={this.onImageLoad}
                />
                <Text style={[styles.bannerText, { lineHeight: 0 }]}>
                    {props.text}
                </Text>
            </View>
        );
    }
}
const styles = StyleSheet.create({
    imageOverlay: {
        position: 'absolute',
        left: 0,
        right: 0,
        bottom: 0,
        top: 0
    },
    container: {
        backgroundColor: '#e1e4e8'
    },
    bannerText: {
        color: "#fff",
        position: 'absolute',
        left: 15,
        bottom: 100
    }
});
export default ProgressiveImage;

运用 animated 的特性将图片读取後慢慢淡出,倘若没有读取成功,就会由骨架屏来显示!!
倘若骨架屏不想有图片只想有背景色
可忽略所有有包含 thumbnail 文字的 func or 变数
他会取 container 的背景颜色来当预设

Day18 done,请多多指教~


<<:  day17 不懂kotlin flow资料流? 那喝杯进口奶茶吧

>>:  [Day 19 - React] 现在开始用框架写网页 — React

[GWS] 服务简单做-RESTful的开发方式

在Genero FGL上也可以做出 RESTful 的 WEB Service。 先将回应WEB R...

JS 38 - 实作 Tab 页签

大家好! 样式 .tabs { width: 100%; max-width: 60em; disp...

[读书笔记] Threading in C# - PART 2: BASIC SYNCHRONIZATION

本篇同步发文於个人Blog: [读书笔记] Threading in C# - PART 2: BA...

Day 09 CSS <背景属性>

CSS背景属性 可以给页面元素添加背景样式 背景属性可设置背景颜色、背景图片、背景平舖、背景图片位置...

Day19:今天来聊一下Denial-of-Service

拒绝服务(DoS)和分布式拒绝服务(DDoS)攻击已成为电脑网路的主要威胁。 这类攻击试图让机器或网...