Two ways to realize the overlay effect of the Android image _android

Source: Internet
Author: User
Tags abs

This paper illustrates two ways to realize the overlay effect of Android image. Share to everyone for your reference, specific as follows:

Effect Chart:

First type:

The second type:

The first is the effect that is drawn through the canvas:

public void A (View v) {//Prevent immutable bitmap passed to Canvas constructor error bitmap Bitmap1 = bitmapfactory
    . 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);
    Store the newly synthesized picture canvas.restore ();
Image.setimagebitmap (NEWBITMAP);

 }

Canvas Canvas = new Canvas (NEWBITMAP); When the canvas is created with NEWBITMAP, the operation is already implemented on the diagram.

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

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);
    Image.setimagedrawable (LA);


Associative 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 to modify the image in the res file in the code.

Layer-list can cascade multiple pictures in order.

Syntax: in Drawalbe/drawable-layer.xml

<layer-list xmlns:android= "Http://schemas.android.com/apk/res/android" >
  <item android:drawable= "@ Android:color/white "/>
  <item android:drawable=" @drawable/logo_overlay "/>
</layer-list>

Use the same way as other pictures:

As referenced in styles:

<resources>
  <style name= "Lookuptheme" parent= "@Android: Style/theme.light.notitlebar" >
    < Item Name= "Android:windowbackground" > @drawable/drawable-layer</item>
  </style>
</ Resources>

Referencing in Java code:

Copy Code code as follows:
((ImageView) Findviewbyid (R.id.imageview)). Setimagedrawable (Getresources (). Getdrawable ( R.drawable.drawable-layer)

Implemented in code:

Resources of resources = Getresources ();
Drawable[] layers = new drawable[2];
Layers[0] = r.getdrawable (r.drawable.white);
LAYERS[1] = r.getdrawable (r.drawable.logo_overlay);
Layerdrawable layerdrawable = new Layerdrawable (layers)
(ImageView) Findviewbyid (R.id.imageview)). Setimagedrawable (layerdrawable);

Let me cite one more example:

To overlay with 2 pictures, it looks like a picture.

First, create a drawalbe/login_head.xml file that reads as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <layer-list xmlns:android=
 "http://schemas.android.com/" Apk/res/android ">
  <item android:id=" @+id/user_faceback_drawable "android:drawable=" @drawable/faceback " />
  <item android:id= "@+id/user_face_drawable" android:drawable= "@drawable/h001" android:left= "10.0dip" android:top= "18.0dip" android:right= "25.0dip" android:bottom= "35.0dip"/>
</layer-list>

You can then use this combination of pictures, such as in the ImageView display:

<imageview android:id= "@+id/faceimg" android:background= "@drawable/login_head" android:layout_width= "Wrap
_content "android:layout_height=" Wrap_content "/>

Using Java code:

How do I make multiple drawable overlay (composite picture)?

You may know that bitmap stack processing in the Android platform can be canvas a layer of painting on the line, and drawable how to deal with it? In addition to the use of Bitmapdrawable Getbitmap method will be drawable converted to bitmap, today Android123 to everyone to say easy with simple layerdrawable class, Layerdrawable is a layer drawing object as its name suggests. The following is represented directly by a simple code:

Bitmap BM = Bitmapfactory.decoderesource (Getresources (), R.DRAWABLE.CWJ);
drawable[] array = new DRAWABLE[3];
Array[0] = new paintdrawable (color.black); Black
array[1] = new paintdrawable (color.white);//white
array[2] = new bitmapdrawable (BM);//bitmap Resource
Layerdrawable ld = new layerdrawable (array); The parameters are above the drawable array
ld.setlayerinset (1, 1, 1, 1, 1);//The first parameter 1 represents the second element of the array, the white
ld.setlayerinset (2, 2, 2, 2, 2); The first parameter 2 represents the third element of the array, the bitmap resource
mimageview.setimagedrawable (LD);

The above method layerdrawable is the key, the Android Development Network prompts Setlayerinset method prototype for public void Setlayerinset (int index, int l, int t, int r, int b) One parameter is the index number of the layer, and the following four parameters are left, top, right, and bottom. For simple image synthesis, we can convert the first and second layers of paintdrawable into bitmapdrawable to achieve a simple picture synthesis.

For more information on Android-related content readers can view the site: "Android graphics and image processing skills summary", "Android Development introduction and Advanced Course", "Android debugging techniques and common problems solution summary", " Android Multimedia How-to Summary (audio, video, audio, etc), summary of Android Basic components usage, Android View tips Summary, Android layout layout tips and a summary of Android controls usage

I hope this article will help you with the Android program.

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.