简体   繁体   English

将图像旋转一个点

[英]rotate an image aroound a point

I have an image which is of rectangular dimension, eg 30 x 60 pixels I want to rotate this image around the bottom center of the image, ie i want to set the pivot in the above example as (15, 60 )pixel. 我有一个具有矩形尺寸的图像,例如30 x 60像素,我想将此图像绕图像的底部中心旋转,即,我想在上述示例中将枢轴设置为(15,60)像素。

I am using a drawble and matrix to get this done, whatever i try i always end up rotating around center of the image. 我正在使用drawble和矩阵来完成此操作,无论我尝试什么,我总是最终围绕图像中心旋转。

Code is : 代码是:

Bitmap bitmapOrg = BitmapFactory.decodeFile("/sdcard/DCIM/2010-06-01_15-32-42_821.jpg");

// float angle = (angle + 10.0f)%360.0f; //浮动角度=(角度+ 10.0f)%360.0f; if(null !=bitmapOrg) { if(null!= bitmapOrg){

             int width = bitmapOrg.getWidth();
             int height = bitmapOrg.getHeight();
             int newWidth = 15;
             int newHeight = 15;

             // calculate the scale - in this case = 0.4f
             float scaleWidth = ((float) newWidth) / width;
             float scaleHeight = ((float) newHeight) / height;

/* Canvas c = new Canvas(bitmapOrg); / * Canvas c = new Canvas(bitmapOrg); float px = ; float px =; float py; 浮动py; c.rotate(angle, px, py)*/ c.rotate(angle,px,py)* /

             // createa matrix for the manipulation
             Matrix matrix = new Matrix();
             // resize the bit map
             matrix.postScale(scaleWidth, scaleHeight);
             // rotate the Bitmap

// matrix.postRotate(45); // matrix.postRotate(45);

             // recreate the new Bitmap
             Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
                               width, height, matrix, true); 

             // make a Drawable from Bitmap to allow to set the BitMap 
             // to the ImageView, ImageButton or what ever
             BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

             ImageView imageView = new ImageView(this);

             // set the Drawable on the ImageView
             imageView.setImageDrawable(bmd);

             // center the Image
             imageView.setScaleType(ScaleType.CENTER);

// imageView.layout(100, 300, 0, 0); // imageView.layout(100,300,0,0); // linLayout.addView(imageView); // linLayout.addView(imageView);

             // add ImageView to the Layout
             linLayout.addView(imageView, 
                new AbsoluteLayout.LayoutParams(
                           LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 10, 30
                     )
             );

can anyone let me know how to get this rectified? 谁能让我知道如何纠正这个问题?

yes with the help of matrix you can rotate the images. 是的,借助矩阵,您可以旋转图像。 After calling RotateBitmap() you can get the bitmap at any time using getBitmap(). 调用RotateBitmap()之后,您可以随时使用getBitmap()获取位图。

public class RotateBitmap {

public static final String  TAG = "RotateBitmap";
private Bitmap                  mBitmap;
private int                     mRotation;
private int                     mWidth;
private int                     mHeight;
private int                     mBitmapWidth;
private int                     mBitmapHeight;

public RotateBitmap( Bitmap bitmap, int rotation )
{
    mRotation = rotation % 360;
    setBitmap( bitmap );
}

public void setRotation( int rotation )
{
    mRotation = rotation;
    invalidate();
}

public int getRotation()
{
    return mRotation % 360;
}

public Bitmap getBitmap()
{
    return mBitmap;
}

public void setBitmap( Bitmap bitmap )
{
    mBitmap = bitmap;

    if ( mBitmap != null ) {
        mBitmapWidth = bitmap.getWidth();
        mBitmapHeight = bitmap.getHeight();
        invalidate();
    }
}

private void invalidate()
{
    Matrix matrix = new Matrix();
    int cx = mBitmapWidth / 2;
    int cy = mBitmapHeight / 2;
    matrix.preTranslate( -cx, -cy );
    matrix.postRotate( mRotation );
    matrix.postTranslate( cx, cx );

    RectF rect = new RectF( 0, 0, mBitmapWidth, mBitmapHeight );
    matrix.mapRect( rect );
    mWidth = (int)rect.width();
    mHeight = (int)rect.height();
}

public Matrix getRotateMatrix()
{
    Matrix matrix = new Matrix();
    if ( mRotation != 0 ) {
        int cx = mBitmapWidth / 2;
        int cy = mBitmapHeight / 2;
        matrix.preTranslate( -cx, -cy );
        matrix.postRotate( mRotation );
        matrix.postTranslate( mWidth / 2, mHeight / 2 );
    }

    return matrix;
}

public int getHeight()
{
    return mHeight;
}

public int getWidth()
{
    return mWidth;
}

public void recycle()
{
    if ( mBitmap != null ) {
        mBitmap.recycle();
        mBitmap = null;
    }
 }
}

Are you translating to the point you want to rotate around before doing the rotation? 在进行旋转之前,是否要平移到要旋转的点?

Check out this resource: http://blogs.sonyericsson.com/developerworld/2010/05/31/android-tutorial-making-your-own-3d-list-part-2/ 查看此资源: http : //blogs.sonyericsson.com/developerworld/2010/05/31/android-tutorial-making-your-own-3d-list-part-2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM