Exploring the ImageView of Android controls

Source: Internet
Author: User

The ImageView control is an image control that displays images.

The following simulated mobile phone Image Viewer

Directory structure

Main. xml layout File

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<ImageView android: id = "@ + id/imageView"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal"
Android: src = "@ drawable/p1"/>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "horizontal"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal">
<Button android: id = "@ + id/previous"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "previous"
Android: layout_gravity = "center_horizontal"/>
<Button android: id = "@ + id/alpha_plus"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Increased transparency"
Android: layout_gravity = "center_horizontal"/>
<Button android: id = "@ + id/alpha_minus"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "reduced transparency"
Android: layout_gravity = "center_horizontal"/>
<Button android: id = "@ + id/next"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Next"
Android: layout_gravity = "center_horizontal"/>
</LinearLayout>
</LinearLayout>

ImageViewActivity class

Package com. ljq. iv;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import android. widget. ImageView;

Public class ImageViewActivity extends Activity {
Private ImageView imageView = null;
Private Button previous = null; // previous
Private Button next = null; // The next
Private Button alpha_plus = null; // increased transparency
Private Button alpha_minus = null; // reduced transparency
Private int currentImgId = 0; // record the image id displayed in the current ImageView
Private int alpha = 255; // record ImageView transparency
Int [] imgId = {// Image array displayed in ImageView
R. drawable. p1,
R. drawable. p2,
R. drawable. p3,
R. drawable. p4,
R. drawable. p5,
R. drawable. p6,
R. drawable. p7,
R. drawable. p8,
};

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

ImageView = (ImageView) findViewById (R. id. imageView );
Previous = (Button) findViewById (R. id. previous );
Next = (Button) findViewById (R. id. next );
Alpha_plus = (Button) findViewById (R. id. alpha_plus );
Alpha_minus = (Button) findViewById (R. id. alpha_minus );

Previous. setOnClickListener (listener );
Next. setOnClickListener (listener );
Alpha_plus.setOnClickListener (listener );
Alpha_minus.setOnClickListener (listener );
}

Private View. OnClickListener listener = new View. OnClickListener (){

Public void onClick (View v ){
If (v = previous ){
CurrentImgId = (currentImgId-1 + imgId. length) % imgId. length;
ImageView. setImageResource (imgId [currentImgId]);
}
If (v = next ){
CurrentImgId = (currentImgId + 1) % imgId. length;
ImageView. setImageResource (imgId [currentImgId]);
}
If (v = alpha_plus ){
Alpha + = 10;
If (alpha & gt; 255 ){
Alpha = 255;
}
ImageView. setAlpha (alpha );
}
If (v = alpha_minus ){
Alpha-= 10;
If (alpha <0 ){
Alpha = 0;
}
ImageView. setAlpha (alpha );
}
}

};

}

Running result

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.