Combine two bitmaps and save as jpg format in android?

I have two bitmaps in my project, what I need is that I need to combine these two bitmaps and combine these bitmaps with one image. I will show my code

public class FotosurpriseActivity extends Activity { /** Called when the activity is first created. */ Bitmap overlay; Paint pTouch; int X = -100; int Y = -100; Canvas c2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.android); Bitmap mBitmapover = BitmapFactory.decodeResource(getResources(), R.drawable.ss); overlay = BitmapFactory.decodeResource(getResources(),R.drawable.ss).copy(Config.ARGB_8888, true); c2 = new Canvas(overlay); pTouch = new Paint(Paint.ANTI_ALIAS_FLAG); // pTouch.setXfermode(new PorterDuffXfermode(Mode.TARGET); pTouch.setColor(Color.TRANSPARENT); pTouch.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL)); setContentView(new BitMapView(this, mBitmap,mBitmapover)); } class BitMapView extends View { Bitmap mBitmap = null; Bitmap mBitmapover = null; public BitMapView(Context context, Bitmap bm, Bitmap bmover) { super(context); mBitmap = bm; mBitmapover = bmover; } @Override public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: { X = (int) ev.getX(); Y = (int) ev.getY(); invalidate(); break; } case MotionEvent.ACTION_MOVE: { X = (int) ev.getX(); Y = (int) ev.getY(); invalidate(); break; } case MotionEvent.ACTION_UP: break; } return true; } @Override protected void onDraw(Canvas canvas) { // called when view is drawn Paint paint = new Paint(); paint.setFilterBitmap(true); // The image will be scaled so it will fill the width, and the // height will preserve the imageรขโ‚ฌโ„ขs aspect ration /* double aspectRatio = ((double) mBitmap.getWidth()) / mBitmap.getHeight(); Rect dest = new Rect(0, 0, this.getWidth(),(int) (this.getHeight() / aspectRatio)); double aspectRatio2 = ((double) mBitmapover.getWidth()) / mBitmapover.getHeight(); Rect dest2 = new Rect(0, 0, this.getWidth(),(int) (this.getHeight() / aspectRatio2)); canvas.drawBitmap(mBitmap, null, dest, paint); canvas.drawBitmap(mBitmapover, null, dest2, paint); */ //draw background canvas.drawBitmap(mBitmap, 0, 0, null); //copy the default overlay into temporary overlay and punch a hole in it c2.drawBitmap(mBitmapover, 0, 0, null); //exclude this line to show all as you draw c2.drawCircle(X, Y, 80, pTouch); //draw the overlay over the background canvas.drawBitmap(overlay, 0, 0, null); } } } 

How can i do this?

+6
source share
1 answer

The onDraw function accesses the view. You really don't need to do this at all. You can do all this in the memory where the bitmaps are located. You also draw another bitmap in C2. You do not need to draw anything on the canvas in onDraw (), all that is needed is to draw it on the screen.

0
source

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


All Articles