I'm working with android project in which I want to rotate image along with touch to some fix pivot point. I have completed all these things but I am facing one problem: While I'm trying to rotate image the image bitmap is resized. I dont have any idea why it occurs. If somebody has then please give me an idea to help overcome this problem.
my code:
package com.demo.rotation;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
public class temp extends Activity{
ImageView img1;
float startX;
float startX2 ;
Bitmap source;
Bitmap bitmap1 = null;
double r;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img1 = (ImageView) findViewById(R.id.img1);
img1.setOnTouchListener(img1TouchListener);
bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.orsl_circle_transparent);
}
private OnTouchListener img1TouchListener = new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case M开发者_运维技巧otionEvent.ACTION_MOVE:
Log.d("MOVE", "1");
if(source!=null)
r = Math.atan2(event.getX() - source.getWidth(),
(source.getHeight() / 2) - event.getY());
Log.i("startX" + event.getX(), "startY" + event.getY());
rotate(r,bitmap1, img1);
img1.setScaleType(ScaleType.CENTER);
break;
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
break;
default :
break;
}
return true;
}
};
private void rotate(double r , Bitmap currentBitmap ,ImageView imageView )
{
int rotation = (int) Math.toDegrees(r);
Matrix matrix = new Matrix();
matrix.setRotate(rotation, currentBitmap.getWidth()/2, currentBitmap.getHeight()/2);
source = Bitmap.createBitmap(currentBitmap, 0, 0, currentBitmap.getWidth(), currentBitmap.getHeight(), matrix, false);
imageView.setImageBitmap(source);
Log.i("HIGHT OF CURRENT BITMAP", ""+source.getHeight());
}
}
You don't rotate the image, you rotate the canvas. Scaling is done using setBounds(). See this question for a bit of help.
You're rotating a rectangular bitmap. When you do so, the canvas size has to change in order to accommodate the full image. You would need to manually crop after the rotate to get the function you desire.
The problem has to do with applying your Bitmap to the ImageView. Create a BitmapDrawable first, then apply. There are some scaling problems that occur if you don't. I'm not sure why, maybe someone else could provide insight. I ran into the same problem when creating a compass app and came to this thread on a search, so I thought I would update with the fix that worked.
int rotation = (int) Math.toDegrees(r);
Matrix matrix = new Matrix();
matrix.postRotate(rotation);
source = Bitmap.createBitmap(currentBitmap, 0, 0, currentBitmap.getWidth(), currentBitmap.getHeight(), matrix, false);
*BitmapDrawable newBmd = new BitmapDrawable(source);
imageView.setImageDrawable(newBmd);*
imageView.setScaleType(ScaleType.CENTER);
精彩评论