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...
错误行:

Bitmap bitmap =Bitmap.createBitmap(img.getDrawingCache());
4

2 回答 2

0

嗨使用此代码,希望这有效

基本上你的图像视图是空的,所以首先初始化它

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));
于 2014-04-23T05:17:49.883 回答
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"。

于 2014-04-24T04:22:50.153 回答