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));
我已經使用上面的代碼在ViewFlipper
中旋轉圖像。
第一次執行,但第二次就返回NullPointerException
...
錯誤行:第二次在Imageview.getDrawingCache中的空指針異常
Bitmap bitmap =Bitmap.createBitmap(img.getDrawingCache());
你如何實例化'img'? – Szymon
位圖位圖= Bitmap.createBitmap(img.getDrawingCache()); –
你的ImageView是動態還是可以在你的xml文件中? – Piyush