您的位置:首页 > 移动开发 > Android开发

利用android Matrix来处理简单图片

2012-07-31 11:38 417 查看
精彩源码:
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);效果:



本文出自 “Android小子的” 博客,请务必保留此出处http://androidrigl.blog.51cto.com/7531835/1247600
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