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

相关文章

C语言中联合体union的使用

本文编辑整理自: http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=179471 一、前言 “联合体”(union)与“结构体”(struct)有一些相似之处。但两者有本质上的不同。在结构体中,各成员有各自的内存空间, 一个结构变量的总长度是各成员长度之和。而在“联合”中,各成员共享一段内存空间, 一个联合变量

Tolua使用笔记(上)

目录   1.准备工作 2.运行例子 01.HelloWorld:在C#中,创建和销毁Lua虚拟机 和 简单调用。 02.ScriptsFromFile:在C#中,对一个lua文件的执行调用 03.CallLuaFunction:在C#中,对lua函数的操作 04.AccessingLuaVariables:在C#中,对lua变量的操作 05.LuaCoroutine:在Lua中,

Vim使用基础篇

本文内容大部分来自 vimtutor,自带的教程的总结。在终端输入vimtutor 即可进入教程。 先总结一下,然后再分别介绍正常模式,插入模式,和可视模式三种模式下的命令。 目录 看完以后的汇总 1.正常模式(Normal模式) 1.移动光标 2.删除 3.【:】输入符 4.撤销 5.替换 6.重复命令【. ; ,】 7.复制粘贴 8.缩进 2.插入模式 INSERT

JAVA读取MongoDB中的二进制图片并显示在页面上

1:Jsp页面: <td><img src="${ctx}/mongoImg/show"></td> 2:xml配置: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001

公共筛选组件(二次封装antd)支持代码提示

如果项目是基于antd组件库为基础搭建,可使用此公共筛选组件 使用到的库 npm i antdnpm i lodash-esnpm i @types/lodash-es -D /components/CommonSearch index.tsx import React from 'react';import { Button, Card, Form } from 'antd'

vue, 左右布局宽,可拖动改变

1:建立一个draggableMixin.js  混入的方式使用 2:代码如下draggableMixin.js  export default {data() {return {leftWidth: 330,isDragging: false,startX: 0,startWidth: 0,};},methods: {startDragging(e) {this.isDragging = tr

Lipowerline5.0 雷达电力应用软件下载使用

1.配网数据处理分析 针对配网线路点云数据,优化了分类算法,支持杆塔、导线、交跨线、建筑物、地面点和其他线路的自动分类;一键生成危险点报告和交跨报告;还能生成点云数据采集航线和自主巡检航线。 获取软件安装包联系邮箱:2895356150@qq.com,资源源于网络,本介绍用于学习使用,如有侵权请您联系删除! 2.新增快速版,简洁易上手 支持快速版和专业版切换使用,快速版界面简洁,保留主

如何免费的去使用connectedpapers?

免费使用connectedpapers 1. 打开谷歌浏览器2. 按住ctrl+shift+N,进入无痕模式3. 不需要登录(也就是访客模式)4. 两次用完,关闭无痕模式(继续重复步骤 2 - 4) 1. 打开谷歌浏览器 2. 按住ctrl+shift+N,进入无痕模式 输入网址:https://www.connectedpapers.com/ 3. 不需要登录(也就是

vue项目集成CanvasEditor实现Word在线编辑器

CanvasEditor实现Word在线编辑器 官网文档:https://hufe.club/canvas-editor-docs/guide/schema.html 源码地址:https://github.com/Hufe921/canvas-editor 前提声明: 由于CanvasEditor目前不支持vue、react 等框架开箱即用版,所以需要我们去Git下载源码,拿到其中两个主

React+TS前台项目实战(十七)-- 全局常用组件Dropdown封装

文章目录 前言Dropdown组件1. 功能分析2. 代码+详细注释3. 使用方式4. 效果展示 总结 前言 今天这篇主要讲全局Dropdown组件封装,可根据UI设计师要求自定义修改。 Dropdown组件 1. 功能分析 (1)通过position属性,可以控制下拉选项的位置 (2)通过传入width属性, 可以自定义下拉选项的宽度 (3)通过传入classN