大飞

大飞 关注TA

挑战一切!

大飞

大飞

关注TA

挑战一切!

  •  世界的顶端
  • 混口饭吃
  • 写了333,609字

该文章投稿至Nemo社区   Js、Css、Html  板块 复制链接


微信小程序——图片自适应

发布于 2017/01/14 21:03 3,103浏览 0回复 3,613

   在开发者中,各种尺寸的图片都可能有,仅仅将图片的高宽写死,往往很难看,当然在安卓中可以设置图片的缩放属性也会达到很好的效果,而在微信小程序中,本人未发现这个方法,所以可以通过计算,让图片显示得更好。

    实现思路:

   1.获取屏幕的宽高,计算宽高比

  2.获取图片的宽高,计算宽高比

  3.对比两者的宽高比,缩放图片对应的宽高

  4.给予图片重新赋值


 视图层:

"true" style="height: 600px;">
  <view wx:for="{{imagelist}}" wx:for-index="idx">
    <image id="{{idx}}" style="width: {{item.imagewidth}}px; height: {{item.imageheight}}px;" src="{{item.imageUrl}}" bindload="imageLoad">image>
  view>
</scroll-view>

控制层:

Page({
  data: {
    maginBottom: 0,
    magnRight: 0,
    screenWidth: 0,
    screenHeight: 0
  },

  onLoad: function () {
    var _this = this;
    //获取屏幕的宽高
    wx.getSystemInfo({
      success: function (res) {
        // success
        console.log(res)
        _this.setData({
          screenHeight: res.windowHeight,
          screenWidth: res.windowWidth,
          maginBottom: res.windowHeight - 40,
          magnRight: res.windowWidth - 40
        })
      }
    })
  },
  /**
   * 监控移动的大小 并设置到图片上
   */
  touhMoveMethod: function (e) {
    var _this = this;
    console.log(e);
    var touchs = e.touches[0];
    var pageX = touchs.pageX;
    var pageY = touchs.pageY;
    //防止越界
    if (pageX < 0 || pageY < 0 || pageX > this.data.screenWidth - 40 || this.data.screenHeight - pageY <= 40) return;
    _this.setData({
      maginBottom: _this.data.screenHeight - 40 - pageY,
      magnRight: _this.data.screenWidth - 40 - pageX
    })

  }
})







Util:

function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * 显示toast
 */
function showToast(name) {
  wx.showToast({
    title: name
  })
}
/** 
 * 获取屏幕宽高
 */
function getScreenInfo() {
  var screenInfo = {}

  wx.getSystemInfo({
    success: function (res) {
      // success
      screenInfo.width = res.windowWidth
      screenInfo.height = res.windowHeight

   console.log('屏幕宽: ' +  screenInfo.width)
    console.log('屏幕高: ' +  screenInfo.height)
    }
  })
  return screenInfo
}
/**
 * 自适应图片宽高
 */
function getAutoImage(e) {
  var imageSize = {};
  var originalWidth = e.detail.width;//图片原始宽  
  var originalHeight = e.detail.height;//图片原始高  
  var originalScale = originalHeight / originalWidth;//图片高宽比  
  console.log('原图宽: ' + originalWidth)
  console.log('原图高: ' + originalHeight)
  var screenInfo = getScreenInfo();
  var windowscale = screenInfo.height / screenInfo.width;//屏幕高宽比  
   console.log('图片高宽比: ' + originalScale)  
  console.log('屏幕高宽比: ' + windowscale)  
  if (originalScale < windowscale) {//图片高宽比小于屏幕高宽比  
    //图片缩放后的宽为屏幕宽  
    imageSize.imageWidth = screenInfo.width;
    imageSize.imageHeight = (screenInfo.width * originalHeight) / originalWidth;
  } else {//图片高宽比大于屏幕高宽比  
    //图片缩放后的高为屏幕高  
    imageSize.imageHeight = screenInfo.height;
    imageSize.imageWidth = (screenInfo.height * originalWidth) / originalHeight;
  }
  console.log('缩放后的宽: ' + imageSize.imageWidth)  
  console.log('缩放后的高: ' + imageSize.imageHeight)  
  return imageSize;  

}




module.exports = {
  formatTime: formatTime,
  showToast: showToast,
  getScreenInfo: getScreenInfo,
  getAutoImage: getAutoImage
}


本文标签
 {{tag}}
点了个评