图片的旋转

 

   主要运用了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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/296075.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

让 AI 为你写代码 - 体验 Github Copilot

前几天在群里看到有大神分享 Copoilot AI 写代码&#xff0c;看了几个截图有点不敢相信自己的眼睛。今天赶紧自己也来体验一下 Copoilot AI 写代码到底有多神奇。申请 现在 Copoilot 还处在预览阶段&#xff0c;想要体验需要先申请。等待大概一晚会收到邮件提示申请试用成功&am…

ajax常见错误和使用总结

先给出标准的js时间ajax <script type"txt/javascript">//1、在IE中实例化Msxml2.XMLHTTP对象 Msxml2.XMLHTTP是IE浏览器的内置对象&#xff0c;该对象具有异步提交数据和获取结果的功能var xmlHttpfalse; function initAJAX() {if(window.XMLHttpRequset){x…

这个24岁北航博士刚毕业就受聘211大学副教授,他大一就保研,学术能力太牛了.........

全世界只有3.14 % 的人关注了爆炸吧知识本文综合整理自&#xff1a;量子位、微言航语近日&#xff0c;有一个人的“朋友圈”在朋友圈火了。别误会&#xff0c;超模君可没在玩套娃游戏。截图给大家搬来了&#xff0c;快看你没看错&#xff01;1996年出生&#xff0c;今年24岁&am…

正则 js截取时间

项目中要把时间截取&#xff0c;只要年月日&#xff0c;不要时分秒&#xff0c;于是 /\d{4}-\d{1,2}-\d{1,2}/g.exec("2012-6-18 00:00:00")或者另一种 var date "2015-12-26 15:22:00"; console.log(date.replace(/\s[\x00-\xff]*/g,));解析 思路:获取到…

数据中心缩小是因为外包和云计算吗

一些IT专家预测&#xff1a;在云计算和数据中心外包时代&#xff0c;将会有越来越多的企业看不到建造和管理数据中心的价值。 “建造数据中心是一笔大的支出&#xff0c;你得考虑到能量、发电、UPS、机架等等&#xff0c;这还没讲到服务器呢&#xff0c;”架构师Tim Antonowicz…

英特尔傲腾内存linux,英特尔傲腾内存怎么样?intel傲腾内存优点和缺点你知道吗?...

英特尔傲腾内存在前一段时间正是发布&#xff0c;对于英特尔内存的性能不少用户一无所知&#xff0c;那么英特尔傲腾内存怎么样&#xff1f;都有哪些优点和缺点&#xff1f;下面装机之家小编来为大家解读下。优点1&#xff1a;3D XPoint随机读取性能强傲腾使用了不同于普通固态…

零代码平台中的服务编排思路

先打个广告&#xff0c;我们的第三场零代码实践的直播在本周五&#xff08; 11 月 5 日 &#xff09;晚8点准时开始&#xff0c;扫描下面二维码&#xff0c;直接预约直播&#xff0c;到时间微信会自动提醒。随着企业数字化转型的进程加快&#xff0c;零代码平台的的应用越来越广…

webservice发布

一朋友问我webservice怎么发布在iis上。我之前也不知道&#xff0c;今天自己亲自试了一把竟然发布成功了。现在这个分享给大家。希望给大家带来一点点方便。 其实和发布普通网站一样&#xff0c;在iis上选择 默认网站--->新建虚拟目录--->取别名&#xff08;随便取&#…

自己平时长期积累的java资料可供大家学习

java 中数据转换 1、如何将字符串String转化为整数int int i Integer.parseInt(str); int i Integer.valueOf(my_str).intValue(); 注: 字串转成Double, Float, Long的方法大同小异。 2、如何将字符串String转化为Integer Integer integerInteger.valueOf(i) 3、…

日本原装进口雪平锅,1台顶4台,有它谁还点外卖?

▲ 点击查看小爆我虽然热爱烹饪&#xff0c;但不得不说「下厨房」&#xff0c;也是个坑。光是锅&#xff0c;我就要买好几个。为了蒸包子馒头买蒸锅&#xff0c;为了炒菜买炒锅&#xff0c;偶尔想精致喝热牛奶又买了小奶锅&#xff0c;为了煲汤、做点卤味解解馋&#xff0c;买炖…

