ImageView实现图片旋转和缩放

来源:互联网 发布:阿里云哪个节点好 编辑:程序博客网 时间:2024/05/28 11:30

原文地址:http://blog.csdn.net/wulianghuan/article/details/8585990


这一篇,给大家介绍一下ImageView控件的使用,ImageView主要是用来显示图片,可以对图片进行放大、缩小、旋转的功能。

android:sacleType属性指定ImageVIew控件显示图片的方式,例如:center表示图像以不缩放的方式显示在ImageView控件的中心,如果设置为fitCenter,表示图像按照比例缩放至合适的位置,并在ImageView控件的中心。

首先我们开发一个简单的案例,实现图片的放大缩小和旋转:

先看看实现的效果,

缩放截图1:


缩放截图2:



旋转截图1:



旋转截图2:




在实现图片的缩放和旋转时,我们都需要用到android.graphics.Matrix这个类,对于Matrix在API中的介绍如下:

Class Overview


The Matrix class holds a 3x3 matrix for transforming coordinates. Matrix does not have a constructor, so it must be explicitly initialized using either reset() - to construct an identity matrix, or one of the set..() functions (e.g. setTranslate, setRotate, etc.).


本实例中使用到android.graphics.Matrix的 setRotate方法来设置旋转角度,以下是API中的该方法介绍:
voidsetRotate(float degrees, float px, float py)
Set the matrix to rotate by the specified number of degrees, with a pivot point at (px, py).

源代码:

MainActivity.java

[html] view plaincopy
  1. package com.imageview.activity;  
  2.   
  3. import com.imageview.activity.R;  
  4. import android.app.Activity;  
  5. import android.graphics.Bitmap;  
  6. import android.graphics.Matrix;  
  7. import android.graphics.drawable.BitmapDrawable;  
  8. import android.os.Bundle;  
  9. import android.util.DisplayMetrics;  
  10. import android.widget.ImageView;  
  11. import android.widget.LinearLayout;  
  12. import android.widget.SeekBar;  
  13. import android.widget.SeekBar.OnSeekBarChangeListener;  
  14.   
  15. public class MainActivity extends Activity implements OnSeekBarChangeListener {  
  16.     private int minWidth = 80;  
  17.     private ImageView imageView;  
  18.     private SeekBar seekBar1;  
  19.     private SeekBar seekBar2;  
  20.     private Matrix matrix = new Matrix();  
  21.   
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.main);  
  26.         imageView = (ImageView) findViewById(R.id.imageview1);  
  27.         seekBar1 = (SeekBar) findViewById(R.id.seekbar1);  
  28.         seekBar2 = (SeekBar) findViewById(R.id.seekbar2);  
  29.         seekBar1.setOnSeekBarChangeListener(this);  
  30.         seekBar2.setOnSeekBarChangeListener(this);  
  31.         // 定义一个DisplayMetrics对象,用来显示旋转的图像  
  32.         DisplayMetrics dm = new DisplayMetrics();  
  33.         // 根据手机屏幕大小来缩放  
  34.         getWindowManager().getDefaultDisplay().getMetrics(dm);  
  35.         seekBar1.setMax(dm.widthPixels - minWidth);  
  36.     }  
  37.   
  38.     @Override  
  39.     public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {  
  40.         switch (seekBar.getId()) {  
  41.         case R.id.seekbar1:  
  42.             int newWidth = progress + minWidth;  
  43.             int newHeight = (int) (newWidth * 3 / 4);  
  44.             imageView.setLayoutParams(new LinearLayout.LayoutParams(newWidth,newHeight));  
  45.             break;  
  46.         case R.id.seekbar2:  
  47.             Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.pic)).getBitmap();  
  48.             // 设置旋转角度  
  49.             matrix.setRotate(progress);  
  50.             // 重新绘制Bitmap  
  51.             bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight(), matrix, true);  
  52.             imageView.setImageBitmap(bitmap);  
  53.             break;  
  54.         }  
  55.     }  
  56.   
  57.     @Override  
  58.     public void onStartTrackingTouch(SeekBar seekBar) {  
  59.   
  60.     }  
  61.   
  62.     @Override  
  63.     public void onStopTrackingTouch(SeekBar seekBar) {  
  64.   
  65.     }  
  66. }  

布局文件main.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.     <ImageView   
  7.         android:layout_width="200dp"  
  8.         android:layout_height="150dp"  
  9.         android:scaleType="fitCenter"  
  10.         android:background="#FFFFFF"  
  11.         android:src="@drawable/pic"  
  12.         android:id="@+id/imageview1"/>  
  13.     <SeekBar   
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:max="100"  
  17.         android:id="@+id/seekbar1"/>  
  18.     <TextView  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:text="拖动来缩放图片" />  
  22.     <SeekBar   
  23.         android:layout_width="fill_parent"  
  24.         android:layout_height="wrap_content"  
  25.         android:max="100"  
  26.         android:id="@+id/seekbar2"/>  
  27.     <TextView  
  28.         android:layout_width="fill_parent"  
  29.         android:layout_height="wrap_content"  
  30.         android:text="拖动来旋转图片" />  
  31. </LinearLayout>  

最后说明一点,要在ImageView中显示的图片进行旋转,请选择一张符合Matrix的3*3矩阵的图片,否则在旋转过程中超过屏幕宽度会引起报错,本例中选取的是一张正方形的图片,如果是长方形的建议做一下代码逻辑判断处理。
0 0