повернуть изображение вокруг точки - PullRequest
0 голосов
/ 03 июня 2010

У меня есть изображение прямоугольного размера, например, 30 x 60 пикселей. Я хочу повернуть это изображение вокруг нижнего центра изображения, т.е. я хочу установить точку поворота в приведенном выше примере как (15, 60) пикселей.

Я использую drawble и матрицу, чтобы сделать это, что бы я ни старался, я всегда вращался вокруг центра изображения.

Код:

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

// float angle = (angle + 10.0f)% 360.0f; 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); float px =; float py; c.rotate (угол, 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);

             // 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); // linLayout.addView (imageView);

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

Может кто-нибудь дать мне знать, как исправить это?

Ответы [ 2 ]

0 голосов
/ 28 мая 2013

да, с помощью матрицы вы можете вращать изображения. После вызова 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;
    }
 }
}
0 голосов
/ 04 июня 2010

Вы переводите в точку, вокруг которой хотите вращаться, прежде чем делать вращение?

Проверьте этот ресурс: http://blogs.sonyericsson.com/developerworld/2010/05/31/android-tutorial-making-your-own-3d-list-part-2/

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...