2014-04-23 38 views
1
  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()); 
+0

你如何實例化'img'? – Szymon

+0

位圖位圖= Bitmap.createBitmap(img.getDrawingCache()); –

+1

你的ImageView是動態還是可以在你的xml文件中? – Piyush

回答

0

喜利用這個代碼,並希望這部作品

基本上你的形象的看法是空,因此第一inintiallise它

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)); 
+0

你的代碼給... 04-23 12:12:07.973:E/AndroidRuntime(11038 ):java.lang.IllegalStateException:無法壓縮回收的位圖 –

+0

確定如此刪除壓縮代碼並使用它 –

+0

它與上面相同...第一次工作,第二次它在位圖中返回NULL bitmap = img.getDrawingCache() ; –

0

我得到的解決方案對於這個問題

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); 

上面的代碼完成圖像旋轉的工作,你必須在你的imageview中設置android:scaleType =「matrix」。