iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android UI之ImageView实现图片旋转和缩放
  • 183
分享到

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

图片Android 2022-06-06 09:06:35 183人浏览 八月长安
摘要

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

这一篇,给大家介绍一下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实现屏幕旋转方法总结Android Tween动画之RotateAnimation实现图片不停旋转效果实例介绍Android开发 旋转屏幕导致Activity重建解决方法Android中利用matrix 控制图片的旋转、缩放、移动Android编程中调用Camera时预览画面有旋转问题的解决方法Android实现图片反转、翻转、旋转、放大和缩小Android 图片缩放与旋转的实现详解Android开发之图形图像与动画(二)Animation实现图像的渐变/缩放/位移/旋转Android 3D旋转动画效果实现分解Flutter RotationTransition实现旋转动画


--结束END--

本文标题: Android UI之ImageView实现图片旋转和缩放

本文链接: https://www.lsjlt.com/news/26542.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作