Imageview for Android.

Source: Internet
Author: User

 

Imageview

First, determine the full path of the imageview: Android. widget. imageview.

Description:

Displays any image, sample mark. The imageview class can load images from various sources (such as resources or image libraries). You need to calculate the size of the image, so it can be used in other la S and provide such scaling and coloring (rendering) various display options.

Android: adjustviewbounds
Whether to maintain the aspect ratio. It must be used together with maxwidth and maxheight. Otherwise, it will not work if used separately.
 
Android: croptopadding
Specifies whether to intercept the specified area and use blank space instead. The setting is ineffective and must be used with scrolly. The effect is as follows. For the implementation code, see the code section:

 

Android: maxheight
Set the maximum height of the view, which is invalid for separate use and must be used with setadjustviewbounds. If you want to set a fixed image size and keep the image aspect ratio, you need to set it as follows:

1) set setadjustviewbounds to true;

2) Set maxwidth and maxheight;

3) set layout_width and layout_height to wrap_content.
 
Android: maxwidth
Set the maximum width of the view. Same as above.
 
Android: scaletype
Set the filling mode of the image.

Android: SRC
Set the drawable (slice or color, but you must specify the size of the view) of the view)
 
Android: tint
Render the image to a specified color.

 

Sample Code:

Switch the image when you click the image and darken the other image (mainly used for image transparency)

[Java: nogutter]
View plaincopyprint?
  1. Package cn.com. chenzheng_java;
  2. Import Android. App. activity;
  3. Import Android. content. res. Resources;
  4. Import Android. Graphics. drawable. drawable;
  5. Import Android. OS. Bundle;
  6. Import Android. View. view;
  7. Import Android. widget. Button;
  8. Import Android. widget. imageview;
  9. Import Android. widget. imageview. scaletype;
  10. /**
  11. *
  12. * @ Author chenzheng_java
  13. *
  14. */
  15. Public class imageviewactivity extends activity {
  16. Private imageview image1;
  17. Private imageview image2;
  18. @ Override
  19. Protected void oncreate (bundle savedinstancestate ){
  20. Super. oncreate (savedinstancestate );
  21. Setcontentview (R. layout. imageview );
  22. Image1 = (imageview) findviewbyid (R. Id. imge1 );
  23. Image2 = (imageview) findviewbyid (R. Id. imge2 );
  24. // Resetbutton = (button) findviewbyid (R. Id. resetbutton );
  25. Final int [] IDs = new int [] {
  26. R. drawable. red_a,
  27. R. drawable. blackk,
  28. R. drawable. Beauty
  29. };
  30. Image1.setonclicklistener (New View. onclicklistener (){
  31. @ Override
  32. Public void onclick (view v ){
  33. Int id = IDs [0];
  34. Image1.setscaletype (scaletype. center); // you can specify the editing mode.
  35. // Set the background of the imageview. If an image already exists, the background will be overwritten.
  36. // Image1.setbackgroundresource (ID );
  37. // Set the image displayed on the imageview
  38. Image1.setimageresource (ID );
  39. Image2.setimageresource (R. drawable. Icon );
  40. Image2.setalpha (100); // sets the transparency. If the value is 255, no transparency is set.
  41. }
  42. });
  43. }
  44. }

Package cn.com. chenzheng_java; </P> <p> Import android. app. activity; <br/> Import android. content. res. resources; <br/> Import android. graphics. drawable. drawable; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. widget. button; <br/> Import android. widget. imageview; <br/> Import android. widget. imageview. scaletype; <br/>/** <br/> * @ author chenzheng_java <br/> */<br/> public class imageviewactivity extends Activity {</P> <p> private imageview image1; <br/> private imageview image2; <br/> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. imageview); </P> <p> image1 = (imageview) findviewbyid (R. id. imge1); <br/> image2 = (imageview) findviewbyid (R. id. imge2); <br/> // resetbutton = (button) findviewbyid (R. id. resetbutton); </P> <p> final int [] IDs = new int [] {<br/> r. drawable. red_a, <br/> r. drawable. blackk, <br/> r. drawable. beauty <br/>}; </P> <p> image1.setonclicklistener (new view. onclicklistener () {<br/> @ override <br/> Public void onclick (view v) {<br/> int id = IDs [0]; <br/> image1.setscaletype (scaletype. center); // set the editing mode </P> <p> // set the background of the imageview. If an image already exists, the background will be overwritten <br/> // image1.setbackgroundresource (ID); </P> <p> // set the image displayed on the imageview <br/> image1.setimageresource (ID ); </P> <p> image2.setimageresource (R. drawable. icon); <br/> image2.setalpha (100); // sets the transparency. If the value is 255, no transparency is displayed. </P> <p >}< br/> }); </P> <p >}< br/>

-----------------------------------------------------------

How to Set an image for imageview is as follows:

Three methods to set the image source
Imageview IV;

String filename = "/data/COM. Test/aa.png;
Bitmap Bm = bitmapfactory. decodefile (filename );
Iv. setimagebitmap (BM );

Imageview IV = new imageview (context );
Iv. setimageresource (IV [position]);
Iv. setscaletype (imageview. scaletype. fit_xy );
Iv. setlayoutparams (new gallery. layoutparams (136,88 ));

Mimageview = (imageview) This. findviewbyid (R. Id. myimageview1 );
Mimageview. setimagedrawable (getresources (). getdrawable (R. drawable. Right ));

