Implementation of overlapping icons in Android

Source: Internet
Author: User

When we are developing applications, especially image browsers or file browsers, involving image or file management, we will inevitably need to process them in batches and use the multiple choice function. How can we visually display the selected file to the user? We can change the font color of the file name to mark it, but it is not very obvious, it is better to display a check box on the file or image icon to select a table. In the layout file of Android, we use an imageview control to display an image. If you need to display a check mark on the image, you must implement it in the layout file, so we can only use absolute layout to overlap the two imageviews. This method is obviously not desirable and its adaptive capability is poor!

Here, we can use two methods:

First: Use canvas

Public void first (view v ){

// Prevent 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 (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 );
// Store the newly merged image
Canvas. Restore ();

Image. setimagebitmap (newbitmap );
}

 

Method 2: Use layerdrawable. The Code is as follows:

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 respectively.
La. setlayerinset (0, 0, 0, 0, 0 );
La. setlayerinset (1, 20, 20, 20, 20 );
Image. setimagedrawable (LA );
}

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.