Android Study Notes 15: image browser

Source: Internet
Author: User
Tags gety

This image browser can implement a predefined Image array in the browser program, and dynamically change the transparency of the image and view the partial details of the image. The imageview control is used, use setimagebitmap to change the image transparency and use setalphe to change the image transparency.

 

 

<? 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">
<LinearLayout
Android: orientation = "horizontal"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: gravity = "center">
<Button android: id = "@ + id/plus"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "increasing transparency"
/>
<Button android: id = "@ + id/minus"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "reducing transparency"
/>
<Button android: id = "@ + id/next"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Next"
/>
</LinearLayout>
<! -- Define the overall ImageView of the displayed image -->
<ImageView android: id = "@ + id/image1"
Android: layout_width = "fill_parent"
Android: layout_height = "240px"
Android: src = "@ drawable/sunyz_1"
Android: scaleType = "centerCrop"/> <! -- Scale up the image size in the center so that the image length (width) is equal to or greater than the View length (width) -->
<! -- Define the ImageView for displaying partial details of an image -->
<ImageView android: id = "@ + id/image2"
Android: layout_width = "120dp"
Android: layout_height = "120dp"
Android: background = "# 0000ff"
Android: layout_marginTop = "10dp"/>
</LinearLayout>
<? 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">
<LinearLayout
Android: orientation = "horizontal"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: gravity = "center">
<Button android: id = "@ + id/plus"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "increasing transparency"
/>
<Button android: id = "@ + id/minus"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "reducing transparency"
/>
<Button android: id = "@ + id/next"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Next"
/>
</LinearLayout>
<! -- Define the overall ImageView of the displayed image -->
<ImageView android: id = "@ + id/image1"
Android: layout_width = "fill_parent"
Android: layout_height = "240px"
Android: src = "@ drawable/sunyz_1"
Android: scaleType = "centerCrop"/> <! -- Scale up the image size in the center so that the image length (width) is equal to or greater than the View length (width) -->
<! -- Define the ImageView for displaying partial details of an image -->
<ImageView android: id = "@ + id/image2"
Android: layout_width = "120dp"
Android: layout_height = "120dp"
Android: background = "# 0000ff"
Android: layout_marginTop = "10dp"/>
</LinearLayout>

 

Package org. crazyit. imageview;
 
Import android. app. Activity;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. graphics. drawable. BitmapDrawable;
Import android. OS. Bundle;
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 ImageViewTest extends Activity
{
// Define an array for accessing images
Int [] images = new int [] {
R. drawable. sunyz_1,
R. drawable. sunyz_2,
R. drawable. sunyz_3,
R. drawable. sunyz_4,
R. drawable. sunyz_5,

};
// Define the default image
Int currentImg = 2;
// Define the initial transparency of the image
Private int alpha = 255;
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Final Button plus = (Button) findViewById (R. id. plus );
Final Button minus = (Button) findViewById (R. id. minus );
Final ImageView image1 = (ImageView) findViewById (R. id. image1 );
Final ImageView image2 = (ImageView) findViewById (R. id. image2 );
Final Button next = (Button) findViewById (R. id. next );
// Define the listener for viewing the next image
Next. setOnClickListener (new OnClickListener ()
{
@ Override
Public void onClick (View v)
{
If (currentImg> = 4)
{
CurrentImg =-1;
}
BitmapDrawable bitmapDrawable = (BitmapDrawable) image1
. GetDrawable ();
// If the image has not been recycled, forcibly recycle the image first.
If (! BitmapDrawable. getBitmap (). isRecycled ())
{
BitmapDrawable. getBitmap (). recycle ();
}
// Change the image displayed in the ImageView
Image1.setImageBitmap (BitmapFactory. decodeResource (getResources ()
, Images [++ currentImg]);
}
});
// Define a method for changing the image transparency
OnClickListener listener = new OnClickListener ()
{
@ Override
Public 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 listeners for two buttons
Plus. setOnClickListener (listener );
Minus. setOnClickListener (listener );
Image1.setOnTouchListener (new OnTouchListener ()
{
@ Override
Public boolean onTouch (View view, MotionEvent event)
{
BitmapDrawable bitmapDrawable = (BitmapDrawable) image1
. GetDrawable ();
// Obtain the bitmap in the first image display box
Bitmap bitmap = bitmapDrawable. getBitmap ();
// The actual bitmap image size and the scaling ratio of the first ImageView
Double scale = bitmap. Fig ()/280.0;
// Obtain the start point of the image to be displayed
Int x = (int) (event. getX () * scale );
Int y = (int) (event. getY () * scale );
// Int x = (int) (event. getX ());
// Int y = (int) (event. getY ());
If (x + 120> bitmap. getWidth ())
{
X = bitmap. getWidth ()-120;
}
If (y + 120> bitmap. getHeight ())
{
Y = bitmap. getHeight ()-120;
}
// Display the specified area of the image
Image2.setImageBitmap (Bitmap. createBitmap (bitmap, x, y, 120,120 ));
Image2.setAlpha (alpha );
Return false;
}
});
}
}


From the column hn307165411

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.