Reputation: 1207
View vvv=mViewFlipper.getCurrentView();
RelativeLayout rrr=(RelativeLayout)vvv;
ImageView img=(ImageView) rrr.getChildAt(0);
img.setRotation(90);
img.setDrawingCacheEnabled(true);
img.buildDrawingCache();
Bitmap bitmap =img.getDrawingCache();
img.destroyDrawingCache();
img.setDrawingCacheEnabled(false);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(rotatedBitmap);
rrr.removeViewAt(0);
ImageView img_new=new ImageView(ImageSlideShow.this);
img_new.setImageDrawable(bmd);
img_new.setScaleType(ScaleType.CENTER);
rrr.addView(img_new, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
I have Used Above code for Image rotating in ViewFlipper
.
First time it is executed but second time it return NullPointerException
...
Error line:
Bitmap bitmap =Bitmap.createBitmap(img.getDrawingCache());
Upvotes: 1
Views: 1206
Reputation: 1207
I got Solution For this problem
ImageView img=(ImageView) rrr.getChildAt(0);
Matrix matrix = new Matrix();
matrix.set(img.getImageMatrix());
matrix.postRotate(90, img.getWidth() / 2, img.getHeight() / 2);
img.setImageMatrix(matrix);
above code complete working for image rotation and you must set android:scaleType="matrix" in your imageview.
Upvotes: 0
Reputation: 3122
hi use this code and hopefully this works
basically your image view is null so first inintiallise it
ImageView img= new ImageView();
img=(ImageView) rrr.getChildAt(0);
img.setRotation(90);
img.setDrawingCacheEnabled(true);
img.buildDrawingCache();
if(img.isDrawingCacheEnabled()){
Bitmap bitmap =img.getDrawingCache(true);
}img.destroyDrawingCache();
img.setDrawingCacheEnabled(false);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100,
bao);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(rotatedBitmap);
rrr.removeViewAt(0);
ImageView img_new=new ImageView(ImageSlideShow.this);
img_new.setImageDrawable(bmd);
img_new.setScaleType(ScaleType.CENTER);
rrr.addView(img_new, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
Upvotes: 0