Android Image Overlay effect-two ways to introduce and content, with resolution immutable bitmap passed to Canvas constructor error

Source: Internet
Author: User

The first is a canvas-drawn effect:

[Java]View Plaincopyprint?
  1. Public void First (View v) {
  2. //Prevent immutable bitmap passed to Canvas constructor error
  3. Bitmap Bitmap1 = Bitmapfactory.decoderesource (Getresources (),
  4. r.drawable.apple). Copy (Bitmap.Config.ARGB_8888, true);
  5. Bitmap bitmap2 = ((bitmapdrawable) getresources (). Getdrawable (
  6. R.drawable.go)). Getbitmap ();
  7. Bitmap newbitmap = null;
  8. Newbitmap = Bitmap.createbitmap (BITMAP1);
  9. Canvas canvas = new Canvas (NEWBITMAP);
  10. Paint paint = new paint ();
  11. int w = bitmap1.getwidth ();
  12. int h = bitmap1.getheight ();
  13. int w_2 = Bitmap2.getwidth ();
  14. int h_2 = Bitmap2.getheight ();
  15. Paint.setcolor (Color.gray);
  16. Paint.setalpha (125);
  17. Canvas.drawrect (0, 0, Bitmap1.getwidth (), Bitmap1.getheight (), paint);
  18. Paint = new paint ();
  19. Canvas.drawbitmap (BITMAP2, Math.Abs (w-w_2)/ 2,
  20. Math.Abs (h-h_2)/ 2, paint);
  21. Canvas.save (Canvas.all_save_flag);
  22. //storing pictures of new compositions
  23. Canvas.restore ();
  24. Image.setimagebitmap (NEWBITMAP);
  25. }
public void First (View v) {//Prevent immutable bitmap passed to Canvas constructor error bitmap Bitmap1 = BITMAPFA        Ctory.decoderesource (Getresources (), r.drawable.apple). Copy (Bitmap.Config.ARGB_8888, true);        Bitmap bitmap2 = ((bitmapdrawable) getresources (). getdrawable (R.drawable.go)). Getbitmap ();        Bitmap newbitmap = null;        Newbitmap = Bitmap.createbitmap (BITMAP1);        Canvas canvas = new canvas (NEWBITMAP);        Paint paint = new paint ();        int w = bitmap1.getwidth ();        int h = bitmap1.getheight ();        int w_2 = Bitmap2.getwidth ();        int h_2 = Bitmap2.getheight ();        Paint.setcolor (Color.gray);        Paint.setalpha (125);        Canvas.drawrect (0, 0, bitmap1.getwidth (), Bitmap1.getheight (), paint);        Paint = new paint ();        Canvas.drawbitmap (BITMAP2, Math.Abs (w-w_2)/2, Math.Abs (h-h_2)/2, paint);        Canvas.save (Canvas.all_save_flag); Storing pictures of new compositions canvas.resTore ();    Image.setimagebitmap (NEWBITMAP); }

Canvas canvas = new Canvas (NEWBITMAP); When you create a canvas with Newbitmap, the operation is already implemented on the diagram.

This example can be done to start playing the effect of the player, calculate the middle position, first cover a layer of transparent gray square, and then in the middle of the picture of the button to play.

The second is to use the system's Layerdrawable class, which is primarily used to control the combination of multiple images:

[Java]View Plaincopyprint?
  1. Public void Second (View v) {
  2. Bitmap Bitmap1 = ((bitmapdrawable) getresources (). Getdrawable (
  3. r.drawable.apple)). Getbitmap ();
  4. Bitmap bitmap2 = ((bitmapdrawable) getresources (). Getdrawable (
  5. R.drawable.go)). Getbitmap ();
  6. drawable[] Array = new drawable[2];
  7. array[0] = new bitmapdrawable (BITMAP1);
  8. array[1] = new bitmapdrawable (BITMAP2);
  9. Layerdrawable la = new layerdrawable (array);
  10. ///Where the first parameter is the index number of the layer, and the next four parameters are left, top, right, and bottom
  11. La.setlayerinset (0, 0, 0, 0, 0);
  12. La.setlayerinset (1, 20);
  13. Image.setimagedrawable (LA);
  14. }
public void second (View v) {        Bitmap Bitmap1 = ((bitmapdrawable) getresources (). getdrawable (                r.drawable.apple)) . Getbitmap ();        Bitmap bitmap2 = ((bitmapdrawable) getresources (). getdrawable (                r.drawable.go)). Getbitmap ();        drawable[] array = new DRAWABLE[2];        Array[0] = new bitmapdrawable (BITMAP1);        ARRAY[1] = new bitmapdrawable (BITMAP2);        Layerdrawable la = new layerdrawable (array);        The first parameter is the index number of the layer, and the following four parameters are left, top, right, and bottom        la.setlayerinset (0, 0, 0, 0, 0);        La.setlayerinset (1, A, A, a);        Image.setimagedrawable (LA);    }

Associative arrays array, controlling the position of each layer

Loaded from: http://blog.csdn.net/gh102/article/details/6719624

Note: The above prevents immutable bitmap passed to Canvas constructor errors

The reason is that if you do not use the Copy method, the direct reference will modify the resource file, and Android is not allowed in the code to modify the image in the res file

Android Image Overlay effect-two ways to introduce and content, with resolution immutable bitmap passed to Canvas constructor error

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.