Flipping a bitmap in android?

I want to keep a memory for my game, and I wanted to ask you because I could not find anything, and the last time I asked something, I got a good answer. Can I flip a bitmap inside an eclipse to save memory for sprites? All the textbooks that I found dealt with rotation, not flipping. The bitmap translation tutorials were just for open Gl or something like that. Please help me. I was looking for tutorials on google, but I quit on page 5. Can someone help me? Does anyone have a good tutorial? By the way, I use canvas. Thanks!

I am going to forcefully close every time I try to start it ... can this be understood? here is my code:

Matrix flipHorizontalMatrix = new Matrix(); flipHorizontalMatrix.setScale(-1,1); flipHorizontalMatrix.postTranslate(0, canvas.getHeight()-arrowL.getHeight()); canvas.drawBitmap(arrowL, flipHorizontalMatrix, null); 

I want the arrow to be in the lower right corner.

+6
source share
2 answers

Since you are using Canvas , why not try the drawBitmap (Bitmap bitmap, Matrix matrix, Paint paint) method drawBitmap (Bitmap bitmap, Matrix matrix, Paint paint) . Use a Matrix that flips the x coordinates.

You can do something like this:

 Matrix flipHorizontalMatrix = new Matrix(); flipHorizontalMatrix.setScale(-1,1); flipHorizontalMatrix.postTranslate(myBitmap.getWidth(),0); canvas.drawBitmap(myBitmap, flipHorizontalMatrix, myPaint); 
+19
source

Thank you, check this code, it may be useful for you to rotate the bitmap image. Here I have an example of an aquarium fish, it should be moved from left to right and turned over and continue moving from right to left and vice versa. here is the code for you ..

  protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); fish = BitmapFactory.decodeResource(getResources(), R.drawable.fish); v = new OurView(this); 

public class OurView extends SurfaceView implements Runnable {

  Thread t = null; SurfaceHolder holder; boolean isitOK = false; String Flag = "right"; Bitmap rotatedBitmap=null; Matrix rotateRight = new Matrix(); Matrix rotateLeft = new Matrix(); Bitmap rSprite=null; Bitmap lSprite=null; public OurView(Context context) { super(context); holder = getHolder(); rotateLeft.setScale(-1, 1); rSprite = Bitmap.createBitmap(fish, 0, 0, fish.getWidth(), fish.getHeight(), rotateRight, true); lSprite = Bitmap.createBitmap(fish, 0, 0, fish.getWidth(), fish.getHeight(), rotateLeft, true); } @Override public void run() { // TODO Auto-generated method stub while (isitOK == true) { if (!holder.getSurface().isValid()) { continue; } Canvas canvas = holder.lockCanvas(); canvas.drawBitmap(bg, 0, 0, null); if(Flag == "right") canvas.drawBitmap(lSprite, x, y, null); if(Flag == "left") canvas.drawBitmap(fish, x, y, null); if (Flag == "right" && x <= 60) { x++; if (x == 60) { Flag = "left"; // canvas.drawBitmap(rSprite, 0, fish.getWidth(), null); canvas.drawBitmap(fish, x, y, null); } } if (Flag == "left" && x >= 0) { x--; if (x == 0) { Flag = "right"; canvas.drawBitmap(fish, x, y, null); } } holder.unlockCanvasAndPost(canvas); } } 
0
source

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


All Articles