Zimgir
Zimgir

Reputation: 111

Translate bitmap with matrix

Hey I've got this problem in an app I'm making. I need to create a pre translated bitmap which means create a new bitmap from the old one where the pixels are shifted by a specified amount. I thought I could achieve that with a matrix with set post or pre translate but none of those move the pixels or do anything visible for that matter.

Functions such as rotate or scale do work on the bitmap and do what is expected. The translate also works on the canvas but it doesn't move the bitmap pixels which is what I want. Is it even possible to do it?

Am I doing something wrong why the translate functions don't work on the bitmap?

Here is a piece of code that shows what I'm doing.

private void move(float dx, float dy) {

    moveX = (float) (dx*complexScale); 
    moveY = (float) (dy*complexScale);

    //canvasMatrix.setTranslate(dx,dy);

    if(!render.isAlive()) {

        focusX = focusX0 - dx*complexScale;
        focusY = focusY0 - dy*complexScale;
        bitmapMatrix.setTranslate(dx, dy);
        needRefresh = true;
    }

}

private void generate() {

    if (!render.isAlive()) {

        render = new Thread(generator);
        fractal = Bitmap.createBitmap(fractal,0,0,mapWidth,mapHeight,bitmapMatrix,true);
        setComplexAxis();
        render.start();
    }

}

Upvotes: 1

Views: 1499

Answers (2)

Cassie
Cassie

Reputation: 5243

There are two ways to do translation. Below dx is the translation in the X axis, and dy is the translation in the Y axis. The other variables should be self explanatory.

1 - Translation within the image (without rotation)

val newBitmap = Bitmap.createBitmap(originalBitmap, dx, dy, newWidth, newHeight, matrix, false)

2 - Complex matrix

 matrix.postTranslate(dx, dy)

 val newBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888)

 val canvas = Canvas(newBitmap)
 canvas.drawBitmap(originalBitmap, matrix, null)

This is an old question but figured I would still answer to help those who stumble upon it like I did.

Upvotes: 1

user3806339
user3806339

Reputation: 144

Translation is done on the canvas. I am not entirely sure if you can just manipulate the pixel. I would recommend creating a view for the bitmap image and apply the translation on the canvas of the view

Upvotes: 0

Related Questions