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.
Related
I want to create a bitmap from an imageView, but I first need to rotate my imageView before converting the bitmap. I use the following code to convert the imageView to Bitmap.
BitmapDrawable drawable = (BitmapDrawable) imgPhoto.getDrawable();
Bitmap photo = drawable.getBitmap();
The imageView implements a rotation gesture. I want to be able to rotate the imageView and then create a Bitmap using the rotated imageView. However, it seems my Bitmap is still getting the original imageView instead of the rotated position. The following is the complete code. Thank you.
public class MainActivity extends AppCompatActivity implements RotationGestureDetector.OnRotationGestureListener{
private ImageView imgPhoto, imgBackground, imgCombine;
private RotationGestureDetector mRotationDetector;
private Button btnCombine;
private float angle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRotationDetector = new RotationGestureDetector(this);
imgPhoto = (ImageView) findViewById(R.id.imgPhoto);
imgBackground = (ImageView) findViewById(R.id.imgBackground);
collageImage = (ImageView) findViewById(R.id.imgCombine);
btnCombine = (Button)findViewById(R.id.btnCombineImage);
btnCombine.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BitmapDrawable drawable = (BitmapDrawable) imgPhoto.getDrawable();
Bitmap photo = drawable.getBitmap();
}
});
}
#Override
public boolean onTouchEvent(MotionEvent event){
mRotationDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
#Override
public void OnRotation(RotationGestureDetector rotationDetector) {
angle = rotationDetector.getAngle();
Log.d("RotationGestureDetector", "Rotation: " + Float.toString(angle));
imgPhoto.setRotation(imgPhoto.getRotation() + (-angle));
}
}
To rotate ImageView:
Matrix matrix = new Matrix();
imageView.setScaleType(ImageView.ScaleType.MATRIX); //required
matrix.postRotate((float) angle, pivotX, pivotY);
imageView.setImageMatrix(matrix);
Source: https://stackoverflow.com/a/10104318/7639113
Then you can create the bitmap by using DrawingCache,
imageView.setDrawingCacheEnabled(true);
Bitmap bitmap = imageView.getDrawingCache();
Try using this code after the onClick. It should provide a rotated bitmap with the angle specified.
Matrix matrix = new Matrix();
matrix.postRotate(angle);
Bitmap rotatedPhoto = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
The rotate effect is actually applied at the Canvas level. So, what you can do is get the current Canvas augmentations and then draw your Bitmap into a new one with the changes.
Matrix transformMatrix = imgPhoto.getImageMatrix()
Bitmap original = photo;
Bitmap adjusted = Bitmap.createBitmap(original.getWidth(),
original.getHeight(),
original.getConfig());
Canvas canvas = new Canvas(adjusted);
canvas.setMatrix(transformMatrix);
canvas.drawBitmap(original, 0, 0, null);
//at this point adjusted bitmap contains the rotated image
I draw a bitmap image into a canvas with this code:the bitmap get from user galley
public class CanvasView extends View {
Bitmap canvasBitmap;
Canvas drawCanvas;
public CanvasView(Context context,AttributeSet attrs) {
super(context,attrs);
}
public void setCanvasPath(String bitmap_path) {
BitmapFactory.Options decode_options = new BitmapFactory.Options();
decode_options.inMutable = true;
canvasBitmap = BitmapFactory.decodeFile(bitmap_path,decode_options);
drawCanvas = new Canvas(canvasBitmap);
invalidate();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (canvasBitmap != null) {
Matrix matrix=new Matrix();
Matrix m = drawCanvas.getMatrix();
RectF drawableRect = new RectF(0, 0, canvasBitmap.getWidth(), canvasBitmap.getHeight());
RectF viewRect = new RectF(0, 0, drawCanvas.getWidth(),drawCanvas.getHeight());
m.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
canvas.drawBitmap(canvasBitmap,m , null);
}
}
}
but when i draw the bitmap it get out of the canvas view,i don't want to stretch the image,i want to my canvas act like image view in android.
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"
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);
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);