我希望每次单击按钮时将图像旋转90度。这部分效果很好:
public void rotateImage(View view) {
mCurrRotation %= 360;
float fromRotation = mCurrRotation;
float toRotation = mCurrRotation += 90;
final RotateAnimation rotateAnim = new RotateAnimation(
fromRotation, toRotation, mImageView.getWidth()/2, mImageView.getHeight()/2);
rotateAnim.setDuration(1000); // Use 0 ms to rotate instantly
rotateAnim.setFillAfter(true); // Must be true or the animation will reset
mImageView.startAnimation(rotateAnim);
这会将图像转换为byteArray,以便将其保存到SQLite数据库中:
bitmapDrawable = (BitmapDrawable) mImageView.getDrawable();
Bitmap imageBitmap = bitmapDrawable.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] image = stream.toByteArray();
但图像会在旋转前保存。
答案 0 :(得分:0)
通过在画布上临时保存Drawable并使用外部方法来实现:
public void rotateImage(View view) {
try{
rotateViewto90();
}catch (Exception e){
Toast ToastMessage = Toast.makeText(getApplicationContext(), "There is no Image for rotation", Toast.LENGTH_LONG);
View toastView = ToastMessage.getView();
toastView.setBackgroundResource(R.drawable.toast_background_color_editor_act);
ToastMessage.show();
}
}
// This method will rotate an image to 90 degrees each time rotateImage Button is clicked.
void rotateViewto90(){
Bitmap bmpOriginal = drawableToBitmap(mImageView.getDrawable());
Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(bmResult);
tempCanvas.rotate(90, bmpOriginal.getWidth()/2, bmpOriginal.getHeight()/2);
tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
mImageView.setImageBitmap(bmResult);
}
//convert Drawable to Bitmap
public static Bitmap drawableToBitmap (Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}