NSPredicate 谓词

比较运算符/**比较运算符 * >:大于 * <:小于 * >:大于等于 * <:小于等于 * ,:等于 * !,<>:不等于 * between:左边的表达式等于右边的表达式的值或者介于它们之间。右边是一个有两个指定上限和下限的数值的数…

如何评价一个开源项目——价值流网络

本文由X-lab开放实验室博士生赵生宇原创出品该篇博客继续之前关于活跃度和协作影响力的介绍继续展开&#xff0c;希望可以在解决协作影响力无法容纳更多数据&#xff0c;从而可以更全面衡量开源生态的同时&#xff0c;也引入一种高可扩展的数学模型&#xff0c;可以在任意时间快…

linux7为nginx添加服务,CentOS7添加Nginx为系统服务

1.编辑系统服务vim /usr/lib/systemd/system/nginx.service[unit]DescriptionWeb ServiceAfternetwork.target[Service]PIDFile/var/run/nginx.pidExecStart/usr/local/nginx/sbin/nginxExecStop/usr/local/nginx/sbin/nginx -s stopExecReload/usr/local/nginx/sbin/nginx -s …

Linux内核升级,从2.6.18升级到3.2.14

今日在centos上安装jsp环境&#xff0c;即&#xff08;Nginxjdkmysqltomcat&#xff09;发现nginx启动后无法访问&#xff0c;于是查看日志&#xff0c;log如下 [rootAY12122501352213a7156 ~]# cat /var/log/nginx/error.log 2013/01/12 16:29:43 [emerg] 32055#0: eventfd() …

【翻译】C#编程语言和JAVA编程语言的比较(下)

原文地址&#xff1a;http://www.25hoursaday.com/CsharpVsJava.html 6、集合 许多有名的编程语言都会包含一个集合框架&#xff0c;框架一般由各种用于保存数据的数据结构和配套的操作对象的算法构成。集合框架的优势是让开发者可以不用写数据结构和排序算法&#xff0c;把精力…

数据库平时错误和使用经验的总结

jdbc里面的操作 jdbc&#xff0c;使用PreparedStatement view sourceprint?001 package com.iflytek.test; 002 003 import java.sql.Connection; 004 import java.sql.DriverManager; 005 import java.sql.PreparedStatement; 006 import java.sql.ResultSet; …

Haproxy 让后端RS记录真实IP

#让RS记录客户端的真实IP#1.先在haproxy.cfg中加入下面参数。listen www ... option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数&#xff0c;必须要放在listen模块下#2.如果是apache&#xff0c;则加入下面参数LogFormat “\”%{X-Forward…

物理太难?这些虚拟动图,让你看懂物理

全世界只有3.14 % 的人关注了爆炸吧知识&#xff08;刻度尺的使用&#xff09;&#xff08;测量平均速度实验&#xff09;&#xff08;声音的产生&#xff09;&#xff08;温度计的使用&#xff09;&#xff08;晶体和非晶体的熔化&#xff09;&#xff08;光的反射&#xff09…

linux http 分析工具,技术|httpstat:一个检查网站性能的 curl 统计分析工具

httpstat 是一个 Python 脚本&#xff0c;它以美妙妥善的方式反映了 curl 统计分析&#xff0c;它是一个单一脚本&#xff0c;兼容 Python 3 &#xff0c;在用户的系统上不需要安装额外的软件(依赖)。从本质上来说它是一个 cURL 工具的封装&#xff0c;意味着你可以在 URL 后使…

微软 Ignite 大会 PowerBI 划重点

2021 年 11 月 2 日&#xff0c;微软举办 Ignite 大会&#xff0c;其中关于 Power BI 有哪些重点&#xff0c;带给你了解。摘要Power BI 和 Power Point 将原生整合。Power BI 和 Teams 将更深入整合。Power BI 云端数据集推出自动聚合。Power BI 数据模型推出混合表。Power BI…