Jigsaw Puzzle code-splice two images

Source: Internet
Author: User

Jigsaw Puzzle code-splice two images

Jigsaw Puzzle code -- splice two images:

OnCreate function:

 

    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                imageview=(ImageView) findViewById(R.id.imageview);        Bitmap background=BitmapFactory.decodeResource(getResources(), R.drawable.back);        Bitmap foreground=BitmapFactory.decodeResource(getResources(), R.drawable.plane);                /*Canvas canvas=new Canvas(background);        drawImage(canvas, background, 0, foreground.getHeight(),         foreground.getWidth(), background.getHeight()/2, 0, 0);*/                Bitmap bitmap=toConformBitmap(background, foreground);        imageview.setImageBitmap(bitmap);    }
Splicing function:

 

Method 1:

 

Private Bitmap toConformBitmap (Bitmap background, Bitmap foreground) {if (background = null) {return null;} int bgWidth = background. getWidth (); int bgHeight = background. getHeight (); int fgWidth = foreground. getWidth (); int fgHeight = foreground. getHeight (); // create a new Bitmap with the same length and width as SRC. Bitmap newbmp = Bitmap. createBitmap (bgWidth + fgWidth, bgHeight + fgHeight, Config. ARGB_8888); Canvas cv = new Canvas (newbmp); cv. drawBitmap (background, 0, 0, null); // start to draw bg cv at coordinates 0, 0. drawBitmap (foreground, 0, bgHeight, null); // draw fg at the coordinate of 0 and 0, and draw cv from any position. save (Canvas. ALL_SAVE_FLAG); // Save the cv. restore (); // store return newbmp ;}

Method 2:

 

 

Private Bitmap toConformBitmap (Bitmap background, Bitmap foreground) {if (background = null) {return null;} int bgWidth = background. getWidth (); int bgHeight = background. getHeight (); int fgWidth = foreground. getWidth (); int fgHeight = foreground. getHeight (); // create a new Bitmap with the same length and width as SRC. Bitmap newbmp = Bitmap. createBitmap (bgWidth + fgWidth, bgHeight + fgHeight, Config. ARGB_8888); Canvas cv = new Canvas (newbmp); // method 2 Rect dst = new Rect (); dst. left = 0; dst. top = 0; dst. right = bgWidth; dst. bottom = bgHeight-50; cv. drawBitmap (background, null, dst, null); Rect dst2 = new Rect (); dst2.left = 0; dst2.top = bgHeight; double Xscale = bgWidth/fgWidth; // zooming ratio of X axis dst2.bottom = (int) (bgHeight + fgHeight * Xscale); dst2.right = bgWidth; cv. drawBitmap (foreground, null, dst2, null); dst2 = null; dst = null; cv. save (Canvas. ALL_SAVE_FLAG); // Save the cv. restore (); // store return newbmp ;}
Save function:

 

 

// Save bitmap as an image private String saveBitmap (Bitmap bitmap) {String imagePath = getApplication (). getFilesDir (). getAbsolutePath () + "/temp.jpg"; File file = new File (imagePath); if (file. exists () {file. delete ();} try {FileOutputStream out = new FileOutputStream (file); if (bitmap. compress (Bitmap. compressFormat.. PNG, 100, out) {out. flush (); out. close () ;}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blockToast. makeText (this, "failed to save", 1 ). show (); e. printStackTrace ();} return imagePath ;}


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.