ImageView: Zoom in, zoom in, and rotate images,

Source: Internet
Author: User

ImageView: Zoom in, zoom in, and rotate images,
ImageView image enlargement, downsize, and Rotation

I. Introduction

 

Ii. Method

1) set the image zoom-in and zoom-out effect

Step 1: Set android: scaleType in the <ImageView> label to "fitCenter"

Android: scaleType = "fitCenter"

Step 2: Get the screen width

DisplayMetrics dm = new DisplayMetrics ();
GetWindowManager (). getDefaultDisplay (). getMetrics (dm );

Dm. widthPixels

Step 3: set the maximum progree value of seekBar to the screen width.

Sb_one.setMax (dm. widthPixels );

Step 4: Set the layout parameter of the imageview, that is, width and height, that is, the width and height of the canvas.

Int width = progress;
Int height = progress * 3/4;

Iv_pic.setLayoutParams (new LinearLayout. LayoutParams (width, height ));

 

2) set the image rotation method

Step 1: Set the angle for matrix for the new bitmap

Private Matrix matrix;

Matrix = new Matrix ();

Matrix. setRotate (int) (progress SS * 3.60 ));

Step 2: Obtain bitmap Resources

BitmapDrawable bitmapDrawable = (BitmapDrawable) (getResources (). getDrawable (R. drawable. image1 ));
Bitmap bitmap = bitmapDrawable. getBitmap ();

Step 3: rebuild bitmap for display

Bitmap newBitmap = bitmap. createBitmap (bitmap, 0, 0, bitmap. getWidth (), bitmap. getHeight (), matrix, false );

Step 4: set a new bitmap for imageview

Iv_pic.setImageBitmap (newBitmap );

 

Iii. code example

:

Set the size and Rotation

Code:

Fry. Activity02

