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, i.e
i want to set the pivot in the above example as (15, 60 )pixel.
I am using a drawble and matrix to get this done,
whatever i try i always end up rotating around center of the image.
Code is :
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(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);
// 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
)
);
can anyone let me know how to get this rectified?
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/
yes with the help of matrix you can rotate the images. After calling RotateBitmap() you can get the bitmap at any time using 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;
}
}
}
Related
So I am trying to scale down a bitmap based on the size of a surface view I have written the following code
public Bitmap scaleBitMap(int width, int height, Bitmap b){
// scale down based on the size of the Surface view.
// width and height = canvas.getHeight(), canvas.getWidth();
// this should set the height and width of the bitmap based on a percentage
// the bit map is returned and then displayed by canvas.drawBitmap(scaleBitMap(canvas.getHeight(),canvas.getWidth(),bitmapHere)....
Bitmap bitmap = null;
try{
bitmap = Bitmap.createScaledBitmap(b, (height/100 * 10), (width/100 * 10), true);
}catch(Exception e){
}
return bitmap;
}
Ok so this code works! returns a scaled bitmap.
I am not sure what are you trying to do, but i have done similar thing before. This code scales bitmap to match screensize. Maybe it will help you.
public Bitmap scaleBitMap(int width, int height, Bitmap b){
// scale down based on the size of the Surface view.
// width and height = canvas.getHeight(), canvas.getWidth();
// this should set the height and width of the bitmap based on a percentage
// the bit map is returned and then displayed by canvas.drawBitmap(scaleBitMap(canvas.getHeight(),canvas.getWidth(),bitmapHere)....
Bitmap bitmap = null;
try{
float scaledvalues[] = scale(b.getWidth(), b.getHeight(), width, height);
bitmap = Bitmap.createScaledBitmap(b, scaledvalues[1], scaledvalues[0], true);
}catch(Exception e){
}
return bitmap;
}
public float[] scale(int bitmapWidth, int bitmapHeight, int viewWidth, int viewHeight){
float scaledheight = -1f;
float scaledwidth = -1f;
float scaledheightpros = -1f;
float scaledwidthpros = -1f;
float finalheight = -1f;
float finalwidth = -1f;
if(bitmapHeight > viewHeight){
scaledheight = bitmapHeight - viewHeight;
float s = scaledheight*100f;
scaledheightpros = s / 100f;
}
if(bitmapWidth > viewWidth){
scaledwidth = bitmapWidth - viewWidth;
float z = scaledwidth * 100f;
scaledwidthpros = z / bitmapWidth;
}
if(scaledheightpros > scaledwidthpros){
float a = bitmapHeight/100f;
float b = bitmapWidth/100f;
finalheight = bitmapHeight - (a * scaledheightpros);
finalwidth = bitmapWidth - (b * scaledheightpros);
}
else{
float a = bitmapHeight/100f;
float b = bitmapWidth/100f;
finalheight = bitmapHeight - (a * scaledwidthpros);
finalwidth = bitmapWidth - (b * scaledwidthpros);
}
float array[] = {finalwidth, finalheight};
return array;
}
It works.. but perhaps i should check my math!
here is the working code....
public Bitmap scaleBitMap(int width, int height, Bitmap b){
// scale down based on the size of the Surface view.
// width and height = canvas.getHeight(), canvas.getWidth();
// this should set the height and width of the bitmap based on a percentage
// the bit map is returned and then displayed by canvas.drawBitmap(scaleBitMap(canvas.getHeight(),canvas.getWidth(),bitmapHere)....
Bitmap bitmap = null;
try{
bitmap = (Bitmap) createScaledBitmap(b, (height/100 * 22), (width/100 * 22), false);
}catch(Exception e){
Log.d("Bitmap","Issue: "+e);
}
return bitmap;
}
I am trying to rotate image to some degree and fill background to red color. For example I have a image and want to rotate minute niddle to 15 min(45 degree) and show that quarter circle with red color.
I used following code with rotate my rotate to some angle but i am not able to fill background color. Please help.
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);
Thanks
you can rotate the bitmap with matrix also.
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;
}
}
}
I use image view to display picture of customer and I use this command to scale my images in the image view
iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
but after this command the heads of the customers have been cut partially so I want to scale the images to buttons after center crop command so the bottom of image will be cut
There's no out of the box solution for your requirements.
You need to do the scaling yourself. Here's how you do it:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// we need to wait till the layout has been inflated to get the size of the ImageView
final ImageView iv = (ImageView) findViewById(R.id.image);
iv.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
scaleBitmap(iv);
}
}
);
}
private void scaleBitmap(ImageView iv) {
// get ImageView and determine width & height
int imageWidth = iv.getWidth();
int imageHeight = iv.getHeight();
Rect imageRect = new Rect(0, 0, imageWidth, imageHeight);
// determine destination rectangle
BitmapDrawable drawable = (BitmapDrawable)iv.getDrawable();
Bitmap sourceBitmap = drawable.getBitmap();
Rect bitmapRect = new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
// determine source rectangle
Rect sourceRect = computeSourceRect(imageRect, bitmapRect);
// here's where we do the magic
Bitmap scaledBitmap = Bitmap.createBitmap(imageRect.width(), imageRect.height(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(scaledBitmap);
canvas.drawBitmap(sourceBitmap, sourceRect, imageRect, new Paint());
iv.setImageBitmap(scaledBitmap);
}
private Rect computeSourceRect(Rect imageRect, Rect bitmapRect) {
float imageWidth = (float) imageRect.width();
float imageHeight = (float) imageRect.height();
float bitmapWidth = (float) bitmapRect.width();
float bitmapHeight = (float) bitmapRect.height();
float aspectRatioImage = imageWidth / imageHeight;
float aspectRatioBitmap = bitmapWidth / bitmapHeight;
if (aspectRatioImage<aspectRatioBitmap) {
float newWidth = bitmapHeight * aspectRatioImage;
float widthOffset = (bitmapWidth - newWidth) / 2f;
return new Rect((int) widthOffset, 0, (int) (widthOffset + newWidth), (int) bitmapHeight);
}
else {
float newHeight = bitmapWidth / aspectRatioImage;
float heightOffset = (bitmapHeight - newHeight) / 2f;
// return this for center_crop
//return new Rect(0, (int) heightOffset, (int) bitmapWidth, (int) (heightOffset + newHeight));
// return this for bottom align
return new Rect(0, 0, (int) bitmapWidth, (int) newHeight);
}
}
This basically scales a Bitmap to an ImageView according to your requirements (can easily be modified to work with a Button). You can do any scaling possible that way.
I want implement move a sprite from position (x ,y ) to position action_down (x1 , y1) .But I can't rotate it .Please help me .Thanks
This is my code:
public Sprite(GameView gameView, Bitmap bmp) {
this.gameView = gameView;
this.bmp = bmp;
this.width = bmp.getWidth() / BMP_COLUMNS;// create width, height
this.height = bmp.getHeight() / BMP_ROWS;
Random rnd = new Random(System.currentTimeMillis());
x = rnd.nextInt(gameView.getWidth() - bmp.getWidth());
y = rnd.nextInt(gameView.getHeight() - bmp.getHeight());
}
public void onDraw(Canvas canvas) {
Matrix matrix = new Matrix();
matrix.postTranslate(x, y);
float dx = x1-x;
float dy = y1-y;
float d = (float)Math.sqrt(dx*dx+dy*dy);
vx = (float) (dx*5/d)/3 ;
vy = (float) (dy*5/d)/3 ;
if(k==1){
x += vx ;
y += vy ;
}
currentFrame = ++currentFrame % BMP_COLUMNS;
int srcX = currentFrame * width;
int srcY = 0 * height;
Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
Rect dst = new Rect(x, y, x + width, y + height);
canvas.drawBitmap(bmp, src, dst, null);
}
You should look at matrix.postRotate or canvas.rotate.
Here you go:
Note: you need to convert from Bitmap to Image.
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
/**
* Created by Chris on 3/28/2014.
*/
public class Sprite {
private Image i;
public Sprite(Image image) {
this.i = image;
}
private BufferedImage image = null;
private Graphics2D graphics = null;
public void onDraw(Canvas canvas) {
if(image == null || graphics == null) {
setup();
}
Graphics g = canvas.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
//Where to draw the Sprite on the canvas.
int x = 100;
int y = 100;
//Because graphics is an instance of Graphics2D
//Converts the degrees "45" to radians.
double rotationAngle = Math.toRadians(45);
double locX = image.getWidth() / 2;
double locY = image.getHeight() / 2;
AffineTransform tx = AffineTransform.getRotateInstance(rotationAngle, locX, locY);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
graphics.drawImage(op.filter(image, null), 0, 0, null);
g.drawImage(image, x, y, (int) (image.getWidth() / 2), (int) (image.getHeight() / 2), null);
}
/**
* Sets the Image up.
*/
private void setup() {
if(image != null) {
image.flush();
image = null;
}
if(graphics != null) {
graphics.dispose();
graphics = null;
}
image = new BufferedImage(i.getWidth(null) * 2, i.getHeight(null) * 2, BufferedImage.TYPE_INT_ARGB);
graphics = image.createGraphics();
}
}
I want to rotate bitmap in my app. When i am going to rotate bitmap, bitmap size also changed.Please help me..
I tried with following Methods,but failed:
1st Method:
public Bitmap rotate(Bitmap bitmapOrg) {
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
Matrix matrix = new Matrix();
matrix.postRotate(45);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
bitmapOrg = resizedBitmap;
ImageView imageView = new ImageView(mContext);
imageView.setImageDrawable(bmd);
imageView.setScaleType(ScaleType.CENTER);
bitmapOrg=((BitmapDrawable)imageView.getDrawable()).getBitmap();
return bitmapOrg;
}
float totalRotated = 0;
public Bitmap rotate(Bitmap mBitmap,float degrees){
// compute the absolute rotation
totalRotated = (totalRotated + degrees) % 360;
// precompute some trig functions
double radians = Math.toRadians(totalRotated);
double sin = Math.abs(Math.sin(radians));
double cos = Math.abs(Math.cos(radians));
// figure out total width and height of new bitmap
int newWidth = (int) (mBitmap.getWidth() * cos + mBitmap.getHeight() * sin);
int newHeight = (int) (mBitmap.getWidth() * sin + mBitmap.getHeight() * cos);
// set up matrix
matrix.reset();
matrix.postTranslate((newWidth - mBitmap.getWidth()) / 2, (newHeight - mBitmap.getHeight()) / 2);
matrix.setRotate(totalRotated);
// create new bitmap by rotating mBitmap
Bitmap rotated = Bitmap.createBitmap(mBitmap, 0, 0,newWidth, newHeight, matrix, true);
return rotated;
}