2013-05-27 31 views
1

我正在尝试将图像旋转到某种程度,并将背景填充为红色。例如,我有一个图像,并希望将分钟旋转至15分钟(45度),并显示带有红色的四分之一圆圈。如何旋转背景颜色的图像?

我用下面的代码旋转我的旋转到某个角度,但我不能填充背景颜色。请帮忙。

RotateAnimation rotateAnimation = new RotateAnimation(
       (1 - 1) * 6, 10 * 6, 
       Animation.RELATIVE_TO_SELF, 0.5f, 
       Animation.RELATIVE_TO_SELF, 0.5f); 

     rotateAnimation.setInterpolator(new LinearInterpolator()); 
     rotateAnimation.setDuration(5000); 
     rotateAnimation.setFillEnabled(true); 
     rotateAnimation.setFillBefore(true); 
     rotateAnimation.setBackgroundColor(Color.RED); 
     orgClockImage.startAnimation(rotateAnimation); 

感谢

+0

你使用的是listview吗?如果是的话,这个链接可能会有所帮助http://stackoverflow.com/questions/15593152/rotating-images-in-listview – 2013-05-27 07:41:07

+0

不,我不使用listview。我有一个像时钟的imageview,我想旋转分钟niddle – Monali

回答

0

可以旋转通过矩阵也是位图。

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

不适合我 – Monali