Android image superposition effect-two methods

Source: Internet
Author: User

:

First:

Second:

 

The first is the effect drawn through canvas:

Public void first (View v) {// prevents Immutable bitmap passed to Canvas constructor errors 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 H2 = bitmap2.getHeight (); paint. setColor (Color. GRAY); paint. setAlpha (1, 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-H2)/2, paint); canvas. save (Canvas. ALL_SAVE_FLAG); // stores the newly merged image canvas. restore (); image. setImageBitmap (newBitmap );}

 

Canvas canvas = new Canvas (newBitmap); when creating a Canvas using newBitmap, all operations are implemented on this graph.

In this example, the player starts playing the video. Calculate the intermediate position, overwrite a transparent Gray Square, and draw a playback button in the middle.

 

The second type is the system LayerDrawable class, which is mainly 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 layer index number, and the following four parameters are left, top, right, and bottom la. setLayerInset (0, 0, 0, 0, 0); la. setLayerInset (1, 20, 20, 20, 20); image. setImageDrawable (la );}

Associate array to control 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, direct reference will modify the resource file, while android does not allow you to modify the images in the res file in the code.

I don't know why I spent an hour testing code and writing this article, maybe for future data viewing, maybe for verifying the effect in my own mind, maybe because my mind suddenly becomes dull, I am listening to four season in one day.

Code: http://files.cnblogs.com/not-code/picture_overlay.zip

Address: http://www.cnblogs.com/not-code/archive/2011/08/18/2145126.html

This article is original. If you need to reprint it, please indicate the author and the source. Thank you!

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.