Android Image Overlay effect-two ways

Source: Internet
Author: User

The first type:

The second type:

The first is a canvas-drawn effect:

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:

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

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 Picture Overlay effect--two ways

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.