React Native 的图片点击放大效果的组件使用 react-native-zoom-image

本文主要是介绍React Native 的图片点击放大效果的组件使用 react-native-zoom-image,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.在index.Android.js下书写

/**
 * Created by TinySymphony on 2017-03-23.
 */

import React, {Component}  from  'react';
import {
StyleSheet,
AppRegistry,
View,
Text,
Easing,
ScrollView
from  'react-native';

import ZoomImage  from  './ZoomImage';

export default  class  App  extends  Component {
constructor( props) {
super(props);
this.text  =  '';
}
render() {
return (
<ScrollView >
<View style ={styles.content} >
< Text style ={styles.txt} >Zoom Image Examples ! Try to click them ~</ Text >

<View style ={styles.imgItem} >
<ZoomImage
source ={{uri :  'https://avatars2.githubusercontent.com/u/7685233?v=3&s=460'}}
imgStyle ={{width :  220, height :  220}}
style ={styles.img}
/>
</View >

<View style ={styles.imgItem} >
<ZoomImage
source ={{uri :  'https://ooo.0o0.ooo/2017/03/31/58de0e9b287f6.jpg'}}
imgStyle ={{width :  250, height :  230}}
style ={styles.img}
enableScaling ={ true}
/>
</View >

<View style ={styles.imgItem} >
<ZoomImage
source ={{uri :  'https://ooo.0o0.ooo/2017/03/31/58de0e9b28328.jpg'}}
imgStyle ={{width :  250, height :  240}}
style ={styles.img}
easingFunc ={Easing.bounce}
/>
</View >

</View >
</ScrollView >
);
}
}

const  styles  =  StyleSheet. create({
content : {
justifyContent :  'center',
alignItems :  'center'
},
txt : {
fontSize :  16,
marginTop :  50,
color :  '#333'
},
img : {
borderWidth :  3,
borderColor :  '#45b7d5'
},
imgItem : {
justifyContent :  'center',
alignItems :  'center',
margin :  20
}
});

AppRegistry. registerComponent( 'App', ()  => App);
// export default App;
// import { AppRegistry } from 'react-native';

// import App from './App';

// AppRegistry.registerComponent('Example', (): App => App);

2.ZoomImage.js文件

import React, {PropTypes, Component}  from  'react';
import {
View,
Text,
Image,
Modal,
Easing,
StyleSheet,
PanResponder,
NativeModules,
findNodeHandle,
Dimensions,
TouchableWithoutFeedback
from  'react-native';
import Animation  from  './Animation';
const  winWidth  = Dimensions. get( 'window').width;
const  winHeight  = Dimensions. get( 'window').height;
const  winRatio  = winWidth  / winHeight;

const  RCTUIManager  = NativeModules.UIManager;

class  ZoomImage  extends  Component {
static propTypes  = {
startCapture : PropTypes.bool,
moveCapture : PropTypes.bool,
responderNegotiate : PropTypes.func,
easingFunc : PropTypes.func,
duration : PropTypes.number,
enableScaling : PropTypes.bool
}
static defaultProps  = {
startCapture :  false,
moveCapture :  false,
duration :  800,
easingFunc : Easing.ease,
enableScaling :  false
}
constructor( props) {
super(props);
this.state  = {
maxSize : {
width :  0,
height :  0
},
isModalVisible :  false
};
this.enableModal  =  false;
this.closeModal  = this.closeModal. bind(this);
this.openModal  = this.openModal. bind(this);
this.getMaxSizeByRatio  = this.getMaxSizeByRatio. bind(this);
}
getMaxSizeByRatio ( ratio) {
return {
width : ratio  >= winRatio  ? winWidth  : winWidth  / ratio,
height : ratio  >= winRatio  ? winWidth  / ratio  : winHeight
};
}
componentDidMount () {
if (this.props.source.uri) {
Image. getSize(this.props.source.uri, ( wh=> {
this. setState(( state=> {
state.maxSize  = this. getMaxSizeByRatio(w  / h);
this.enableModal  =  true;
});
});
else {
this. setState(( state=> {
state.maxSize  = this. getMaxSizeByRatio(this.props.imgStyle.width  / this.props.imgStyle.height);
this.enableModal  =  true;
});
}
}
openModal () {
if ( !this.refs.view  ||  !this.enableModal)  return;
RCTUIManager. measure( findNodeHandle(this.refs.view), ( xywhpxpy=> {
this.originPosition  = {x, y, w, h, px, py};
});
this. setState({
isModalVisible :  true
});
}
closeModal () {
this. setState({
isModalVisible :  false
});
}
render () {
return (
<TouchableWithoutFeedback style ={this.props.imgStyle}
onPress ={this.openModal}
ref = "view" >
<View style ={this.props.style} >
<Image
source ={this.props.source}
resizeMode ={this.props.resizeMode}
style ={this.props.imgStyle} />
<ImageModal
visible ={this.state.isModalVisible}
onClose ={this.closeModal}
originPosition ={this.originPosition}
size ={this.state.maxSize}
minAlpha ={this.props.minAlpha}
source ={this.props.source}
duration ={this.props.duration}
easingFunc ={this.props.easingFunc}
enableScaling ={this.props.enableScaling} />
</View >
</TouchableWithoutFeedback >
);
}
}

class  ImageModal  extends  Component {
constructor( props) {
super(props);
this._initModalStyle  = {
style : {
backgroundColor :  'rgba(0, 0, 0, 1)'
}
};
this._modalStyle  =  JSON. parse( JSON. stringify(this._initModalStyle));
this._initContentStyle  = {
style : {
top :  0,
bottom :  0,
left :  0,
right :  0
}
};
this._contentStyle  =  JSON. parse( JSON. stringify(this._initContentStyle));
this._initImgSize  = {
style : this.props.size
};
this._imgSize  =  JSON. parse( JSON. stringify(this._initImgSize));
this._inAnimation  =  false;
this._setNativeProps  = this._setNativeProps. bind(this);
this._closeModalByTap  = this._closeModalByTap. bind(this);
this._closeModal  = this._closeModal. bind(this);
this._rebounce  = this._rebounce. bind(this);
this._touchPositionCheck  = this._touchPositionCheck. bind(this);
this._updateNativeStyles  = this._updateNativeStyles. bind(this);
this._pan  = PanResponder. create({
onStartShouldSetPanResponder : this._onStartShouldSetPanResponder. bind(this),
onStartShouldSetPanResponderCapture : ( evtgestureState=> this.props.startCapture,
onMoveShouldSetPanResponder : this._onMoveShouldSetPanResponder. bind(this),
onMoveShouldSetPanResponderCapture : ( evtgestureState=> this.props.moveCapture,
onPanResponderTerminationRequest : ( evtgestureState=>  true,
onPanResponderGrant : this._handlePanResponderGrant. bind(this),
onPanResponderMove : this._handlePanResponderMove. bind(this),
onPanResponderRelease : this._handlePanResponderEnd. bind(this),
onPanResponderTerminate : this._handlePanResponderEnd. bind(this),
onShouldBlockNativeResponder : ( evtgestureState=>  true
});
}
_onStartShouldSetPanResponder ( evtgestureState) {
// set responder for tapping when the drawer is open
// TODO: tap close
if (this._inAnimation)  return;
return  false;
}
_onMoveShouldSetPanResponder ( evtgestureState) {
// custom pan responder condition function
if (this._inAnimation)  return;
if (this.props.responderNegotiate  && this.props. responderNegotiate(evt, gestureState)  ===  falsereturn  false;
if (this. _touchPositionCheck(gestureState)) {
return  true;
}
return  false;
}
_handlePanResponderGrant( evtgestureState) {
}
_handlePanResponderMove ( evtgestureState) {
const { dy= gestureState;
this. _updateNativeStyles(dy);
}
_handlePanResponderEnd ( evtgestureState) {
const { dy= gestureState;
if (dy  >  0.4  * winHeight) {
this. _closeModal( true);
else  if ( -dy  >  0.4  * winHeight) {
this. _closeModal( false);
else {
this. _rebounce();
}
}
_touchPositionCheck( gestureState) {
const { dxdy= gestureState;
if ( Math. abs(dy)  <=  Math. abs(dx)) {
return  false;
}
return  true;
}
_closeModal( isDown) {
const { easingFunconClose= this.props;
let current  = this._contentStyle.style.top;
this._inAnimation  =  true;
new  Animation({
start : current,
end : isDown  ? winHeight  :  -winHeight,
duration :  140,
easingFunc,
onAnimationFrame : ( val=> {
this. _updateNativeStyles(val);
},
onAnimationEnd : ()  => {
this._inAnimation  =  false;
onClose();
this. _setNativeProps( true);
}
}).start();
}
_closeModalByTap() {
if (this._inAnimation) {
return  false;
}
this. _closeModal( true);
}
_rebounce( isDown) {
const { durationeasingFunc= this.props;
let current  = this._contentStyle.style.top;
this._inAnimation  =  true;
new  Animation({
start : current,
end :  0,
duration :  Math. abs(current  / winHeight)  * duration,
easingFunc,
onAnimationFrame : ( val=> {
this. _updateNativeStyles(val);
},
onAnimationEnd : ()  => {
this._inAnimation  =  false;
}
}).start();
}
_updateNativeStyles( dy) {
const {
width,
height
= this.props.size;
// this._contentStyle.style.left = dx;
// this._contentStyle.style.right = -dx;
this._contentStyle.style.top  = dy;
this._contentStyle.style.bottom  =  -dy;
this._modalStyle.style.backgroundColor  =  `rgba(0, 0, 0, ${ 1   -   Math . abs (dy)  /  winHeight  *   0.9 })`;
if (this.props.enableScaling) {
this._imgSize.style.width  = width  * ( 1  -  Math. abs(dy  / winHeight)  *  0.6);
this._imgSize.style.height  = height  * ( 1  -  Math. abs(dy  / winHeight)  *  0.6);
else {
this._imgSize.style.width  = width;
this._imgSize.style.height  = height;
}
this. _setNativeProps();
}
_setNativeProps( isReset) {
if (isReset) {
this._contentStyle  =  JSON. parse( JSON. stringify(this._initContentStyle));
this._modalStyle  =  JSON. parse( JSON. stringify(this._initModalStyle));
this._imgSize  =  JSON. parse( JSON. stringify(this._initImgSize));
}
this.content  && this.content. setNativeProps(this._contentStyle);
this.mask  && this.mask. setNativeProps(this._modalStyle);
this.img  && this.img. setNativeProps(this._imgSize);
}
componentDidUpdate () {
new  Animation({
start :  0,
end :  1,
duration :  100,
easingFunc : Easing.ease,
onAnimationFrame : ( val=> {
this.mask  && this.mask. setNativeProps({style : {
opacity : val
}});
},
onAnimationEnd : ()  => {
this._inAnimation  =  false;
}
}).start();
}
render () {
const {
visible,
onClose,
source,
size   // origin size of the image
= this.props;
if (visible) { this._inAnimation  =  true; }
this._initImgSize.style  = size;
return (
<Modal
visible ={visible}
transparent ={ true}
onRequestClose ={onClose} >
<View style ={styles.mask} ref ={ mask  => {this.mask  = mask;}} { ...this._pan.panHandlers} >
<TouchableWithoutFeedback
ref ={ ref  => {this.imgContainer  = ref;}}
onPress ={this._closeModalByTap} >
<View
ref ={ ref  => {this.content  = ref;}}
style ={styles.content} >
<Image ref ={ img  => {this.img  = img;}} source ={source} style ={[size, styles.img]} />
</View >
</TouchableWithoutFeedback >
</View >
</Modal >
);
}
}

const  styles  =  StyleSheet. create({
mask : {
position :  'absolute',
right :  0,
left :  0,
top :  0,
bottom :  0,
backgroundColor :  'rgba(0, 0, 0, 1)',
opacity :  0
},
content : {
position :  'absolute',
right :  0,
left :  0,
top :  0,
bottom :  0,
justifyContent :  'center',
alignItems :  'center',
backgroundColor :  'transparent'
},
toucharea : {
flex :  1,
justifyContent :  'center',
alignItems :  'center',
alignSelf :  'stretch'
},
modalText : {
color :  '#fff'
},
img : {
}
});

ZoomImage.ImageModal  = ImageModal;

export default ZoomImage;


3.Animation.js

export default  function  Animation( option) {
this.animate  = this.animate. bind(this);
this.start  = this.start. bind(this);
this.option  = option;
}

Animation.prototype. animate  =  function ( now) {
const {
start,
end,
duration,
onAnimationFrame,
onAnimationEnd  = ()  => {},
easingFunc  =  t  => t
= this.option;
var currentDuration  = now  - this.startTime;
if (currentDuration  >= duration) {
onAnimationFrame(end);
onAnimationEnd();
return;
}
let value;
if (start  > end) {
value  = start  - (start  - end)  *  easingFunc(currentDuration  / duration);
else {
value  = (end  - start)  *  easingFunc(currentDuration  / duration)  + start;
}
onAnimationFrame(value);
requestAnimationFrame(this.animate);
};

Animation.prototype. start  =  function () {
this.startTime  =  new  Date();
this. animate(this.startTime);
};

Examples效果如下

项目原地址https://github.com/Tinysymphony/react-native-zoom-image

这篇关于React Native 的图片点击放大效果的组件使用 react-native-zoom-image的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/509786

相关文章

Python使用Spire.PDF实现为PDF添加水印

《Python使用Spire.PDF实现为PDF添加水印》在现代数字化办公环境中,PDF已成为一种广泛使用的文件格式,尤其是在需要保持文档格式时,下面我们就来看看如何使用Python为PDF文件添加水... 目录一、准备工作二、实现步骤1. 导入必要的库2. 创建 PdfDocument 对象3. 设置水印

Java中的ConcurrentBitSet使用小结

《Java中的ConcurrentBitSet使用小结》本文主要介绍了Java中的ConcurrentBitSet使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、核心澄清:Java标准库无内置ConcurrentBitSet二、推荐方案:Eclipse

Go语言结构体标签(Tag)的使用小结

《Go语言结构体标签(Tag)的使用小结》结构体标签Tag是Go语言中附加在结构体字段后的元数据字符串,用于提供额外的属性信息,这些信息可以通过反射在运行时读取和解析,下面就来详细的介绍一下Tag的使... 目录什么是结构体标签?基本语法常见的标签用途1.jsON 序列化/反序列化(最常用)2.数据库操作(

Java中ScopeValue的使用小结

《Java中ScopeValue的使用小结》Java21引入的ScopedValue是一种作用域内共享不可变数据的预览API,本文就来详细介绍一下Java中ScopeValue的使用小结,感兴趣的可以... 目录一、Java ScopedValue(作用域值)详解1. 定义与背景2. 核心特性3. 使用方法

spring中Interceptor的使用小结

《spring中Interceptor的使用小结》SpringInterceptor是SpringMVC提供的一种机制,用于在请求处理的不同阶段插入自定义逻辑,通过实现HandlerIntercept... 目录一、Interceptor 的核心概念二、Interceptor 的创建与配置三、拦截器的执行顺

C#中checked关键字的使用小结

《C#中checked关键字的使用小结》本文主要介绍了C#中checked关键字的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录✅ 为什么需要checked? 问题:整数溢出是“静默China编程”的(默认)checked的三种用

C#中预处理器指令的使用小结

《C#中预处理器指令的使用小结》本文主要介绍了C#中预处理器指令的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 第 1 名:#if/#else/#elif/#endif✅用途:条件编译(绝对最常用!) 典型场景: 示例

Mysql中RelayLog中继日志的使用

《Mysql中RelayLog中继日志的使用》MySQLRelayLog中继日志是主从复制架构中的核心组件,负责将从主库获取的Binlog事件暂存并应用到从库,本文就来详细的介绍一下RelayLog中... 目录一、什么是 Relay Log(中继日志)二、Relay Log 的工作流程三、Relay Lo

使用Redis实现会话管理的示例代码

《使用Redis实现会话管理的示例代码》文章介绍了如何使用Redis实现会话管理,包括会话的创建、读取、更新和删除操作,通过设置会话超时时间并重置,可以确保会话在用户持续活动期间不会过期,此外,展示了... 目录1. 会话管理的基本概念2. 使用Redis实现会话管理2.1 引入依赖2.2 会话管理基本操作

Springboot请求和响应相关注解及使用场景分析

《Springboot请求和响应相关注解及使用场景分析》本文介绍了SpringBoot中用于处理HTTP请求和构建HTTP响应的常用注解,包括@RequestMapping、@RequestParam... 目录1. 请求处理注解@RequestMapping@GetMapping, @PostMappin