Flip Option to move on X or Y axis

This seems like a silly question, but I have not seen any way to do this using Drawable class methods. Then I thought, maybe I will have to turn the canvas over somehow. Still unable to find a suitable method.

I just need to "flip" the Drawable on it the y-axis. Preferably center y. How can i do this?

+4
source share
1 answer

From 10k foot, you want to create a new bitmap and specify a transform matrix to flip the bitmap.

It might be a bit overkill, but here is a small sample application that illustrates how to do this. As recorded, pre-scaling the transformation matrix (-1.0f, 1.0f) flips the image in the x direction, pre-scaling (1.0f, -1.0f) flips it in the y direction.

 public class flip extends Activity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Set view to our created view setContentView(new drawView(this)); } private class drawView extends View{ public drawView(Context context){ super(context); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //Load the jellyfish drawable Bitmap sprite = BitmapFactory.decodeResource(this.getResources(), R.drawable.jellyfish); //Create a matrix to be used to transform the bitmap Matrix mirrorMatrix = new Matrix(); //Set the matrix to mirror the image in the x direction mirrorMatrix.preScale(-1.0f, 1.0f); //Create a flipped sprite using the transform matrix and the original sprite Bitmap fSprite = Bitmap.createBitmap(sprite, 0, 0, sprite.getWidth(), sprite.getHeight(), mirrorMatrix, false); //Draw the first sprite canvas.drawBitmap(sprite, 0, 0, null); //Draw the second sprite 5 pixels to the right of the 1st sprite canvas.drawBitmap(fSprite, sprite.getWidth() + 5, 0, null); } } } 
+8
source

Source: https://habr.com/ru/post/1333747/


All Articles