为什么使用ImageLoader旋转显示肖像图像?

时间:2014-08-11 18:46:33

标签: android gridview rotation orientation universal-image-loader

我正在尝试使用ImageAdapter和ImageLoader将图库作为GridView显示。 所有肖像图像都旋转+ -90度。 我知道getExifOrientation代码来检查图像方向并将其旋转回去,但我根本不知道在哪里使用它..

这是我使用的ImageAdapter类,在这里得到了大部分内容并尝试正确使用它。

public class ImageAdapter extends BaseAdapter {

    ArrayList<String> _list;
    LayoutInflater _inflater;
    Context _context;

    public ImageAdapter(Context context, ArrayList<String> imageList) {

        _context = context;
        _inflater = LayoutInflater.from(_context);
        _list = new ArrayList<String>();
        this._list = imageList;
    }

    @Override
    public int getCount() {
        return _imageUrls.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null) {
            convertView = _inflater.inflate(R.layout.multiphoto_item, null);
        }

        final ImageView imageView = (ImageView) convertView.findViewById(R.id.image_view);
        imageView.setTag(position);
        imageView.setScaleType(ScaleType.CENTER);

        _imageLoader.displayImage("file://"+_imageUrls.get(position), imageView, _options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(Bitmap loadedImage) {
                Animation anim = AnimationUtils.loadAnimation(ViewCategoryActivity.this, R.anim.fade_in);
                imageView.setAnimation(anim);
                anim.start();
            }


        });

        imageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Integer position = (Integer)imageView.getTag();
                String url = _imageUrls.get(position);
                Intent intent = new Intent("com.eibimalul.smartgallery.SingleImageDisplay");
                intent.putExtra("selectedImagePosition", position);
                intent.putStringArrayListExtra("imageUrls", _imageUrls);
                intent.putExtra("Title", _activityTitle);
                startActivityForResult(intent, 0);
            }
        });

        return convertView;
    }
}

我将它连接到GridView:

_options = new DisplayImageOptions.Builder()
    .showStubImage(R.drawable.stub_image)
    .showImageForEmptyUri(R.drawable.image_for_empty_url)
    .cacheInMemory()
    .cacheOnDisc()
    .build();

    _imageAdapter = new ImageAdapter(this, _imageUrls);

    GridView gridView = (GridView) findViewById(R.id.gridview);
    gridView.setAdapter(_imageAdapter);

任何帮助将不胜感激..

1 个答案:

答案 0 :(得分:2)

不确定,但对我来说这是有效的,但我不认为这是解决这个问题的最佳方法,但它会有所帮助。

mImageLoader.displayImage(photoBean.getPhotoURL(), imageView, options, new ImageRotationListener());
    options = new DisplayImageOptions.Builder()
    .cacheOnDisk(true)
    .cacheInMemory(true)
    .considerExifParams(true)
    .bitmapConfig(Bitmap.Config.RGB_565)
    .showImageForEmptyUri(R.drawable.icon_venue_default)
    .showImageOnLoading(R.drawable.icon_venue_default)
    .showImageOnFail(R.drawable.icon_venue_default)
    .build();
package com.urbanft.utils;

import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.view.View;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.io.File;

/**
 * Created by kiwitech on 30/8/16.
 */
public class ImageRotationListener implements ImageLoadingListener {

    @Override
    public void onLoadingStarted(String imageUri, View view) {
    }

    @Override
    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
    }

    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        try {
            if(loadedImage != null){
                File file = ImageLoader.getInstance().getDiskCache().get(imageUri);
                if(file == null){
                    return;
                }
                int rotation = getCameraPhotoOrientation(file);
                Bitmap bitmap = null;
                if(rotation != 0){
                    bitmap = resizeBitmap(loadedImage, 1000, 1000);
                    if(bitmap != null){
                        if(view instanceof ImageView){
                            ((ImageView) view).setImageBitmap(bitmap);
                        }
                    }
                }
                view.setBackgroundDrawable(null);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onLoadingCancelled(String imageUri, View view) {
    }

    public Bitmap resizeBitmap( Bitmap input, int destWidth, int destHeight )
    {
        int srcWidth = input.getWidth();
        int srcHeight = input.getHeight();
        boolean needsResize = false;
        float p;
        if (srcWidth > destWidth || srcHeight > destHeight) {
            needsResize = true;
            if ( srcWidth > srcHeight && srcWidth > destWidth) {
                p = (float)destWidth / (float)srcWidth;
                destHeight = (int)( srcHeight * p );
            }
            else {
                p = (float)destHeight / (float)srcHeight;
                destWidth = (int)( srcWidth * p );
            }
        }
        else {
            destWidth = srcWidth;
            destHeight = srcHeight;
        }
        if (needsResize) {
            Bitmap output = Bitmap.createScaledBitmap( input, destWidth, destHeight, true );
            return output;
        }
        else {
            return input;
        }
    }

    public int getCameraPhotoOrientation(File imageFile) throws Exception {
        int rotate = 0;
        try {
            if(imageFile.exists()){
                ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
                int orientation = Integer.parseInt(exif.getAttribute(ExifInterface.TAG_ORIENTATION));
                switch (orientation) {
                    case ExifInterface.ORIENTATION_NORMAL:
                        rotate = 0;
                        break;

                    case ExifInterface.ORIENTATION_ROTATE_270:
                        rotate = 270;
                        break;

                    case ExifInterface.ORIENTATION_ROTATE_180:
                        rotate = 180;
                        break;

                    case ExifInterface.ORIENTATION_ROTATE_90:
                        rotate = 90;
                        break;
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return rotate;
    }
}