Imageview

First, determine the full path of the imageview: Android. widget. imageview.

Description:

Displays any image, sample mark. The imageview class can load images from various sources (such as resources or image libraries). You need to calculate the size of the image, so it can be used in other la S and provide such scaling and coloring (rendering) various display options.

Android: adjustviewbounds
Whether to maintain the aspect ratio. It must be used together with maxwidth and maxheight. Otherwise, it will not work if used separately.
 
Android: croptopadding
Specifies whether to intercept the specified area and use blank space instead. The setting is ineffective and must be used with scrolly. The effect is as follows. For the implementation code, see the code section:

 

Android: maxheight
Set the maximum height of the view, which is invalid for separate use and must be used with setadjustviewbounds. If you want to set a fixed image size and keep the image aspect ratio, you need to set it as follows:

1) set setadjustviewbounds to true;

2) Set maxwidth and maxheight;

3) set layout_width and layout_height to wrap_content.
 
Android: maxwidth
Set the maximum width of the view. Same as above.
 
Android: scaletype
Set the filling mode of the image.

Android: SRC
Set the drawable (slice or color, but you must specify the size of the view) of the view)
 
Android: tint
Render the image to a specified color.

 

Sample Code:

Switch the image when you click the image and darken the other image (mainly used for image transparency)

[Java: nogutter]
View plaincopyprint?
  1. Package cn.com. chenzheng_java;
  2. Import Android. App. activity;
  3. Import Android. content. res. Resources;
  4. Import Android. Graphics. drawable. drawable;
  5. Import Android. OS. Bundle;
  6. Import Android. View. view;
  7. Import Android. widget. Button;
  8. Import Android. widget. imageview;
  9. Import Android. widget. imageview. scaletype;
  10. /**
  11. *
  12. * @ Author chenzheng_java
  13. *
  14. */
  15. Public class imageviewactivity extends activity {
  16. Private imageview image1;
  17. Private imageview image2;
  18. @ Override
  19. Protected void oncreate (bundle savedinstancestate ){
  20. Super. oncreate (savedinstancestate );
  21. Setcontentview (R. layout. imageview );
  22. Image1 = (imageview) findviewbyid (R. Id. imge1 );
  23. Image2 = (imageview) findviewbyid (R. Id. imge2 );
  24. // Resetbutton = (button) findviewbyid (R. Id. resetbutton );
  25. Final int [] IDs = new int [] {
  26. R. drawable. red_a,
  27. R. drawable. blackk,
  28. R. drawable. Beauty
  29. };
  30. Image1.setonclicklistener (New View. onclicklistener (){
  31. @ Override
  32. Public void onclick (view v ){
  33. Int id = IDs [0];
  34. Image1.setscaletype (scaletype. center); // you can specify the editing mode.
  35. // Set the background of the imageview. If an image already exists, the background will be overwritten.
  36. // Image1.setbackgroundresource (ID );
  37. // Set the image displayed on the imageview
  38. Image1.setimageresource (ID );
  39. Image2.setimageresource (R. drawable. Icon );
  40. Image2.setalpha (100); // sets the transparency. If the value is 255, no transparency is set.
  41. }
  42. });
  43. }
  44. }

Package cn.com. chenzheng_java; </P> <p> Import android. app. activity; <br/> Import android. content. res. resources; <br/> Import android. graphics. drawable. drawable; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. widget. button; <br/> Import android. widget. imageview; <br/> Import android. widget. imageview. scaletype; <br/>/** <br/> * @ author chenzheng_java <br/> */<br/> public class imageviewactivity extends Activity {</P> <p> private imageview image1; <br/> private imageview image2; <br/> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. imageview); </P> <p> image1 = (imageview) findviewbyid (R. id. imge1); <br/> image2 = (imageview) findviewbyid (R. id. imge2); <br/> // resetbutton = (button) findviewbyid (R. id. resetbutton); </P> <p> final int [] IDs = new int [] {<br/> r. drawable. red_a, <br/> r. drawable. blackk, <br/> r. drawable. beauty <br/>}; </P> <p> image1.setonclicklistener (new view. onclicklistener () {<br/> @ override <br/> Public void onclick (view v) {<br/> int id = IDs [0]; <br/> image1.setscaletype (scaletype. center); // set the editing mode </P> <p> // set the background of the imageview. If an image already exists, the background will be overwritten <br/> // image1.setbackgroundresource (ID); </P> <p> // set the image displayed on the imageview <br/> image1.setimageresource (ID ); </P> <p> image2.setimageresource (R. drawable. icon); <br/> image2.setalpha (100); // sets the transparency. If the value is 255, no transparency is displayed. </P> <p >}< br/> }); </P> <p >}< br/>

-----------------------------------------------------------

How to Set an image for imageview is as follows:

Three methods to set the image source
Imageview IV;

String filename = "/data/COM. Test/aa.png;
Bitmap Bm = bitmapfactory. decodefile (filename );
Iv. setimagebitmap (BM );

Imageview IV = new imageview (context );
Iv. setimageresource (IV [position]);
Iv. setscaletype (imageview. scaletype. fit_xy );
Iv. setlayoutparams (new gallery. layoutparams (136,88 ));

Mimageview = (imageview) This. findviewbyid (R. Id. myimageview1 );
Mimageview. setimagedrawable (getresources (). getdrawable (R. drawable. Right ));

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.