ImageView of the UI component and its subclass (I) ImageView display image

Source: Internet
Author: User

ImageView of the UI component and its subclass (I) ImageView display image

Inheritance relationships of the ImageView family

ImageView inherited from View componentIts main function room displays images and Drawable objects.

The direct subclass of ImageView is ImageButton, QuickContactBadge, and the indirect subclass is ZoomButton. Therefore, all XML attributes and methods of ImageView can be used in ImageButton and ZoomButton.

 

The XML attributes of ImageView are as follows:

Android: adjustViewBounds: whether to adjust its own boundary to maintain the aspect ratio of the displayed image. It must be used together with maxWidth and MaxHeight. Otherwise, it will not work if used separately.

Android: cropToPadding: true. This component will be cropped to the padding that retains the ImageView.

Android: MaxHeight, android: maxWidth: sets the maximum height and width of the View. It is invalid to use it separately 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: tint: rendering an image to a specified color

Android: src: sets the ID of the Drawable object displayed in ImageView.

Android: scaleType: setScaleType (ImageView. ScaleType). ImageView. ScaleType is an internal class. It sets how to scale or move an image to adapt to the ImageView size. The common values are as follows:

Matrix (ImageView. ScaleType. MATRIC): uses the matric matrix for drawing scaling.

 

FitXY (ImageView. ScaleType. FIT_XY ):Horizontal and vertical scaling of imagesMake the image fully adapt to the ImageView, and the horizontal and vertical ratio of the image may change.

FitStart (ImageView. ScaleType. FIT_START): The image is scaled horizontally and vertically until the longer side is equal to the ImageView side.Upper left corner.

FitEnd (ImageView. ScaleType. FIT_END): The image is scaled horizontally and vertically until the longer side is equal to the ImageView side.Bottom right corner.

FitCenter (ImageView. ScaleType. FIT_CENTER): The image is scaled horizontally and vertically until the longer side is equal to the ImageView side.Center.

 

Center (ImageView. ScaleType. CENTER): place the image in the middle of ImageVIew without any scaling

CenterInside (ImageView. ScaleType. CENTER_INSIDE): keeps the image horizontally and vertically scaled so that the image can be fully displayed in the ImageView.

CenterCrop (ImageView. ScaleType. CENTER_CROP): keeps the vertical and horizontal ratio so that the image can completely overwrite the ImageView. You only need to display the shortest side of the image.

 

There are many ways to set image resources in ImageView:Bitmap, Drawable, Resource

SetImageBitmap (Bitmap btm); Use Bitmap to set the image displayed in this ImageView

SetImageDrawable (Drawable drawable); Use the Drawable object to set the image displayed in this ImageView. The Drawable object here is the actual Resource obtained by the Resource object, not the Resource ID.

SetImageResource (int reId); Use the image resource ID to set the image displayed in the ImageView

 

For example, the image browser sets the image transparency.

 

     
  
   "
   
   
   
   
   
       
      
  
 
 

Package com. hust. imageviewtest; import android. app. activity; import android. graphics. bitmap; import android. graphics. drawable. bitmapDrawable; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. motionEvent; import android. view. view; import android. view. view. onClickListener; import android. view. view. onTouchListener; import android. widget. button; import android. widget. imageView; public class MainActivity extends Activity {// defines an array int [] images = new int [] {R. drawable. lijiang, R. drawable. qiao, R. drawable. shuangta, R. drawable. shui, R. drawable. xiangbi,}; // defines the default image int currentImg = 2; // defines the initial transparency of the image private int alpha = 255; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); final Button plus = (Button) findViewById (R. id. button1); final Button minus = (Button) findViewById (R. id. button2); final Button next = (Button) findViewById (R. id. button3); final ImageView image1 = (ImageView) findViewById (R. id. imageView1); final ImageView image2 = (ImageView) findViewById (R. id. imageView2); // define the listener next for viewing the next image. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// controls ImageView to display the next image image1.setImageResource (images [++ currentImg % images. length]) ;}}); // defines the method for changing the image transparency. OnClickListener listener = new OnClickListener () {@ Overridepublic void onClick (View v) {if (v = plus) {alpha + = 20;} if (v = minus) {alpha-= 20;} if (alpha> = 255) {alpha = 255 ;} if (alpha <= 0) {alpha = 0 ;}// change the image transparency image1.setAlpha (alpha) ;}; // Add the listener plus for the two buttons. setOnClickListener (listener); minus. setOnClickListener (listener); listener (new OnTouchListener () {@ Overridepublic boolean onTouch (View view, MotionEvent event) {BitmapDrawable bitmapDrawable = (BitmapDrawable) image1.getDrawable (); // obtain the Bitmap bitmap = bitmapDrawable In the first image display box. getBitmap (); // double scale = bitmap. getWidth ()/320.0; // get the start point int x = (int) (event. getX () * scale); int y = (int) (event. getY () * scale); if (x + 120> bitmap. getWidth () {x = bitmap. getWidth ()-120;} if (y + 120> bitmap. getHeight () {y = bitmap. getHeight ()-120;} // display the image in the specified image2.setImageBitmap (Bitmap. createBitmap (bitmap, x, y, 120,120); image2.setAlpha (alpha); return false ;}) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Override public boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest. xml. int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}

 

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.