在全屏模式下查看时,高分辨率图像无法在图像视图中显示?

时间:2013-12-13 08:02:58

标签: android imageview fullscreen mode high-resolution

我在三星tab2中以全屏模式显示图像,但每当要显示大图像时,图像视图中都不会显示任何内容。

image_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background_repet_"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imgFullImage"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_margin="15dp"
      />

</LinearLayout>

FullImageActivity.java: -

    ImageView imgFullImage;

    String dispFilename;
    String fileUrl;
    Bitmap bitmap;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.image_view);

        imgFullImage = (ImageView)findViewById(R.id.imgFullImage);

        fileUrl = getIntent().getExtras().getString("fileurl");
        dispFilename=getIntent().getExtras().getString("dispFilename");

            imgFullImage.setScaleType(ScaleType.FIT_CENTER);

                bitmap=decodeSampledBitmapFromResource(fileUrl,
        getDeviceWidth(FullImageActivity.this),getDeviceHeight(FullImageActivity.this));

               Log.e("Fill Image Size in 
                 Bytes","====>"+bitmap.getByteCount());     
        Log.e("Bitmap width & height","===>"+bitmap.getWidth() +"x"+bitmap.getHeight());
                imgFullImage.setImageBitmap(bitmap);


    }

  public static int getDeviceWidth(Activity activity) {
        int deviceWidth = 0;

        Point size = new Point();
        WindowManager windowManager = activity.getWindowManager();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            windowManager.getDefaultDisplay().getSize(size);
            deviceWidth = size.x;
        } else {
            Display display = windowManager.getDefaultDisplay();
            deviceWidth = display.getWidth();
        }
        return deviceWidth;
    }

@SuppressLint("NewApi")
public static int getDeviceHeight(Activity activity) {
    int deviceHeight = 0;

    Point size = new Point();
    WindowManager windowManager = activity.getWindowManager();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        windowManager.getDefaultDisplay().getSize(size);
        deviceHeight = size.y;
    } else {
        Display display = windowManager.getDefaultDisplay();
        deviceHeight = display.getHeight();
    }
    return deviceHeight;
}

public static Bitmap decodeSampledBitmapFromResource(String pathName,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(pathName, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(pathName, options);
}

public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and
        // keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

日志: -

12-13 13:31:45.238: E/Fill Image Size in Bytes(24036): ====>15668224
12-13 13:31:45.238: E/Bitmap width & height(24036): ===>2288x1712

如果有任何想法请回复。

提前致谢...

4 个答案:

答案 0 :(得分:2)

public static Bitmap decodeSampledBitmapFromResource(String pathName,
            int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(pathName, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(pathName, options);
    }

    public static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and
            // keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

答案 1 :(得分:0)

更改为:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/background_repet_" >

<ImageView android:id="@+id/imgFullImage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_margin="15dp"
    android:scaleType="fitCenter"/>

</LinearLayout>

答案 2 :(得分:0)

@SuppressLint("NewApi")
    public static int getDeviceWidth(Activity activity) {
        int deviceWidth = 0;

        Point size = new Point();
        WindowManager windowManager = activity.getWindowManager();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            windowManager.getDefaultDisplay().getSize(size);
            deviceWidth = size.x;
        } else {
            Display display = windowManager.getDefaultDisplay();
            deviceWidth = display.getWidth();
        }
        return deviceWidth;
    }

    @SuppressLint("NewApi")
    public static int getDeviceHeight(Activity activity) {
        int deviceHeight = 0;

        Point size = new Point();
        WindowManager windowManager = activity.getWindowManager();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            windowManager.getDefaultDisplay().getSize(size);
            deviceHeight = size.y;
        } else {
            Display display = windowManager.getDefaultDisplay();
            deviceHeight = display.getHeight();
        }
        return deviceHeight;
    }

您可以使用设备高度和宽度..并在imageview中设置此图片..

ImageView image = new ImageView(viewHomeScreen);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.FILL_PARENT,
                FrameLayout.LayoutParams.FILL_PARENT);

        params.height = getDeviceHeight(activity);
        params.width = getDevicewidth(activity);
FrameLayout framelayout = new FrameLayout(viewHomeScreen);

        framelayout.addView(image, 0, params);

我希望这对你有用

答案 3 :(得分:0)

public static void setImagesNew(ImageView img, String pathName,
            Activity activity) {

        Bitmap bmp = decodeSampledBitmapFromResource(pathName,
                getDeviceWidth(activity), getDeviceHeight(activity));

        img.setImageBitmap(bmp);

        bmp = null;
        System.gc();
        Runtime.getRuntime().gc();

    }

使用此功能,imagesize将与您的设备高度宽度兼容.. 你的问题与图像有关吗? 所以这是最好的解决方案......