public class MainActivity extends Activity {
private ImageView mIv;
float scale=1;
private ImageView mMiv;
private int mCurrentX=0;
private int degrees;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIv = (ImageView) findViewById(R.id.show_iv);
mMiv = (ImageView) findViewById(R.id.show_Mirror_iv);
}
/*
* 放大图片
*/
public void magnifyImg(View view) {
scale+=0.2;
//先找 到将要放大的图片
Bitmap oldMap =BitmapFactory.decodeResource(getResources(), R.drawable.boy);
//新建一个画笔
Paint paint=new Paint();
//新建一个画纸,注明画纸的大小
Bitmap newMap= Bitmap.createBitmap(oldMap.getWidth(), oldMap.getHeight(), Config.RGB_565);
//新建一个画板,把纸放进去
Canvas canvas=new Canvas(newMap);
//设置画板的背景
canvas.drawColor(Color.WHITE);
Matrix matrix=new Matrix();
//设置放大的比例
matrix.setScale(scale, scale);
canvas.drawBitmap(oldMap, matrix, paint);
mIv.setImageBitmap(newMap);
}
/*
* 缩小图片
*/
public void shrinkImg(View view) {
scale-=0.2;
if (scale<=0) {
showToast("已经是最小了");
scale=1;
}
//先找 到将要放大的图片
Bitmap oldMap =BitmapFactory.decodeResource(getResources(), R.drawable.boy);
//新建一个画笔
Paint paint=new Paint();
//新建一个画纸,注明画纸的大小
Bitmap newMap= Bitmap.createBitmap(oldMap.getWidth(), oldMap.getHeight(), Config.RGB_565);
//新建一个画板,把纸放进去
Canvas canvas=new Canvas(newMap);
//设置画板的背景
canvas.drawColor(Color.WHITE);
Matrix matrix=new Matrix();
//设置放大的比例
matrix.setScale(scale, scale);
canvas.drawBitmap(oldMap, matrix, paint);
mIv.setImageBitmap(newMap);
}
/*
* 向下镜像图片
*/
public void verticalMirrorImg(View view) {
//先找 到将要放大的图片
Bitmap oldMap =BitmapFactory.decodeResource(getResources(), R.drawable.boy);
//新建一个画笔
Paint paint=new Paint();
//新建一个画纸,注明画纸的大小
Bitmap newMap= Bitmap.createBitmap(oldMap.getWidth(), oldMap.getHeight(), Config.RGB_565);
//新建一个画板,把纸放进去
Canvas canvas=new Canvas(newMap);
//设置画板的背景
canvas.drawColor(Color.WHITE);
Matrix matrix=new Matrix();
//设置向上翻转
matrix.setScale(1.0f, -1.0f);
//向下移动,一定要用post不然setScale()没有效果
matrix.postTranslate(0, oldMap.getHeight());
canvas.drawBitmap(oldMap, matrix, paint);
mMiv.setImageBitmap(newMap);
}
/*
* 向右移动
*/
public void transRightImg(View view) {
mCurrentX+=5;
//先找 到将要放大的图片
Bitmap oldMap =BitmapFactory.decodeResource(getResources(), R.drawable.boy);
//新建一个画笔
Paint paint=new Paint();
//新建一个画纸,注明画纸的大小
Bitmap newMap= Bitmap.createBitmap(oldMap.getWidth(), oldMap.getHeight(), Config.RGB_565);
//新建一个画板,把纸放进去
Canvas canvas=new Canvas(newMap);
//设置画板的背景
// canvas.drawColor(Color.WHITE);
Matrix matrix=new Matrix();
//向下移动
matrix.setTranslate(mCurrentX, 0);
canvas.drawBitmap(oldMap, matrix, paint);
mIv.setImageBitmap(newMap);
}
/*
* 右移动
*/
public void clockwiseImg(View view) {
degrees+=45;
//先找 到将要放大的图片
Bitmap oldMap =BitmapFactory.decodeResource(getResources(), R.drawable.boy);
//新建一个画笔
Paint paint=new Paint();
//新建一个画纸,注明画纸的大小
Bitmap newMap= Bitmap.createBitmap(oldMap.getWidth(), oldMap.getHeight(), Config.RGB_565);
//新建一个画板,把纸放进去
Canvas canvas=new Canvas(newMap);
//设置画板的背景
// canvas.drawColor(Color.WHITE);
Matrix matrix=new Matrix();
//向右旋转
matrix.setRotate(degrees, oldMap.getWidth()/2, oldMap.getHeight()/2);
canvas.drawBitmap(oldMap, matrix, paint);
mIv.setImageBitmap(newMap);
}
public void showToast(String msg){
Toast.makeText(this, msg, 0).show();
}
}