图片的旋转

 

   主要运用了Matrix类,postRotate()方法和postScale()方法;

 

   Matrix:中文是矩阵的意思,主要用于图片的缩放,平移与旋转;

   postRotate()用于旋转,postScale()用于缩放;

 

具体MianAvtivity代码如下:

 1 package com.example.lenovo.a1105;
 2 
 3 import android.graphics.Bitmap;
 4 import android.graphics.BitmapFactory;
 5 import android.graphics.Matrix;
 6 import android.graphics.drawable.BitmapDrawable;
 7 import android.support.v7.app.AppCompatActivity;
 8 import android.os.Bundle;
 9 import android.view.Menu;
10 import android.view.MenuItem;
11 import android.view.View;
12 import android.widget.Button;
13 import android.widget.ImageView;
14 
15 public class MainActivity extends AppCompatActivity {
16 
17     private Bitmap myBitmap;
18     private Matrix myMatrix=new Matrix();
19     private int width;
20     private int height;
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity_main);
25 
26         Button rotaToLeft=(Button)findViewById(R.id.rotaToLeft);
27         Button rotaToRight=(Button)findViewById(R.id.rotaToRight);
28         Button scaleBig=(Button)findViewById(R.id.scaleBig);
29         Button scaleSmall=(Button)findViewById(R.id.scaleSmall);
30 
31         myBitmap= BitmapFactory.decodeResource(getResources(),R.drawable.pic);
32                                      //获取资源图片pic的Bitmap
33         width=myBitmap.getWidth();
34         height=myBitmap.getHeight();
35                             //这两句是获取图片原始大小
36 
37         //以下是四个按钮的单击事件
38 
39         rotaToLeft.setOnClickListener(new View.OnClickListener() {
40             @Override
41             public void onClick(View v) {
42                 myMatrix.postRotate(-90);  //逆时针旋转90°
43 
44                 Bitmap newBitmap=Bitmap.createBitmap(myBitmap,0,0,width,height,myMatrix,false);
45                                 //创建一个新的图片,重新绘图
46                 BitmapDrawable newBmp=new BitmapDrawable(newBitmap);
47                                //把Bitmap装换成Drawable,使其可以显示在ImageView与ImageButton中
48                 ImageView imageView=(ImageView)findViewById(R.id.pic);
49                                //创建Image对象
50                 imageView.setImageDrawable(newBmp);
51             }
52         });
53         rotaToRight.setOnClickListener(new View.OnClickListener() {
54             @Override
55             public void onClick(View v) {
56                 myMatrix.postRotate(90); //顺时针旋转90°
57                 Bitmap newBitmap=Bitmap.createBitmap(myBitmap,0,0,width,height,myMatrix,false);
58                 BitmapDrawable newBmp=new BitmapDrawable(newBitmap);
59                 ImageView imageView=(ImageView)findViewById(R.id.pic);
60                 imageView.setImageDrawable(newBmp);
61             }
62         });
63         scaleBig.setOnClickListener(new View.OnClickListener() {
64             @Override
65             public void onClick(View v) {
66                 myMatrix.postScale(1.2f, 1.2f);
67 
68                 Bitmap newBitmap=Bitmap.createBitmap(myBitmap,0,0,width,height,myMatrix,true);
69                 BitmapDrawable newBmp=new BitmapDrawable(newBitmap);
70                 ImageView imageView=(ImageView)findViewById(R.id.pic);
71                 imageView.setImageDrawable(newBmp);
72 
73             }
74         });
75         scaleSmall.setOnClickListener(new View.OnClickListener() {
76             @Override
77             public void onClick(View v) {
78                 myMatrix.postScale(0.5f, 0.5f);
79 
80                 Bitmap newBitmap=Bitmap.createBitmap(myBitmap,0,0,width,height,myMatrix,true);
81                 BitmapDrawable newBmp=new BitmapDrawable(newBitmap);
82                 ImageView imageView=(ImageView)findViewById(R.id.pic);
83                 imageView.setImageDrawable(newBmp);
84             }
85         });
86     }
87 
88 }
View Code

