문제

            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());
도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top