Android 图片缩放与旋转的实现详解
时间:2023-03-10 10:57:41
本文使用Matrix实现Android实现图片缩放与旋转。示例代码如下:
package com.android.matrix;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;
/**
* Android实现图片缩放与旋转。
* @author Administrator
*
*/
public class MatixActivity extends Activity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setTitle("Android实现图片缩放与旋转。");
LinearLayout linLayout = new LinearLayout(this);
//加载需要操作的图片,这里是一张图片
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.r);
//获取这个图片的宽和高
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
//定义预转换成的图片的宽度和高度
int newWidth = 200;
int newHeight = 200;
//计算缩放率,新尺寸除原始尺寸
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 创建操作图片用的matrix对象
Matrix matrix = new Matrix();
// 缩放图片动作
matrix.postScale(scaleWidth, scaleHeight);
//旋转图片 动作
matrix.postRotate(45);
// 创建新的图片
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
//将上面创建的Bitmap转换成Drawable对象,使得其可以使用在ImageView, ImageButton中
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
//创建一个ImageView
ImageView imageView = new ImageView(this);
// 设置ImageView的图片为上面转换的图片
imageView.setImageDrawable(bmd);
//将图片居中显示
imageView.setScaleType(ScaleType.CENTER);
//将ImageView添加到布局模板中
linLayout.addView(imageView,
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
)
);
// 设置为本activity的模板
setContentView(linLayout);
}
}
上例是静态地实现图片缩放,下例中可以通过鼠标滑轮和方向键实现图片动态的放大与缩小。
程序结构如下图:
Zoom.java文件中代码:
package com.android.zooming;
import android.view.View;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
public class Zoom extends View {
private Drawable image;
private int zoomControler=20;
public Zoom(Context context){
super(context);
image=context.getResources().getDrawable(R.drawable.x);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
//控制图像的宽度和高度
image.setBounds((getWidth()/2)-zoomControler, (getHeight()/2)-zoomControler, (getWidth()/2)+zoomControler, (getHeight()/2)+zoomControler);
image.draw(canvas);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_DPAD_UP)//放大
zoomControler+=10;
if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN) //缩小
zoomControler-=10;
if(zoomControler<10)
zoomControler=10;
invalidate();
return true;
}
}
标签:Android,图片缩放,旋转
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java容器详细解析
2023-08-23 16:13:38
![](https://img.aspxhome.com/file/2023/1/119131_0s.png)
C# using的本质及使用详解
2022-10-10 06:11:23
![](https://img.aspxhome.com/file/2023/0/76390_0s.png)
自定义mybatis插件如何实现sql日志打印
2023-11-29 11:22:37
![](https://img.aspxhome.com/file/2023/0/60670_0s.png)
Android Bitmap和Drawable相互转换的简单代码
2022-01-26 11:09:01
对指定的网页进行截图的效果 C#版
2022-07-04 03:14:18
Spring+SpringMVC+MyBatis深入学习及搭建(三)之MyBatis全局配置文件解析
2022-03-21 05:25:23
![](https://img.aspxhome.com/file/2023/2/84692_0s.png)
Java中的Kotlin 内部类原理
2021-10-26 23:02:13
Android Fragment+FragmentTabHost组件实现常见主页面(仿微信新浪)
2023-10-20 10:00:50
IDEA2020.1个性化设置的实现
2023-10-17 09:31:34
![](https://img.aspxhome.com/file/2023/5/85505_0s.jpg)
Android开发获取重力加速度和磁场强度的方法
2022-05-27 23:07:09
![](https://img.aspxhome.com/file/2023/2/106022_0s.png)
Java由浅入深带你精通继承super
2023-11-23 02:34:28
![](https://img.aspxhome.com/file/2023/7/59737_0s.png)
关于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服务传输的问题
2023-01-04 15:38:04
![](https://img.aspxhome.com/file/2023/0/110790_0s.jpg)
android长截屏原理及实现代码
2021-11-13 05:55:50
![](https://img.aspxhome.com/file/2023/4/128984_0s.jpg)
C# 正则判断一个数字的格式是否有逗号的代码
2021-09-13 07:42:12
详解通过JDBC进行简单的增删改查(以MySQL为例)
2023-08-14 20:16:40
![](https://img.aspxhome.com/file/2023/5/58035_0s.png)
flutter中build.gradle仓库的配置(解决外网下载速度过慢失败的问题)
2023-06-16 02:07:20
android使用ItemDecoration给RecyclerView 添加水印
2022-01-17 19:42:54
![](https://img.aspxhome.com/file/2023/6/100616_0s.gif)
Android中手机录屏并转换GIF的两种方式
2021-12-03 13:00:52
![](https://img.aspxhome.com/file/2023/9/138029_0s.png)
深入浅出MyBatis中映射文件和实体类的关联性
2022-08-29 07:34:08
Android自定义View绘制的方法及过程(二)
2023-05-02 14:42:17