Ich habe den folgenden Code verwendet, um das Bild zu drehen. Es gibt kein Problem bei der Bildrotation, aber wenn ich das Bild drehe, werden die Bildpixel reduziert. Wenn ich kontinuierlich drehe, verschwindet das Bild. Wie kann ich dieses Problem lösen.
main.xml
ImageResizeTestActivity.java
public class ImageResizeTestActivity extends Activity {
/** Called when the activity is first created. */
Button click;
ImageView img;
static File rotated_File;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
click = (Button) findViewById(R.id.button1);
img = (ImageView) findViewById(R.id.imageView1);
String fname = "Rotated_Image.jpg";
rotated_File = new File("/sdcard/" + fname);
click.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (!(rotated_File.exists())) {
Log.v("Inside if", "if");
rotateImage();
} else {
Log.v("Inside else", "else");
rotateImage(rotated_File.getAbsolutePath());
}
}
});
}
protected void rotateImage(String absolutePath) {
// TODO Auto-generated method stub
Bitmap myImg = BitmapFactory.decodeFile(absolutePath);
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(),
myImg.getHeight(), matrix, true);
saveImage_Rotate(rotated);
img.setImageBitmap(rotated);
}
protected void rotateImage() {
// TODO Auto-generated method stub
Bitmap myImg = BitmapFactory.decodeResource(getResources(),
R.drawable.test);
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(),
myImg.getHeight(), matrix, true);
saveImage_Rotate(rotated);
img.setImageBitmap(rotated);
}
static void saveImage_Rotate(Bitmap dest2) {
if (rotated_File.exists())
rotated_File.delete();
try {
FileOutputStream out = new FileOutputStream(rotated_File);
dest2.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
nach der Drehung