How to rotate the image without changing its size? - android

I am developing an android app where I have to rotate a circular image around another circular image, but when I am running this the size of my circular image gets automatically changing continuously. So please help me how to solve this. Here is my code
public class JobSearch extends Activity implements OnTouchListener {
private ImageView dialer;
private float y=0;
public boolean onTouch(View v, MotionEvent event) {
double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY());
int rotation=(int)Math.toDegrees(r);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
//x=event.getX();
y=event.getY();
updateRotation(rotation);
break;
case MotionEvent.ACTION_UP:
break;
}//switch
return true;
}//onTouch
private void updateRotation(double rot){
float newRot=new Float(rot);
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.round_button_big);
Matrix matrix=new Matrix();
matrix.postRotate(newRot);//,bitmap.getWidth()/2,bitmap.getHeight()/2);
Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
dialer.setImageBitmap(reDrawnBitmap);
if(newRot>=-5 && newRot<=5)
Toast.makeText(this,"12 O\'Clock",Toast.LENGTH_SHORT).show();
if(newRot>=85 && newRot<=95)
Toast.makeText(this,"3 O\'Clock",Toast.LENGTH_SHORT).show();
if(newRot>=175 || newRot<=-175)
Toast.makeText(this,"6 O\'Clock",Toast.LENGTH_SHORT).show();
if(newRot>=-95 && newRot<=-85)
Toast.makeText(this,"9 O\'Clock",Toast.LENGTH_SHORT).show();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dialer = (ImageView) findViewById(R.id.big_button);
dialer.setOnTouchListener(this);
}//onCreate
}

This this one; worked for me perfectly.
private Bitmap rotateBitmap(Bitmap bitmap, int rotationAngleDegree){
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int newW=w, newH=h;
if (rotationAngleDegree==90 || rotationAngleDegree==270){
newW = h;
newH = w;
}
Bitmap rotatedBitmap = Bitmap.createBitmap(newW,newH, bitmap.getConfig());
Canvas canvas = new Canvas(rotatedBitmap);
Rect rect = new Rect(0,0,newW, newH);
Matrix matrix = new Matrix();
float px = rect.exactCenterX();
float py = rect.exactCenterY();
matrix.postTranslate(-bitmap.getWidth()/2, -bitmap.getHeight()/2);
matrix.postRotate(rotationAngleDegree);
matrix.postTranslate(px, py);
canvas.drawBitmap(bitmap, matrix, new Paint( Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG ));
matrix.reset();
return rotatedBitmap;
}

Try this code for Rotating Image -
public class bitmaptest extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout linLayout = new LinearLayout(this);
// load the origial BitMap (500 x 500 px)
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.android);
int width = bitmapOrg.width();
int height = bitmapOrg.height();
int newWidth = 200;
int newHeight = 200;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 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);
// add ImageView to the Layout
linLayout.addView(imageView,
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
)
);
// set LinearLayout as ContentView
setContentView(linLayout);
}
}
And, also see this Tutorial

This is my code to rotate image around itself and slowly reduce the speed
rotateAnimation1 = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation1.setInterpolator(new LinearInterpolator());
rotateAnimation1.setDuration(duration);
rotateAnimation1.setRepeatCount(0);
imgBottle.startAnimation(rotateAnimation1);
flagSpinAvailable = false;
rotateAnimation1.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation anim) {
};
public void onAnimationRepeat(Animation anim) {
};
public void onAnimationEnd(Animation anim) {
duration = duration + 70;
startMoving();
};
});

this is the code of rotate the image
Bitmap bMap = BitmapFactory.decodeResource(getResources(),R.drawable.test);
Matrix mat = new Matrix();
mat.postRotate(90);
Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0,
bMap.getWidth(), bMap.getHeight(), mat, true);
BitmapDrawable bmd = new BitmapDrawable(bMapRotate);
image.setImageBitmap(bMapRotate);
image.setImageDrawable(bmd);

Related

android : Why I couldn't rotate antiClockwise?