main.xml代码如下:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="vertical">
 5 
 6     <ImageView
 7         android:id="@+id/pic"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:layout_gravity="center"
11         android:background="@drawable/pic"
12         android:layout_marginTop="40dp"
13         />
14 
15     <LinearLayout
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:layout_marginTop="40dp"
19         android:orientation="horizontal">
20         <Button
21             android:id="@+id/rotaToLeft"
22             android:layout_width="wrap_content"
23             android:layout_height="wrap_content"
24             android:text="左旋" />
25         <Button
26             android:id="@+id/rotaToRight"
27             android:layout_width="wrap_content"
28             android:layout_height="wrap_content"
29             android:text="右旋" />
30 
31         <Button
32             android:id="@+id/scaleBig"
33             android:layout_width="wrap_content"
34             android:layout_height="wrap_content"
35             android:text="放大" />
36 
37         <Button
38             android:id="@+id/scaleSmall"
39             android:layout_width="wrap_content"
40             android:layout_height="wrap_content"
41             android:text="缩小" />
42 
43 
44     </LinearLayout>
45 
46 
47 </LinearLayout>
View Code

 

实现后如下:

   

转载于:https://www.cnblogs.com/Lynn0814/p/4939779.html

<think>我们面对的用户问题是如何旋转图片。根据任务要求,需要调用网络搜索工具进行检索,然后整合信息生成回答。 由于不能使用第一人称和步骤词汇,我们将以直接的方式提供方法,并分点说明(用换行分隔)。 同时,回答中需要包含相关代码或公式时,要严格遵循格式要求。 最后,必须添加相关问题部分。 根据用户意图,旋转图片是一个常见的图像处理任务,可以通过多种工具和编程语言实现。我们将提供几种主流的方法: 1. 使用操作系统自带的图片查看器或编辑器(如Windows照片查看器、macOS预览)。 2. 使用在线图片旋转工具。 3. 使用专业的图像处理软件(如Photoshop、GIMP)。 4. 使用编程方式(如Python的PIL/Pillow库)进行旋转。 我们将分别介绍这些方法,并在适当的地方提供代码示例。 注意:由于要求禁止将引用内容集中在末尾,所以我们在每个方法中会直接提及关键点,但不会标注引用来源(实际搜索中会参考多个来源,整合成答案)。 开始生成回答:</think>要旋转图片,可根据不同平台和需求选择以下方法: ### 使用操作系统内置工具 **Windows系统** 打开图片 → 右键选择"编辑" → 在画图工具中点击"旋转"菜单 → 选择旋转角度(90°/180°/270°) **macOS系统** 用预览打开图片 → 点击工具栏"标记"图标 → 选择顺时针/逆时针旋转按钮 ### 在线工具(无需安装) 1. **Photopea**:访问 [photopea.com](https://www.photopea.com) 上传图片 → 菜单选择"图像" → "旋转" → 输入角度 → 导出文件 2. **Pixlr**:访问 [pixlr.com](https://pixlr.com) 选择"Pixlr E" → 上传图片 → 顶部菜单"图像" → "旋转"选项 ### 编程实现(Python示例) 使用Pillow库进行精确角度旋转: ```python from PIL import Image # 打开图片旋转 image = Image.open("input.jpg") rotated = image.rotate(45, expand=True) # 旋转45度(expand防止裁剪) rotated.save("output.jpg") ``` 参数说明: `rotate(角度, expand=True)`中: - 角度支持任意值(如$30.5^\circ$) - `expand=True`确保旋转图片完整显示 ### 专业软件操作 **Photoshop/GIMP** 快捷键`Ctrl+T`(Win)/`Cmd+T`(Mac)进入自由变换 → 鼠标悬停边角拖动旋转 → 按Enter确认 或通过菜单:图像 → 图像旋转 → 指定角度 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值