Matlab绘制像素风字母颜色及透明度随机变化动画

本文是使用 Matlab 绘制像素风字母颜色及透明度随机变化动画的教程

实现效果

请添加图片描述

实现代码

如果需要更改为其他字母组合,在下面代码的基础上简单修改就可以使用。
步骤:(1) 定义字母形状;(2) 给出字母组合顺序;(3) 重新运行程序;

Matlab源文件代码也已经上传,下载链接就在文章头部的位置。
在这里插入图片描述

% 定义字母的形状
D_single = [
    1 1 1 0 0;
    1 0 0 1 0;
    1 0 0 0 1;
    1 0 0 0 1;
    1 0 0 0 1;
    1 0 0 1 0;
    1 1 1 0 0
];

G_single = [
    0 1 1 1 0;
    1 0 0 0 1;
    1 0 0 0 0;
    1 0 1 1 1;
    1 0 0 0 1;
    1 0 0 0 1;
    0 1 1 1 1
];

O_single = [
    0 1 1 1 0;
    1 0 0 0 1;
    1 0 0 0 1;
    1 0 0 0 1;
    1 0 0 0 1;
    1 0 0 0 1;
    0 1 1 1 0
];

% 存储需要显示的字母形状的单元格数组
letters = {G_single, O_single, O_single, D_single};

% 设置参数
blockSize = 10; % 每个方格的大小
rows = 7;       % 字母'A'的行数
cols = 5;       % 字母'A'的列数
numLetters = length(letters); % 插入的字母数量
extraCols = 10; % 额外增加的背景栅格列数
numFrames = 50; % 动画总帧数
fps = 3;        % 动画帧率
scaleFactor = 5;  % 窗口大小缩放系数,可调整以更好地控制窗口大小
gifFilename = 'animated_letters.gif'; % 输出的GIF文件名

% 定义两个字母之间的间隔
spaceCols = 1;  % 间隔1列

% 计算总列数
totalCols = numLetters * cols + (numLetters-1) * spaceCols + extraCols;  % 增加背景栅格的列数

% 计算左右两侧额外的空列数,以居中字母
leftPadding = floor(extraCols / 2);
rightPadding = extraCols - leftPadding;

% 初始化整个矩阵
A = zeros(rows, totalCols); % 初始化全为0的矩阵

% 在矩阵中插入numLetters个字母,,并居中
% 如果n小于1,退出程序,并打印警告信息
if numLetters < 1
    disp('Warning: n must be greater than or equal to 1.');
    return;
end

for k = 1:numLetters
    singleLetter = letters{k};
    colStart = leftPadding + (k - 1) * (cols + spaceCols) + 1;
    colEnd = leftPadding + (k - 1) * (cols + spaceCols) + cols;
    A(:, colStart:colEnd) = singleLetter;
end

% 初始化图形窗口,设置合适的窗口大小
windowWidth = blockSize * totalCols * scaleFactor + 20;  % 绘图窗口宽度
windowHeight = blockSize * rows * scaleFactor + 20;      % 绘图窗口高度
figure('Units', 'pixels', 'Position', [100 100 windowWidth windowHeight]);
axis equal;
hold on;
axis off; % 隐藏坐标轴

% 设置紧凑布局,移除边缘空白
set(gca, 'Units', 'normalized', 'Position', [0 0 1 1]);

% 设置浅灰色背景
set(gca, 'Color', [0.9 0.9 0.9]);  % 0.9是浅灰色

% 绘制浅色栅格背景
lightGrayColor = [0.8, 0.8, 0.8];  % 浅灰色
for i = 0:rows
    y = i * blockSize;
    plot([0, totalCols * blockSize], [y, y], 'Color', lightGrayColor, 'LineWidth', 0.5); % 绘制横线
end
for j = 0:totalCols
    x = j * blockSize;
    plot([x, x], [0, rows * blockSize], 'Color', lightGrayColor, 'LineWidth', 0.5); % 绘制竖线
end

% 创建矩形句柄数组
hRect = zeros(rows, totalCols);

% 初始化矩形句柄和位置
for i = 1:rows
    for j = 1:totalCols
        if A(i, j) == 1
            x = (j - 1) * blockSize;
            y = (rows - i) * blockSize;
            hRect(i, j) = rectangle('Position', [x, y, blockSize, blockSize], ...
                                    'EdgeColor', 'w', 'LineWidth', 1);
        end
    end
end

% 动画循环
for frame = 1:numFrames
    for i = 1:rows
        for j = 1:totalCols
            if A(i, j) == 1
                % 随机生成新的颜色和透明度
                randomColor = rand(1, 3);
                randomAlpha = 0.5+ (1-0.5)*rand(); % 透明度在(0.5-1)之间变化
                % 更新方块的颜色和透明度
                set(hRect(i, j), 'FaceColor', randomColor,'FaceAlpha', randomAlpha);
            end
        end
    end
    
    % 捕获当前帧的图像
    frameImage = getframe(gcf);
    im = frame2im(frameImage); % 将帧转换为图像
    
    % 将图像写入GIF文件
    % 使用64种颜色而不是256种,以减小文件大小
    [imind, cm] = rgb2ind(im, 64);
    if frame == 1
        imwrite(imind, cm, gifFilename, 'gif', 'Loopcount', inf, 'DelayTime', 1/fps);
    else
        imwrite(imind, cm, gifFilename, 'gif', 'WriteMode', 'append', 'DelayTime', 1/fps);
    end
    
    % pause(1/fps);  % 控制matlab中显示的帧率,和输出的gif图像一致
end

hold off;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值