In my App I should rotate a picture like hand of clock clockwise and anticlockwise around bottom center.
I use this code :
private ImageView myImageView;
private float curScale = 1F;
private float curRotate = 70;
private Bitmap bitmap;
private int bmpHeight;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myImageView = (ImageView) findViewById(R.id.imageview);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.teste);//here you insert your image
bmpHeight = bitmap.getHeight();
drawMatrix();
}
private void drawMatrix() {
Matrix matrix = new Matrix();
Matrix matrixb = new Matrix();
Matrix matrixc = new Matrix();
matrix.postScale(curScale, curScale);
matrixb.setRotate(curRotate, 100, bmpHeight);
matrixc.setTranslate(100, 0);
matrix.setConcat(matrixb, matrixc);
Bitmap targetBitmap = Bitmap.createBitmap(200, bmpHeight,
bitmap.getConfig());
Canvas canvas = new Canvas(targetBitmap);
canvas.drawBitmap(bitmap, matrix, new Paint());
myImageView.setImageBitmap(targetBitmap);
}
But I couldn't rotate it anticlockwise because the width of picture change.
is there any solution?
Thanks in Advance.

Rotate an image by 90,180,270 and 360 degrees every time a button is clicked and retain the rotation degree in portrait and landscape mode?

I am making an app in android where in an image has to be rotated whenever rotate button is clicked by 90,180,270 and 360 degrees. The rotation has to be the same in portrait and in landscape mode.
I am a beginner in Android Programming and I have basic knowledge of Java.
Kindly, help me soon.
The code that I have used is as below.I can only rotate it 90 degrees once. How do it continue with it?
btn_rotate = (ImageButton) findViewById(R.id.btn_rotate);
btn_rotate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bitmap bmap = BitmapFactory.decodeResource(getResources(), R.drawable.about_us_ipad_p);
Display d = getWindowManager().getDefaultDisplay();
#SuppressWarnings("deprecation")
int x = d.getWidth();
#SuppressWarnings("deprecation")
int y = d.getHeight();
ImageView imgView = (ImageView) findViewById(R.id.imgViewEdit_Pic);
Bitmap scaledBmap = Bitmap.createScaledBitmap(bmap, y, x, true);
Matrix matrix = new Matrix();
matrix.postRotate(90, 180, 270);
Bitmap rotatedBmap = Bitmap.createBitmap(scaledBmap,0,0,scaledBmap.getWidth(),scaledBmap.getHeight(),matrix,true);
imgView.setImageBitmap(rotatedBmap);
}
});
this piece of code may help you and it is self explanatory...
public class TestActivity extends Activity {
private int rotation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
rotation = savedInstanceState.getInt("ANGLE");
}
final ImageView imageView = (ImageView) findViewById(R.id.imageView);
final Button button = (Button) findViewById(R.id.iv_icon);
DisplayMetrics metrics = getResources().getDisplayMetrics();
final int width = metrics.widthPixels;
final int height = metrics.heightPixels;
final Bitmap imageBitmap = BitmapFactory.decodeResource(
getResources(), R.drawable.image1);
final Bitmap scaledBitmap = Bitmap.createScaledBitmap(imageBitmap, width, height, true);
imageView.setImageBitmap(getRotatedBitmap(scaledBitmap));
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
rotation += 90;
rotation %= 360;
Bitmap bitmap = getRotatedBitmap(scaledBitmap);
imageView.setImageBitmap(bitmap);
}
});
}
private Bitmap getRotatedBitmap(Bitmap bitmap) {
if (rotation % 360 == 0) {
return bitmap;
}
Matrix matrix = new Matrix();
matrix.postRotate(rotation, bitmap.getWidth() / 2,
bitmap.getHeight() / 2);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight() / 2, matrix, true);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("ANGLE", rotation);
super.onSaveInstanceState(outState);
}
}
yourimage.setRotation(yourimage.getRotation() + 90);
In manifest file, lock your screen orientation like this:
android:screenOrientation="portrait"
or
android:screenOrientation="landscape"

Android: How to rotate my Bitmap using accelerometer?

