欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android UI之ImageView实现图片旋转和缩放

程序员文章站 2023-11-21 23:08:34
这一篇,给大家介绍一下imageview控件的使用,imageview主要是用来显示图片,可以对图片进行放大、缩小、旋转的功能。 android:sacletype属性指...

这一篇,给大家介绍一下imageview控件的使用,imageview主要是用来显示图片,可以对图片进行放大、缩小、旋转的功能。
android:sacletype属性指定imageview控件显示图片的方式,例如:center表示图像以不缩放的方式显示在imageview控件的中心,如果设置为fitcenter,表示图像按照比例缩放至合适的位置,并在imageview控件的中心。
首先我们开发一个简单的案例,实现图片的放大缩小和旋转:
先看看实现的效果:
缩放截图1:

Android UI之ImageView实现图片旋转和缩放

缩放截图2:

Android UI之ImageView实现图片旋转和缩放

旋转截图1:

Android UI之ImageView实现图片旋转和缩放

旋转截图2:

Android UI之ImageView实现图片旋转和缩放

在实现图片的缩放和旋转时,我们都需要用到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中的该方法介绍:

void setrotate(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 plaincopyprint?
package com.imageview.activity; 
 
import com.imageview.activity.r; 
import android.app.activity; 
import android.graphics.bitmap; 
import android.graphics.matrix; 
import android.graphics.drawable.bitmapdrawable; 
import android.os.bundle; 
import android.util.displaymetrics; 
import android.widget.imageview; 
import android.widget.linearlayout; 
import android.widget.seekbar; 
import android.widget.seekbar.onseekbarchangelistener; 
 
public class mainactivity extends activity implements onseekbarchangelistener { 
 private int minwidth = 80; 
 private imageview imageview; 
 private seekbar seekbar1; 
 private seekbar seekbar2; 
 private matrix matrix = new matrix(); 
 
 @override 
 public void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.main); 
  imageview = (imageview) findviewbyid(r.id.imageview1); 
  seekbar1 = (seekbar) findviewbyid(r.id.seekbar1); 
  seekbar2 = (seekbar) findviewbyid(r.id.seekbar2); 
  seekbar1.setonseekbarchangelistener(this); 
  seekbar2.setonseekbarchangelistener(this); 
  // 定义一个displaymetrics对象,用来显示旋转的图像 
  displaymetrics dm = new displaymetrics(); 
  // 根据手机屏幕大小来缩放 
  getwindowmanager().getdefaultdisplay().getmetrics(dm); 
  seekbar1.setmax(dm.widthpixels - minwidth); 
 } 
 
 @override 
 public void onprogresschanged(seekbar seekbar, int progress,boolean fromuser) { 
  switch (seekbar.getid()) { 
  case r.id.seekbar1: 
   int newwidth = progress + minwidth; 
   int newheight = (int) (newwidth * 3 / 4); 
   imageview.setlayoutparams(new linearlayout.layoutparams(newwidth,newheight)); 
   break; 
  case r.id.seekbar2: 
   bitmap bitmap = ((bitmapdrawable) getresources().getdrawable(r.drawable.pic)).getbitmap(); 
   // 设置旋转角度 
   matrix.setrotate(progress); 
   // 重新绘制bitmap 
   bitmap = bitmap.createbitmap(bitmap, 0, 0, bitmap.getwidth(),bitmap.getheight(), matrix, true); 
   imageview.setimagebitmap(bitmap); 
   break; 
  } 
 } 
 
 @override 
 public void onstarttrackingtouch(seekbar seekbar) { 
 
 } 
 
 @override 
 public void onstoptrackingtouch(seekbar seekbar) { 
 
 } 
} 

布局文件main.xml

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:orientation="vertical" > 
 <imageview 
  android:layout_width="200dp" 
  android:layout_height="150dp" 
  android:scaletype="fitcenter" 
  android:background="#ffffff" 
  android:src="@drawable/pic" 
  android:id="@+id/imageview1"/> 
 <seekbar 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:max="100" 
  android:id="@+id/seekbar1"/> 
 <textview 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="拖动来缩放图片" /> 
 <seekbar 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:max="100" 
  android:id="@+id/seekbar2"/> 
 <textview 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="拖动来旋转图片" /> 
</linearlayout> 

最后说明一点,要在imageview中显示的图片进行旋转,请选择一张符合matrix的3*3矩阵的图片,否则在旋转过程中超过屏幕宽度会引起报错,本例中选取的是一张正方形的图片,如果是长方形的建议做一下代码逻辑判断处理。
以上就是关于matrix的实现效果,希望对大家的学习有所帮助。