Matrix控制变换

Matrix控制变换_第1张图片

实例,复制一张图片到res/drawable-mdpi文件夹下

MyView.java

package com.example.matrixtest;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;


public class MyView extends View{


private Bitmap bitmap;

private Matrix matrix=new Matrix();

public MyView(Context context) {
super(context);
bitmap=((BitmapDrawable) context.getResources().getDrawable(R.drawable.jujishou)).getBitmap();
}




@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

//平移  matrix.setTranslate(30, 30);

//倾斜 matrix.setSkew(1f, 0);


//旋转  matrix.setRotate(30f);

//缩放
matrix.setScale(0.1f, 0.5f);

//绘制新位图
canvas.drawBitmap(bitmap, matrix, null);


}
}

MainActivity.java

package com.example.matrixtest;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(MainActivity.this));
}


}

你可能感兴趣的:(Matrix)