旋转图片的几种方式笔记

1.矩阵

private void init(Context context) {
   mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
   //加载需要的图片
   mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.black_line);
   bitmapDisplay = mBitmap;
   matrix = new Matrix();
   //获取图片的宽高
   mWidth = mBitmap.getWidth();
   mHeight = mBitmap.getHeight();
}
//  设置旋转比例
public void setAngle(float fAngle) {
    matrix.setRotate(fAngle, mWidth / 2, mHeight / 2);
    invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(bitmapDisplay,matrix, mPaint);
}

2.旋转canvas

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    mBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.black_line)).getBitmap();
    mBitmapHeight = mBitmap.getHeight();
    mBitmapWidth = mBitmap.getWidth();
    int left = (screenWidth - mBitmapWidth) / 2;
    int top = (screenHeight - mBitmapHeight) / 2;
    mSrcRect = new Rect(0, 0, mBitmapWidth, mBitmapHeight);
    Log.d("----------", left + ":" + top);
    mDesRect = new Rect(0, 150, mBitmapWidth, 150+mBitmapHeight);
    canvas.rotate(mDegrees, mBitmapWidth / 2, 150+mBitmapHeight / 2);
    canvas.scale(mScale, mScale, mBitmapWidth / 2, 150+mBitmapHeight / 2);
    canvas.drawBitmap(mBitmap, mSrcRect, mDesRect, mBitPaint);
}

你可能感兴趣的:(android)