`
nanjingjiangbiao_T
  • 浏览: 2739593 次
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

Android开发--Matrix(二)--实现图片的旋转

 
阅读更多

Matrix功能很是强大,利用这个类提供的一系列方法,我们可以实现图片的旋转。

下面以一个例子说明实现方法。

首先,我们看下实现的截图:


下面给出具体的实现代码:

1.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:background="@drawable/white"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
  <TextView
    android:id="@+id/myTextView1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#ff0000"
    android:text="@string/app_name"/>
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
  >
    <Button
      android:id="@+id/myButton1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/str_button1" />
    <ImageView
      android:id="@+id/myImageView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" />
    <Button
      android:id="@+id/myButton2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/str_button2" />
  </LinearLayout>
</LinearLayout>

2.主程序的实现

public class EX04_24 extends Activity
{
  private Button mButton1;
  private Button mButton2; 
  private TextView mTextView1; 
  private ImageView mImageView1; 
  private int ScaleTimes; private int ScaleAngle; 
  /** Called when the activity is first created. */ 
  @Override
  public void onCreate(Bundle savedInstanceState) 
  { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    mButton1 =(Button) findViewById(R.id.myButton1);
    mButton2 =(Button) findViewById(R.id.myButton2); 
    mTextView1 = (TextView) findViewById(R.id.myTextView1); 
    mImageView1 = (ImageView) findViewById(R.id.myImageView1); 
   
    //定义旋转的角度和图像转变的比例(大小)
    ScaleTimes = 1; 
    ScaleAngle = 1; 
    
    final Bitmap mySourceBmp = 
      BitmapFactory.decodeResource(getResources(), R.drawable.hippo); 
    
    final int widthOrig = mySourceBmp.getWidth(); 
    final int heightOrig = mySourceBmp.getHeight(); 
    
    /* 程序刚执行,加载默认的Drawable */ 
    mImageView1.setImageBitmap(mySourceBmp); 
   
    /* 向左选转按钮 */
    mButton1.setOnClickListener(new Button.OnClickListener()
    {
      
       @Override
       public void onClick(View v) 
      { 
     // TODO Auto-generated method stub 
     ScaleAngle--; 
     if(ScaleAngle<-5) 
     {
       ScaleAngle = -5; 
       } 
     
     /* ScaleTimes=1,维持1:1的宽高比例*/ 
     int newWidth = widthOrig * ScaleTimes;
     int newHeight = heightOrig * ScaleTimes; 
     
     float scaleWidth = ((float) newWidth) / widthOrig;
     float scaleHeight = ((float) newHeight) / heightOrig; 
     
     Matrix matrix = new Matrix(); 
     /* 使用Matrix.postScale设定维度 */
     matrix.postScale(scaleWidth, scaleHeight);
     /* 使用Matrix.postRotate方法旋转Bitmap*/
     //matrix.postRotate(5*ScaleAngle); 
     matrix.setRotate(5*ScaleAngle); 
     /* 建立新的Bitmap对象 */
     Bitmap resizedBitmap = 
       Bitmap.createBitmap(mySourceBmp, 0, 0, widthOrig, heightOrig, matrix, true);
     /**/ 
     BitmapDrawable myNewBitmapDrawable = 
       new BitmapDrawable(resizedBitmap); 
     mImageView1.setImageDrawable(myNewBitmapDrawable); 
     mTextView1.setText(Integer.toString(5*ScaleAngle));
     }
       });
    /* 向右选转按钮 */
    mButton2.setOnClickListener(new Button.OnClickListener() 
    { 
      @Override
      public void onClick(View v) 
      { 
        // TODO Auto-generated method stub 
        ScaleAngle++; 
        if(ScaleAngle>5) 
        { 
          ScaleAngle = 5; 
          } 
        /* ScaleTimes=1,维持1:1的宽高比例*/ 
        int newWidth = widthOrig * ScaleTimes; 
        int newHeight = heightOrig * ScaleTimes; 
        /* 计算旋转的Matrix比例 */ 
        float scaleWidth = ((float) newWidth) / widthOrig; 
        float scaleHeight = ((float) newHeight) / heightOrig; 
        Matrix matrix = new Matrix();
        /* 使用Matrix.postScale设定维度 */
        matrix.postScale(scaleWidth, scaleHeight); 
        /* 使用Matrix.postRotate方法旋转Bitmap*/
        //matrix.postRotate(5*ScaleAngle);
        matrix.setRotate(5*ScaleAngle);
        /* 建立新的Bitmap对象 */ 
        Bitmap resizedBitmap = 
          Bitmap.createBitmap(mySourceBmp, 0, 0, widthOrig, heightOrig, matrix, true);
        /**/ BitmapDrawable myNewBitmapDrawable = 
          new BitmapDrawable(resizedBitmap); 
        mImageView1.setImageDrawable(myNewBitmapDrawable); 
        mTextView1.setText(Integer.toString(5*ScaleAngle)); 
        } 
      }); 
    } 
  }


分享到:
评论

相关推荐

    Android开发之图片旋转功能实现方法【基于Matrix】

    本文实例讲述了Android开发之图片旋转功能实现方法。分享给大家供大家参考,具体如下: 在Android中进行图像旋转需要使用Matrix,它包含了一个3*3的矩阵,专门用于进行图像变换匹配。Matrix ,中文里叫矩阵,高等...

    android 利用matrix实现图片的旋转与缩放

    在Android开发中,图片的旋转和缩放是常见的需求,特别是在设计用户界面或者处理图像时。Matrix类在Android图形处理中扮演着重要角色,它是一个3x3的矩阵,用于进行2D变换,如平移、旋转、缩放和倾斜等。本教程将...

    Android开发之ImageView通过matrix实现两点缩放和图片拖动

    在Android开发中,ImageView是用于显示图像的常见组件。然而,仅靠基本的ImageView功能,我们往往无法满足一些复杂的交互需求,比如用户可以对图片进行拖动和双指缩放。这种功能通常需要借助Matrix类来实现。Matrix...

    Android-3D自动旋转的旋转木马

    在Android应用开发中,"Android-3D自动旋转的旋转木马"是一个独特且吸引人的交互元素,通常用于展示一系列图像或卡片,就像真实的旋转木马一样,以3D效果自动旋转。这种控件可以增强用户体验,使得应用程序更加生动...

    Android--开发--常用图片特效处理.rar

    这个名为"Android--开发--常用图片特效处理.rar"的压缩包很可能包含了实现各种图片特效的技术文档、示例代码或库资源。下面我们将详细探讨一些Android平台上常见的图片特效处理技术。 1. **图片裁剪与旋转**: 在...

    Android代码-android相册系统(用Matrix实现).zip

    通过以上讲解,我们可以看出这个项目不仅涉及到了Matrix类的图像变换,还涵盖了Android开发中的多个核心概念,包括UI设计、数据管理、图片处理等,为学习Android开发的人员提供了一个实用的示例。

    android缩放/旋转图片Matrix代码

    在Android开发中,处理图像时经常需要用到缩放和旋转的功能。本文将详细介绍如何利用`Matrix`类来实现对图像的缩放与旋转操作,并通过一个具体的示例代码进行解析。 #### 一、Matrix 类简介 `Matrix`类是Android中...

    Android-CameraMatrix实现WheelView3d效果

    在Android开发中,创建独特的用户界面控件可以显著提升应用的用户体验。本示例中,我们探讨的是如何利用Camera和Matrix这两个核心图形处理组件来实现一个3D效果的WheelView。这种控件常用于模拟旋转轮盘,如日历选择...

    android 动态旋转图片 Bitmap与Matrix旋转ImageView

    在Android开发中,图片处理是一项常见的任务,动态旋转图片是其中的一个重要环节。Bitmap和Matrix是Android系统提供的两个核心类,用于处理图像和实现图像的变换。Matrix类提供了丰富的功能,可以用来对图像进行平移...

    安卓Android源码——android相册系统(用Matrix实现).rar

    这个压缩包“安卓Android源码——android相册系统(用Matrix实现).rar”显然提供了一个使用Matrix类来实现相册功能的示例代码。Matrix是Android SDK中的一个关键类,用于进行2D图像变换,如旋转、缩放、平移等。接...

    Android-使用Matrix对Bitmap进行处理

    在Android开发中,Bitmap是用于图像处理的基本对象,它存储并表示了图像的数据。而Matrix则是Android图形系统中的一个关键类,它允许我们对图像进行各种变换操作,如旋转、缩放、平移和倾斜等。这个教程将深入探讨...

    安卓Android源码——android相册系统(用Matrix实现).zip

    在安卓开发中,Android相册系统是用户交互的重要部分,用于管理和展示用户的图片。这个压缩包文件"安卓Android源码——android相册系统(用Matrix实现).zip"包含了一个使用Matrix类来实现的相册系统源码。Matrix是...

    android应用源码图片放大缩小旋转涂鸦源码.zip

    在Android平台上,开发一款能够实现图片放大、缩小、旋转以及涂鸦功能的应用是常见的需求,这对于用户交互和图像处理有着重要的意义。这份"android应用源码图片放大缩小旋转涂鸦源码.zip"提供了完整的源代码,可以...

    Android实训-图片编辑工具(裁剪+旋转)

    在Android开发中,图片编辑工具是用户界面中的一个重要组成部分,特别是在社交、摄影或者设计类应用中。本实训项目聚焦于两个关键功能:图片裁剪和图片旋转,这都是对图像进行基本操作的核心需求。让我们详细探讨这...

    Android使用Matrix实现图片缩放,移动

    在Android开发中,图片处理是一项常见的任务,尤其是在创建交互式应用时,用户可能需要缩放、移动或旋转图片。本文将深入探讨如何使用Android的Matrix类来实现这一功能,同时避免内存溢出(OOM)问题。 Matrix是...

    安卓开发-android相册系统(用Matrix实现).zip.zip

    在Android开发中,创建一个高效的相册系统是常见的需求,Matrix类在图像处理和视图变换中扮演着重要角色。本教程将深入探讨如何利用Matrix实现Android相册系统的功能。 Matrix类是Android图形库中的核心组件,它...

    Android实现图片顺时逆时旋转及拖拽显示效果

    在Android开发中,实现图片的顺时针和逆时针旋转以及拖拽显示效果是一项常见的需求,这在各种应用中都有广泛的应用,如照片编辑、游戏界面等。本篇文章将详细探讨如何在Android环境中实现这一功能。 首先,我们需要...

    Android开发图片旋转的两种方式

    在Android开发中,图片旋转是常见的需求,例如用户拍照后可能需要调整图片方向,或者在应用内展示图片时根据需要进行动态旋转。本教程将详细讲解两种实现Android图片旋转的方法:动画(Animation)和Matrix变换。 ...

    Android开发-Sensor传感器-AndroidStudio(二)小方

    在Android开发中,Sensor传感器是实现设备智能化和交互性的重要组成部分。本文将深入探讨如何在Android Studio环境下利用Sensor API进行传感器编程,这是Android开发-Sensor传感器系列教程的第二部分。 一、Android...

    安卓图片轮播广告轮播自动滚屏相关-android之旋转罗盘风车开发Android随手指旋转图片.rar

    4. **旋转罗盘/风车效果**:这种效果的实现通常需要对图片进行旋转处理,可以使用Matrix对象来操作图像的坐标系,实现旋转。同时,为了达到风车般连续旋转的效果,可能还需要利用到自定义View或者SurfaceView,实现...

Global site tag (gtag.js) - Google Analytics