i want to rotate my bitmap img which is a car steering using one axis of accelerometer. I have no idea how i can use it with matrix or calling the sensorchanged method in other class. My app is in landscape mode and i have two classes one for sensor and one for bitmap. I am completely lost.
public class CustomDrawableView extends View {
AnimationSteeringActivity sens = new AnimationSteeringActivity();
public CustomDrawableView(Context context) {
// TODO Auto-generated constructor stub
super(context);
}
#Override
public void onDraw(Canvas canvas) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.steering);
int canvasWidth = canvas.getWidth();
int canvasHeight = canvas.getHeight();
int bitmapWidth = bmp.getWidth();
int bitmapHeight = bmp.getHeight();
int centreX = (canvasWidth - bitmapWidth) / 2;
int centreY = (canvasWidth - bitmapHeight) / 2;
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(bmp, centreX, centreY, null);
and
public class AnimationSteeringActivity extends Activity implements
SensorEventListener {
/** Called when the activity is first created. */
/** Called when the activity is first created. */
CustomDrawableView mCustomDrawableView = null;
ShapeDrawable mDrawable = new ShapeDrawable();
public static int x;
public static int y;
private SensorManager sensorManager = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get a reference to a SensorManager
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mCustomDrawableView = new CustomDrawableView(this);
setContentView(mCustomDrawableView);
// setContentView(R.layout.main);
}
// This method will update the UI on new sensor events
public void onSensorChanged(SensorEvent sensorEvent) {
{
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
// the values you were calculating originally here were over
// 10000!
x = (int) Math.pow(sensorEvent.values[1], 2);
y = (int) Math.pow(sensorEvent.values[2], 2);
}
if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
}
}
}
Look for doing actually rotation at this link
rotating a bitmap
so in the onSensorChanged method there you will rotate with the wanted x or y value depending on what you want
Bitmap myBitmap = BitmapFactory.decodeStream(downloadImageFromWeb());
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.android1);
Display d = getWindowManager().getDefaultDisplay();
int x = d.getWidth();
int y = d.getHeight();
ImageView img1 = (ImageView)findViewById(R.id.img1);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, y, x, true);
Matrix matrix = new Matrix();
matrix.postRotate(-90);
Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
img1.setImageBitmap(rotatedBitmap);

Resize Bitmap image?

I want to change the image size when changing screen orientation. I tried to use the following code, but it does not work.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
myImage = Bitmap.createBitmap(BitmapFactory
.decodeResource(this.getResources(), R.drawable.pic));
Display d = ((WindowManager)getSystemService(WINDOW_SERVICE))
.getDefaultDisplay();
int ScreenHeight = d.getHeight();
int ScreenWidth = d.getWidth();
Bitmap ScaledImage = Bitmap.createScaledBitmap(myImage , ScreenWidth, ScreenHeight,
true);
imageview = (ImageView)findViewById(R.id.imageView2);
imageview.setImageBitmap(ScaledImage);
}
}
Below is the code to rotate or re size your image in android
public class bitmaptest extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout linLayout = new LinearLayout(this);
// load the origial BitMap (500 x 500 px)
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.android);
int width = bitmapOrg.width();
int height = bitmapOrg.height();
int newWidth = 200;
int newHeight = 200;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 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);
// add ImageView to the Layout
linLayout.addView(imageView,
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
)
);
// set LinearLayout as ContentView
setContentView(linLayout);
}
}

adding rotate buttons - android map app

I currently working on a map application for the android.
the android emulator doesn't support multi touch so I cant rotate the map.
can someone refer me to where I can learn how to rotate the map and how to add the buttons ?
public class bitmaptest extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout linLayout = new LinearLayout(this);
// load the origial BitMap (500 x 500 px)
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.android);
int width = bitmapOrg.width();
int height = bitmapOrg.height();
int newWidth = 200;
int newHeight = 200;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 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);
// add ImageView to the Layout
linLayout.addView(imageView,
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
)
);
// set LinearLayout as ContentView
setContentView(linLayout);
}
}

Categories

Resources