Matrix的操作,总共分为translate(平移),rotate(旋转),scale(缩放)和skew(倾斜)四种,每一种变换在
Android的API里都提供了set, post和pre三种操作方式,除了translate,其他三种操作都可以指定中心点。
set是直接设置Matrix的值,每次set一次,整个Matrix的数组都会变掉。
post是后乘,当前的矩阵乘以参数给出的矩阵。可以连续多次使用post,来完成所需的整个变换。例如,要将一个图片旋
转30度,然后平移到(100,100)的地方,那么可以这样做:
- Bitmap bmp = ((BitmapDrawable)getResources().getDrawable(R.drawable.show)).getBitmap();
- mBitmap = bmp;
- /*首先,将缩放为100*100。这里scale的参数是比例。有一点要注意,如果直接用100/
- bmp.getWidth()的话,会得到0,因为是整型相除,所以必须其中有一个是float型的,直接用100f就好。*/
- mMatrix.setScale(100f/bmp.getWidth(), 100f/bmp.getHeight());
- //平移到(100,100)处
- mMatrix.postTranslate(100, 100);
- //倾斜x和y轴,以(100,100)为中心。
- mMatrix.postSkew(0.2f, 0.2f, 100, 100);
Matrix matrix = new Matrix();
matrix.setRotate(90);
使用下面的方式也可以实现垂直镜像:
Matrix matrix = new Matrix();
matrix.setScale (1.0,-1.0);
matrix.postTraslate(0, fHeight);
缩放
- //指定矩阵(x轴不变,y轴相反) 就是把图片倒过来
- matrix.preScale(1, -1);
android图片缩小和放大Matrix
public class ex04_22 extends Activity{
}
- public class MyView extends View {
- private Bitmap mBitmap;
- private Matrix mMatrix = new Matrix();
- public MyView(Context context) {
- super(context);
- initialize();
- }
- private void initialize() {
- Bitmap bmp = ((BitmapDrawable)getResources().getDrawable(R.drawable.show)).getBitmap();
- mBitmap = bmp;
- /*首先,将缩放为100*100。这里scale的参数是比例。有一点要注意,如果直接用100/
- bmp.getWidth()的话,会得到0,因为是整型相除,所以必须其中有一个是float型的,直接用100f就好。*/
- mMatrix.setScale(100f/bmp.getWidth(), 100f/bmp.getHeight());
- //平移到(100,100)处
- mMatrix.postTranslate(100, 100);
- //倾斜x和y轴,以(100,100)为中心。
- mMatrix.postSkew(0.2f, 0.2f, 100, 100);
- }
- @Override protected void onDraw(Canvas canvas) {
- // super.onDraw(canvas); //如果界面上还有其他元素需要绘制,只需要将这句话写上就行了。
- canvas.drawBitmap(mBitmap, mMatrix, null);
- }
- }
- public class MatrixActivity extends Activity{
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setTitle("eoeAndroid教程: 缩放和旋转图片 -by:IceskYsl");
- LinearLayout linLayout = new LinearLayout(this);
- // 加载需要操作的图片,这里是eoeAndroid的logo图片
- Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
- R.drawable.blackk);
- //获取这个图片的宽和高
- int width = bitmapOrg.getWidth();
- int height = bitmapOrg.getHeight();
- //定义预转换成的图片的宽度和高度
- int newWidth = 200;
- int newHeight = 200;
- //计算缩放率,新尺寸除原始尺寸
- float scaleWidth = ((float) newWidth) / width;
- float scaleHeight = ((float) newHeight) / height;
- // 创建操作图片用的matrix对象
- Matrix matrix = new Matrix();
- // 缩放图片动作
- matrix.postScale(scaleWidth, scaleHeight);
- //旋转图片 动作
- matrix.postRotate(45);
- // 创建新的图片
- Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
- width, height, matrix, true);
- //将上面创建的Bitmap转换成Drawable对象,使得其可以使用在ImageView, ImageButton中
- BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
- //创建一个ImageView
- ImageView imageView = new ImageView(this);
- // 设置ImageView的图片为上面转换的图片
- imageView.setImageDrawable(bmd);
- //将图片居中显示
- imageView.setScaleType(ScaleType.CENTER);
- //将ImageView添加到布局模板中
- linLayout.addView(imageView,
- new LinearLayout.LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
- )
- );
- // 设置为本activity的模板
- setContentView(linLayout);
- }
- }