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

 更新时间:2021年09月23日 09:41:54   投稿:lijiao  
这篇文章主要介绍了Android UI之ImageView实现图片旋转和缩放的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

这一篇,给大家介绍一下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中的该方法介绍:

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的实现效果,希望对大家的学习有所帮助。

相关文章

  • android dialog背景模糊化效果实现方法

    android dialog背景模糊化效果实现方法

    这篇文章主要为大家详细介绍了android dialog背景模糊化效果的实现方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04
  • Android自定义View实现广告信息上下滚动效果

    Android自定义View实现广告信息上下滚动效果

    这篇文章主要为大家详细介绍了Android自定义View实现广告信息上下滚动的具体代码,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • Android多线程学习实例详解

    Android多线程学习实例详解

    这篇文章主要介绍了Android多线程,结合实例形式较为详细的分析了Android多线程的概念、使用方法与相关注意事项,需要的朋友可以参考下
    2016-10-10
  • Android 架构之数据库框架搭建

    Android 架构之数据库框架搭建

    这篇文章主要给大家介绍的是Android 架构之数据库框架搭建,在本篇中,将会让你一点一滴从无到有创建一个不再为数据库而烦恼的框架。需要的朋友可以参考下面文章的具体内容
    2021-09-09
  • Android Studio连接SQLite数据库的登录注册实现

    Android Studio连接SQLite数据库的登录注册实现

    这篇文章主要介绍了Android Studio连接SQLite数据库的登录注册实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • 解析android 流量监测的实现原理

    解析android 流量监测的实现原理

    本篇文章是对android中流量监测的实现原理进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • Android开发Jetpack组件LiveData使用讲解

    Android开发Jetpack组件LiveData使用讲解

    LiveData是Jetpack组件的一部分,更多的时候是搭配ViewModel来使用,相对于Observable,LiveData的最大优势是其具有生命感知的,换句话说,LiveData可以保证只有在组件( Activity、Fragment、Service)处于活动生命周期状态的时候才会更新数据
    2022-08-08
  • Android常用设计模式之原型模式详解

    Android常用设计模式之原型模式详解

    这篇文章主要为大家介绍了Android常用设计模式之原型模式详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • android屏蔽按钮连续点击的示例代码

    android屏蔽按钮连续点击的示例代码

    这篇文章主要介绍了android屏蔽按钮连续点击的示例代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12
  • Android ListView之setEmptyView正确使用方法

    Android ListView之setEmptyView正确使用方法

    这篇文章主要介绍了Android ListView之setEmptyView正确使用方法的相关资料,希望通过本文能帮助到大家使用该方法,需要的朋友可以参考下
    2017-09-09

最新评论