微信小程序全栈开发实战学习五(view容器组件及Flex布局一个view如何实现所有常见的UI布局?)

1.如何使用view实现常见的UI布局

(1)先来看view常见的属性
在这里插入图片描述
hover-class:按住元素会加一个bc_red的类样式
hover-stop-propagation:阻止父节点出现单击态,默认为false不阻止
这样点击子组件,父组件就不会触发了
在这里插入图片描述
catchtap同bindtap事件一样,也是绑定事件,但是可以阻止冒泡事件,这样点击子元素就不会触发父元素的点击事件。

(2)接下来了解下flex布局
先来了解下主轴辅轴,这两个概念是通过flex-direction设置的值来判断的,值为row(从左到右)或者row-reverse(从右到左)时,表示元素横向排列,主轴为X辅轴为Y。值为colum(从上到下)或者colum-reverse(从下到上)的时候,纵向排列,主轴为Y辅轴为X。
ps:接下来的例子默认主轴为X辅轴为Y
1.justify-content:调整内容在主轴方向的排列方式
- flex-start:元素向主轴的起点看齐
在这里插入图片描述

  • flex-end:元素向主轴的尾部看齐

  • center:居中对齐

  • space-between:向首尾对齐,子元素中间间隔相等,两端没有间隔,(如果发现两端有间隔,可以看看父元素有没有伪类)。
    在这里插入图片描述

  • space-around:空白在周围均匀分布
    在这里插入图片描述
    2.align-items:对齐元素在辅轴方向的对齐方式

  • stretch:拉伸填满整个容器(子元素不能有height和min-height的束缚)
    在这里插入图片描述

  • flex-start:横向的时候就是顶部对齐
    在这里插入图片描述

  • flex-end:横向的时候就是底部对齐
    在这里插入图片描述

  • center:居中对齐
    在这里插入图片描述

  • baseline:以子元素的第一行文字对齐的
    在这里插入图片描述
    3.align-content:对齐多行内容再辅轴方向上的排列方式

  • 同justify-content的值作用相同
    4.flex-wrap:主轴一行显示不了的时候的换行策略

  • no-wrap不换行(默认)

  • wrap:换行
    在这里插入图片描述

  • wrap-reverse:反向换行,第一行在最下面

2.如何把view上的内容绘制到画布上,生成一张海报

1.先用wx.createCanvasContext创建一个画布,
2.在画布上绘制内容、文本或者是图片。
3.接着通过wx.canvasToTempFilePath保存到本地并获取一个临时图片路径
4.最后通过wx.saveImageToPhotosAlbum保存临时文件到本地相册里
这里只是一个简单的例子,如果想看内容丰富一点的例子,可以移步到这个链接[https://www.cnblogs.com/lingXie/p/10609530.html](https://www.cnblogs.com/lingXie/p/10609530.html)

在这里插入图片描述
保存后的图片
在这里插入图片描述

具体代码:
<view class="container">
<!-- 画布 -->
<!-- canvas -->
<canvas bindlongpress='baocun' canvas-id="myCanvas" style="width:300px;height:320px;position:fixed;top:40px;"></canvas>
<!-- 要保存至相册的图片 -->
</view>
//index.js
const app = getApp()

Page({
  data: {
    imagePath:''
  },

  onLoad: function() {
    // 调用画布
    // this.createNewImg();
    var that=this;
    setTimeout(function(){
      that.createNewImg(); 
    },500);
  },

   /*
   1.绘制画布
   2.将canvas转换为图片保存到本地,然后将图片路径传给image图片的src
   */
  createNewImg: function () {
    var that = this;
    var ctx = wx.createCanvasContext('myCanvas');
    //绘制文本
    //语法:fillText(text, x, y, [maxWidth]) strokeText(text, x, y, [maxWidth])
    ctx.font = "normal 18px Arial";
    ctx.textAlign = 'center';
    ctx.textBaseline = 'bottom';
    ctx.fillStyle = 'blue';
    ctx.fillText("我是一张图,长按可以保存", 150, 250);
    //图片绘制
    var img='../../mock/images/test.jpg';
    ctx.drawImage(img, 0, 0, 300, 200);
    ctx.save(); // 保存当前ctx的状态 ctx.restore();
    ctx.draw();
    setTimeout(function () {
      wx.canvasToTempFilePath({
        x: 0,
        y: 0,
        width: 300,
        height: 320,
        destWidth: 300 * 2,//生成图片的大小设置成canvas大小的二倍解决模糊
        destHeight: 320 * 2,
        canvasId: 'myCanvas',
        fileType: 'jpg',
        success: function (res) {
          var tempFilePath = res.tempFilePath;
          that.setData({
            imagePath: tempFilePath
          });
          wx.hideToast();
          console.log('canvas图片地址:' + that.data.imagePath);
        }
      });
    }, 600);
  },
   
  //点击保存到相册
  baocun: function () {
    var that = this;
    wx.saveImageToPhotosAlbum({
      filePath: that.data.imagePath,
      success(res) {
        wx.showModal({
          content: '图片已保存到相册,赶紧晒一下吧~',
          showCancel: false,
          confirmText: '好的',
          confirmColor: '#333',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定');
            }
          }, fail: function (res) {
            console.log(res)
          }
        })
      }
    })
  },
})

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常感谢您的提问。根据您的要求,我为您提供以下代码示例供参考。请注意,由于不知道具体的页面布局,因此我只提供了基本的代码示例,您需要根据实际情况进行调整。 ```html <view class="swiper-container"> <swiper class="swiper" indicator-dots="false" autoplay="true" interval="5000" duration="1000"> <swiper-item> <view class="flex-container"> <view class="flex-item"></view> <view class="flex-item item1"></view> <view class="flex-item item2"></view> <view class="flex-item"></view> </view> </swiper-item> <swiper-item> <view class="flex-container"> <view class="flex-item"></view> <view class="flex-item item3"></view> <view class="flex-item item4"></view> <view class="flex-item"></view> </view> </swiper-item> <swiper-item> <view class="flex-container"> <view class="flex-item"></view> <view class="flex-item item5"></view> <view class="flex-item item6"></view> <view class="flex-item"></view> </view> </swiper-item> </swiper> </view> ``` ```css .swiper-container { width: 100%; height: 200rpx; } .swiper { width: 100%; height: 100%; } .flex-container { display: flex; justify-content: space-between; align-items: center; height: 100%; } .flex-item { flex: 1; height: 100%; } .item1 { background-color: #ff5d5d; } .item2 { background-color: #ffaa00; } .item3 { background-color: #ffc107; } .item4 { background-color: #4caf50; } .item5 { background-color: #2196f3; } .item6 { background-color: #9c27b0; } ``` 在上面的代码中,我们使用了 swiper 组件实现轮播效果,并且结合 flex 布局实现图中所示的效果。我们使用了 3 个 swiper-item 元素,每个元素中包含一个 flex-container 元素和 4 个 flex-item 元素。其中,每个 flex-item 元素的宽度都是平均分配的,因此可以实现每个元素平均分的效果。通过为每个 flex-item 元素添加不同的样式,可以实现不同的背景色。 希望这个示例代码能够对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值