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将博客内容html导出为Markdown格式

《Python将博客内容html导出为Markdown格式》Python将博客内容html导出为Markdown格式,通过博客url地址抓取文章,分析并提取出文章标题和内容,将内容构建成html,再转... 目录一、为什么要搞?二、准备如何搞?三、说搞咱就搞!抓取文章提取内容构建html转存markdown

在React中引入Tailwind CSS的完整指南

《在React中引入TailwindCSS的完整指南》在现代前端开发中,使用UI库可以显著提高开发效率,TailwindCSS是一个功能类优先的CSS框架,本文将详细介绍如何在Reac... 目录前言一、Tailwind css 简介二、创建 React 项目使用 Create React App 创建项目

vue使用docxtemplater导出word

《vue使用docxtemplater导出word》docxtemplater是一种邮件合并工具,以编程方式使用并处理条件、循环,并且可以扩展以插入任何内容,下面我们来看看如何使用docxtempl... 目录docxtemplatervue使用docxtemplater导出word安装常用语法 封装导出方

Linux换行符的使用方法详解

《Linux换行符的使用方法详解》本文介绍了Linux中常用的换行符LF及其在文件中的表示,展示了如何使用sed命令替换换行符,并列举了与换行符处理相关的Linux命令,通过代码讲解的非常详细,需要的... 目录简介检测文件中的换行符使用 cat -A 查看换行符使用 od -c 检查字符换行符格式转换将

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

详解C#如何提取PDF文档中的图片

《详解C#如何提取PDF文档中的图片》提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使用,下面我们就来看看如何使用C#通过代码从PDF文档中提取图片吧... 当 PDF 文件中包含有价值的图片,如艺术画作、设计素材、报告图表等,提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使