利用android Matrix来处理简单图片

精彩源码:

AChartEngine的简单使用:柱状图、饼状图、折线图
http://www.eoeandroid.com/thread-188241-1-1.html

第三方集成之新浪微薄
http://www.eoeandroid.com/thread-168264-1-1.html

第三方集成之人人客户端
http://www.eoeandroid.com/thread-168100-1-1.html

Matrix是由一个3×3的矩阵组成的,因为涉及到数学中的矩阵概念先不做解释。Matrix已经给我们封装好了一些方法,这里先看看每个方法的效果。
  程序目录如下:

main.xml展示变换前后的图片:

复制代码
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 
    >
 <imageview
 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/picture1"
    />
 
  <textview
    android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="下图是改变后的效果" /> <imageview android:id="@+id/myimage" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/picture1" />
复制代码

MainActivity负责利用Matrix处理图片,首先演示图片旋转效果,主要代码:

复制代码
public void onCreate(Bundle savedInstanceState) {
 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);
 
    ImageView imageView;
 
    Matrix mMatrix = new Matrix(); 
    imageView = (ImageView) findViewById(R.id.myimage);
 
    Bitmap bmp = ((BitmapDrawable) getResources().getDrawable(
 
            R.drawable.picture1)).getBitmap();
        mMatrix.setRotate(60);
 
    Bitmap bm = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),bmp.getHeight(), mMatrix, true);
 
    imageView.setImageBitmap(bm);
 
}
复制代码

注意:Matrix的操作,总共分为translate(平移),rotate(旋转),scale(缩放)和skew(倾斜)四种,每一种变换在
  Android的API里都提供了set, post和pre三种操作方式,除了translate,其他三种操作都可以指定中心点。
  set是直接设置Matrix的值,每次set一次,整个Matrix的数组都会变掉。
  post是后乘,当前的矩阵乘以参数给出的矩阵。可以连续多次使用post,来完成所需的整个变换。
  例如,要将一个图片旋转30度,然后平移到(100,100)的地方,那么可以这样做:

Matrix m = new Matrix(); 
 
m.postRotate(30); 
 
m.postTranslate(100, 100);

将图片旋转60度:

图片倾斜:mMatrix.postSkew(0.3f, 0.7f);效果:

图片缩放,x轴缩小0.5倍,y轴扩大2.5倍:mMatrix.setScale(0.5f, 2.5f);效果:

 

 

 

posted on   vus520  阅读(2801)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!

导航

< 2012年7月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 1 2 3 4
5 6 7 8 9 10 11
点击右上角即可分享
微信分享提示