首页 > 代码库 > Android图片旋转,缩放,位移,倾斜,对称完整示例(二)——Bitmap.createBitmap()和Matrix
Android图片旋转,缩放,位移,倾斜,对称完整示例(二)——Bitmap.createBitmap()和Matrix
MainActivity如下:
main.xml如下:
package cc.c; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.widget.ImageView; /** * Demo描述: * 利用Bitmap.createBitmap()和Matrix实现图像 * 的旋转,缩放,位移,倾斜. * 该方式的作用和mImageView.setImageMatrix(matrix); * 是一样的 * * 备注说明: * 利用Bitmap.createBitmap()和Matrix的方式没有实现图像 * 关于X轴,Y轴和XY的对称. * 明白怎么实现的coder请指点,多谢. * */ public class MainActivity extends Activity { private Bitmap mBitmap; private Bitmap mNewBitmap; private ImageView mImageView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mImageView=(ImageView) findViewById(R.id.imageView); mImageView.setScaleType(ImageView.ScaleType.MATRIX); BitmapDrawable bitmapDrawable=(BitmapDrawable) mImageView.getDrawable(); mBitmap=bitmapDrawable.getBitmap(); //平移的两种方式,效果一致 //testTranslate1(mBitmap); testTranslate2(mBitmap); //围绕图片中心点旋转且位移的两种方式,效果一致 //testRotate1(mBitmap); //testRotate2(mBitmap); //围绕原点旋转后平移的两种方式,效果一致 //testRotateAndTranslate1(mBitmap); //testRotateAndTranslate2(mBitmap); //测试缩放的两种方式,效果一致 //testScale1(); //testScale2(mBitmap); //测试倾斜各两种方式,效果一致 //testSkewX1(); //testSkewX2(mBitmap); //testSkewY1(); //testSkewY2(mBitmap); //testSkewXY1(); //testSkewXY2(mBitmap); //测试对称 //testSymmetryX(mBitmap); //testSymmetryY(mBitmap); //testSymmetryXY(mBitmap); } //平移的方式一 private void testTranslate1(Bitmap bitmap){ Matrix matrix=new Matrix(); int width=bitmap.getWidth(); int height=bitmap.getHeight(); matrix.postTranslate(width, height); mImageView.setImageMatrix(matrix); } //平移的方式二 private void testTranslate2(Bitmap bitmap){ Matrix matrix=mImageView.getImageMatrix(); int width=bitmap.getWidth(); int height=bitmap.getHeight(); matrix.postTranslate(width, height); mNewBitmap=Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false); mImageView.setImageBitmap(mNewBitmap); } //围绕图片中心点旋转且位移的方式一 private void testRotate1(Bitmap bitmap){ Matrix matrix=new Matrix(); int width=bitmap.getWidth(); int height=bitmap.getHeight(); matrix.postRotate(45f, width/2, height/2); matrix.postTranslate(width, height); mImageView.setImageMatrix(matrix); } //围绕图片中心点旋转且位移的方式二 //注意问题: //在方式一种旋转45°采用matrix.postRotate(45f, width/2, height/2);即可 //但在方式二中只需旋转22.5度matrix.postRotate(45/2f, width/2, height/2); private void testRotate2(Bitmap bitmap){ Matrix matrix=mImageView.getImageMatrix(); int width=bitmap.getWidth(); int height=bitmap.getHeight(); matrix.postRotate(45/2f, width/2, height/2); matrix.postTranslate(width, height); mNewBitmap=Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false); mImageView.setImageBitmap(mNewBitmap); } //围绕原点旋转后平移的方式一 private void testRotateAndTranslate1(Bitmap bitmap) { Matrix matrix = new Matrix(); int width =bitmap.getWidth(); int height = bitmap.getHeight(); matrix.setRotate(45f); matrix.postTranslate(width, height); mImageView.setImageMatrix(matrix); } //围绕原点旋转后平移的方式二 //注意问题: //同上 private void testRotateAndTranslate2(Bitmap bitmap) { Matrix matrix = mImageView.getImageMatrix(); int width =bitmap.getWidth(); int height = bitmap.getHeight(); matrix.setRotate(45/2f); matrix.postTranslate(width, height); mNewBitmap=Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false); mImageView.setImageBitmap(mNewBitmap); } //缩放的方式一 private void testScale1() { Matrix matrix = new Matrix(); matrix.setScale(0.5f, 0.5f); mImageView.setImageMatrix(matrix); } //缩放的方式二 private void testScale2(Bitmap bitmap) { Matrix matrix = new Matrix(); int width =bitmap.getWidth(); int height = bitmap.getHeight(); matrix.setScale(0.5f, 0.5f); mNewBitmap=Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false); mImageView.setImageBitmap(mNewBitmap); } //水平倾斜的方式一 private void testSkewX1() { Matrix matrix = new Matrix(); matrix.setSkew(0.5f, 0); mImageView.setImageMatrix(matrix); } //水平倾斜的方式二 private void testSkewX2(Bitmap bitmap) { Matrix matrix = new Matrix(); int width =bitmap.getWidth(); int height = bitmap.getHeight(); matrix.setSkew(0.5f, 0); mNewBitmap=Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false); mImageView.setImageBitmap(mNewBitmap); } // 垂直倾斜的方式一 private void testSkewY1() { Matrix matrix = new Matrix(); matrix.setSkew(0, 0.5f); mImageView.setImageMatrix(matrix); } // 垂直倾斜的方式二 private void testSkewY2(Bitmap bitmap) { Matrix matrix = new Matrix(); int width =bitmap.getWidth(); int height = bitmap.getHeight(); matrix.setSkew(0, 0.5f); mNewBitmap=Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false); mImageView.setImageBitmap(mNewBitmap); } // 水平且垂直倾斜的方式一 private void testSkewXY1() { Matrix matrix = new Matrix(); matrix.setSkew(0.5f, 0.5f); mImageView.setImageMatrix(matrix); } // 水平且垂直倾斜的方式二 private void testSkewXY2(Bitmap bitmap) { Matrix matrix = new Matrix(); int width =bitmap.getWidth(); int height = bitmap.getHeight(); matrix.setSkew(0.5f, 0.5f); mNewBitmap=Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false); mImageView.setImageBitmap(mNewBitmap); } // 水平对称--图片关于X轴对称 private void testSymmetryX(Bitmap bitmap) { Matrix matrix = new Matrix(); int height =bitmap.getHeight(); float matrixValues[] = { 1f, 0f, 0f, 0f, -1f, 0f, 0f, 0f, 1f }; matrix.setValues(matrixValues); //若是matrix.postTranslate(0, height);//表示将图片上下倒置 matrix.postTranslate(0, height*2); mImageView.setImageMatrix(matrix); } // 垂直对称--图片关于Y轴对 private void testSymmetryY(Bitmap bitmap) { Matrix matrix = new Matrix(); int width=bitmap.getWidth(); float matrixValues[] = {-1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f}; matrix.setValues(matrixValues); //若是matrix.postTranslate(width,0);//表示将图片左右倒置 matrix.postTranslate(width*2, 0); mImageView.setImageMatrix(matrix); } // 关于X=Y对称 private void testSymmetryXY(Bitmap bitmap) { Matrix matrix = new Matrix(); int width = bitmap.getWidth(); int height = bitmap.getHeight(); float matrixValues[] = { 0f, -1f, 0f, -1f, 0f, 0f, 0f, 0f, 1f }; matrix.setValues(matrixValues); matrix.postTranslate(width+height, width+height); mImageView.setImageMatrix(matrix); } }
main.xml如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/imageView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src=http://www.mamicode.com/"@drawable/a">
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。