使用矩阵设置ImageView



public class MyView extends  View {


public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
// initialize1();  
initialize();  
}


private Bitmap mBitmap;  


private Matrix mMatrix = new Matrix();  


public MyView(Context context) {  


super(context);   


}  
//第一种方式使用原生矩阵
private void initialize1() {       


mBitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.rtf)).getBitmap(); 
//角度大于零代表顺时针旋转
//cosValue,sinValue 旋转的角度的余弦值       平移距离  px,py    受scale影响
float cosValue = (float) Math.cos(Math.PI/6);   
float sinValue = (float) Math.sin(Math.PI/6); 
int  scale=2;
int px=500,py=500;
mMatrix.setValues(  
new float[]{  
cosValue, sinValue, px,  
-sinValue, cosValue, py,  
0, 0, scale});  


}   
//第二种方式使用androidAPI设置矩阵
private void initialize() {  


Bitmap bmp = ((BitmapDrawable)getResources().getDrawable(R.drawable.rtf)).getBitmap();  
mBitmap = bmp;  
//控制缩放比例
mMatrix.setScale(0.5f, 0.5f);  
//平移到(x,y)处  
mMatrix.postTranslate(500, 500);  
/**
* 图像倾斜    

* kx  控制y轴     正数逆时针倾斜
* ky  控制x轴    正数 顺时针倾斜 
* 如果kx=ky  效果与图像旋转 类似
* px py  控制位置
*/ 
mMatrix.postSkew(0.3f, 0.3f, 0,0); 
/**
* 图像旋转     
* degrees 大于零为顺时针
* px py  旋转中心坐标

*/
mMatrix.postRotate(50,500,500);
}  
@Override protected void onDraw(Canvas canvas) {  
//      super.onDraw(canvas);  //如果界面上还有其他元素需要绘制,只需要将这句话写上就行了。  


canvas.drawBitmap(mBitmap, mMatrix, null);  
}  


}


更多转载:http://blog.csdn.net/mengweiqi33/article/details/7446383

猜你喜欢

转载自blog.csdn.net/weiqingsong150/article/details/46273449