You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android图片转二维数组仅提取左上象限问题求助

解决Android过程式动画中仅显示图片左上象限的问题

我太懂这种卡在细节上的烦躁了——明明要处理整张720x720的图片,结果只提取了左上象限的像素还铺满了画布,肯定越看越别扭。咱们一步步拆解问题,找到修复的关键点。

常见问题根源

这种情况大概率是像素遍历范围错误或者图片加载/坐标计算逻辑出了偏差,常见的坑有这几个:

  • 遍历像素时,x或y的循环只覆盖了一半范围(比如误写了x < width/2)
  • 加载Bitmap时不小心用了采样压缩(比如inSampleSize=2),导致图片被缩成360x360
  • 像素坐标的索引计算搞反了宽高,或者用了错误的尺寸值

修复方案与示例代码

我给你整理了一套正确的实现逻辑,你可以对照自己的代码调整:

public class MainActivity extends AppCompatActivity {
    private Bitmap originalBitmap;
    private int[][] colorArray;
    private ImageView imageView;
    private Canvas drawCanvas;
    private Bitmap drawBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.image_view);

        // 1. 正确加载720x720图片(这里以资源文件为例,你也可以替换成其他加载方式)
        originalBitmap = loadOriginalBitmap();
        int bitmapWidth = originalBitmap.getWidth();
        int bitmapHeight = originalBitmap.getHeight();

        // 先打日志确认宽高是否真的是720x720,排除加载问题
        Log.d("PixelAnim", "图片宽高: " + bitmapWidth + "x" + bitmapHeight);

        // 2. 初始化对应大小的颜色二维数组
        colorArray = new int[bitmapHeight][bitmapWidth];

        // 3. 遍历整张图片的所有像素,提取颜色值
        for (int y = 0; y < bitmapHeight; y++) {
            for (int x = 0; x < bitmapWidth; x++) {
                colorArray[y][x] = originalBitmap.getPixel(x, y);
            }
        }

        // 4. 创建用于逐帧绘制的画布和Bitmap
        drawBitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
        drawCanvas = new Canvas(drawBitmap);
        imageView.setImageBitmap(drawBitmap);

        // 5. 启动逐像素绘制动画
        startPixelDrawAnimation(bitmapWidth, bitmapHeight);
    }

    // 封装图片加载逻辑,避免采样压缩导致的尺寸错误
    private Bitmap loadOriginalBitmap() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false; // 禁止系统自动缩放
        options.inSampleSize = 1; // 不采样,加载原图
        return BitmapFactory.decodeResource(getResources(), R.drawable.your_720x720_image, options);
    }

    private void startPixelDrawAnimation(int width, int height) {
        // 计算总像素数,用ValueAnimator控制绘制进度
        ValueAnimator animator = ValueAnimator.ofInt(0, width * height - 1);
        animator.setDuration(6000); // 6秒完成绘制,可自行调整
        animator.addUpdateListener(animation -> {
            int totalDrawn = (int) animation.getAnimatedValue();
            // 计算当前要绘制的像素坐标:x是总像素数取模宽度,y是总像素数除以宽度
            int currentX = totalDrawn % width;
            int currentY = totalDrawn / width;

            // 绘制单个像素
            Paint pixelPaint = new Paint();
            pixelPaint.setColor(colorArray[currentY][currentX]);
            drawCanvas.drawPoint(currentX, currentY, pixelPaint);

            // 更新ImageView显示
            imageView.invalidate();
        });
        animator.start();
    }
}

重点检查项

你可以对照自己的代码,重点排查这几个地方:

  • 确认图片加载时没有开启采样压缩(inSampleSize必须为1,且inScaled=false)
  • 检查像素遍历的循环条件:x要从0到width-1,y要从0到height-1,绝对不能出现width/2或者height/2的限制
  • 绘制时的坐标计算要正确:currentX = 总像素数 % 宽度,currentY = 总像素数 / 宽度,别搞反宽高

按照这个思路调整后,应该就能正常显示整张图片的逐像素动画了。

内容的提问来源于stack exchange,提问作者Zyx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:31:14