1 package fry; 2 3 import com. example. iamgeViewDemo1.R; 4 5 import android. app. activity; 6 import android. graphics. bitmap; 7 import android. graphics. matrix; 8 import android. graphics. drawable. bitmapDrawable; 9 import android. OS. bundle; 10 import android. util. displayMetrics; 11 import android. view. viewGroup. layoutParams; 12 import android. widget. imageView; 13 import android. widget. linearLayout; 14 import android. widget. seekBar; 15 import android. widget. seekBar. onSeekBarChangeListener; 16 17 public class Activity02 extends Activity implements OnSeekBarChangeListener {18 private ImageView iv_pic; 19 private SeekBar sb_one; 20 private SeekBar sb_two; 21 private Matrix matrix Matrix; 22 23 @ Override24 protected void onCreate (Bundle savedInstanceState) {25 // TODO Auto-generated method stub26 setTitle ("imageView Resize and rotate images"); 27 super. onCreate (savedInstanceState); 28 setContentView (R. layout. activity02); 29 30 iv_pic = (ImageView) findViewById (R. id. iv_pic); 31 sb_one = (SeekBar) findViewById (R. id. sb_one); 32 sb_two = (SeekBar) findViewById (R. id. sb_two); 33 // set the progress value of SeekBar to change the listening event 34 sb_one.setOnSeekBarChangeListener (this); 35 sb_two.setOnSeekBarChangeListener (this); 36 37 matrix = new Matrix (); 38 39 // 1) set the image size to 40 // 41 // Step 1: Set the android in the <ImageView> tab: set scaleType to "fitCenter" 42 // 43 // Step 2: Get the screen width 44 // 45 // Step 3: set the maximum progree value of seekBar to screen width 46 // 47 // Step 4: Set the layout parameter of imageview, that is, width and height, that is, the width and height of the canvas are 48 49 50 51 // set the image to zoom in and out 52 // step 1: Get the screen width 53 DisplayMetrics dm = new DisplayMetrics (); 54 getWindowManager (). getdefadisplay display (). getMetrics (dm); 55 // Step 2: set the maximum progree value of seekBar to 56 sb_one.setMax (dm. widthPixels); 57} 58 @ Override59 public void onProgressChanged (SeekBar seekBar, int progress, 60 boolean fromUser) {61 // TODO Auto-generated method stub62 switch (seekBar. getId () {63 case R. id. sb_one: // zoom in or out 64 int width = progress; 65 int height = progress * 3/4; 66 // Step 3: Set the layout parameter of imageview, that is, width and height, that is, 67 iv_pic.setLayoutParams (new LinearLayout. layoutParams (width, height); 68 break; 69 case R. id. sb_two: // rotate 70 // set the Rotation Degree 71 // set the image rotation method 72 // Step 1: Set the angle for the matrix for the new bitmap73 matrix. setRotate (int) (progress * 3.60); 74 // Step 2: Get bitmap resource 75 BitmapDrawable bitmapDrawable = (BitmapDrawable) (getResources (). getDrawable (R. drawable. image1); 76 Bitmap bitmap = bitmapDrawable. getBitmap (); 77 // Step 3: rebuild bitmap to display 78 Bitmap newBitmap = bitmap. createBitmap (bitmap, 0, 0, bitmap. getWidth (), bitmap. getHeight (), matrix, false); 79 // Step 4: set a new bitmap80 iv_pic.setImageBitmap (newBitmap); 81 break; 82 default: 83 break; 84} 85 86 87 88} 89 @ Override90 public void onStartTrackingTouch (SeekBar seekBar) {91 // TODO Auto-generated method stub92 93} 94 @ Override95 public void onStopTrackingTouch (SeekBar seekBar) {96 // TODO Auto-generated method stub97 98} 99}

Activity02.xml

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: orientation = "vertical"> 6 7 <ImageView 8 android: id = "@ + id/iv_pic" 9 android: layout_width = "match_parent" 10 android: layout_height = "300dip" 11 android: background = "@ android: color/black" 12 android: scaleType = "fitCenter" 13 android: src = "@ drawable /Image1 "14/> 15 <! -- Set the display mode of the image: scale up or down the image to the width of the view, center display --> 16 <SeekBar17 android: id = "@ + id/sb_one" 18 android: layout_width = "match_parent" 19 android: layout_height = "wrap_content" 20 android: progress = "100" 21/> 22 23 <TextView 24 android: layout_width = "match_parent" 25 android: layout_height = "wrap_content" 26 android: text = "drag to zoom in and out the image" 27/> 28 <SeekBar 29 android: id = "@ + id/sb_two" 30 android: layout_width = "match_parent" 31 android: layout_height = "wrap_content" 32/> 33 <TextView 34 android: layout_width = "match_parent" 35 android: layout_height = "wrap_content" 36 android: text = "dragging to rotate an image" 37/> 38 39 </LinearLayout>

 

Iv. Gains

1. Set image center display

Android: scaleType = "fitCenter"

2. Matrix object

Private Matrix matrix;

In Android, if you have used Matrix for image processing, you must know the Matrix class. The Matrix in android is a 3x3 Matrix.

In Android development, matrices are powerful and widely used artifacts. For example, they are used to create animation effects, change the image size, and add various filters to images.

3. DisplayMetrics object for display of screen size and so on

DisplayMetrics dm = new DisplayMetrics ();

4. Obtain the screen width of the mobile phone.

DisplayMetrics dm = new DisplayMetrics ();
GetWindowManager (). getDefaultDisplay (). getMetrics (dm );

Sb_one.setMax (dm. widthPixels );

5. Use LinearLayout to set imageView canvas Parameters

Iv_pic.setLayoutParams (new LinearLayout. LayoutParams (width, height ));

6. Set the rotation of matrix

Matrix. setRotate (int) (progress SS * 3.60 ));

7. BitmapDrawable class is actually used to obtain image resources.

BitmapDrawable bitmapDrawable = (BitmapDrawable) (getResources (). getDrawable (R. drawable. image1 ));

8. bitmapDrawable to bitmap

Bitmap bitmap = bitmapDrawable. getBitmap ();

9. Create a bitmap

Bitmap newBitmap = bitmap. createBitmap (bitmap, 0, 0, bitmap. getWidth (), bitmap. getHeight (), matrix, false );

10. Set bitmap for imageView

Iv_pic.setImageBitmap (newBitmap );

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.