android 显示大图片

//MianActivity

package com.example.showsmallandbigpic;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Display;
import android.widget.ImageView;

public class picActivity extends Activity {
    private ImageView ivTest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pic);
        ivTest=(ImageView)findViewById(R.id.iv);
        Intent intent=getIntent();
        String s=intent.getStringExtra("type");
        if(s.equals("small")){
            Bitmap bm=BitmapFactory.decodeResource(getResources(), R.drawable.small);
            ivTest.setImageBitmap(bm);
        }
        else{
            Display currenDisplay=getWindowManager().getDefaultDisplay();
            int dw=currenDisplay.getWidth();
            int dh=currenDisplay.getHeight();
            BitmapFactory.Options bmpFactoryOptions=new BitmapFactory.Options();
            bmpFactoryOptions.inJustDecodeBounds=true;        
            Bitmap bmpBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.big,bmpFactoryOptions);
            int heightRatio=(int)Math.ceil(bmpFactoryOptions.outHeight/(float)dh);
            int widthRatio=(int)Math.ceil(bmpFactoryOptions.outWidth/(float)dw);
            System.out.println("heightRation="+heightRatio);
            System.out.println("widthRatio="+widthRatio);            
            if(heightRatio>1&&widthRatio>1){
                if (heightRatio>widthRatio)
                    bmpFactoryOptions.inSampleSize=heightRatio;
            
            else {
                bmpFactoryOptions.inSampleSize=widthRatio;
            }
            }
            bmpFactoryOptions.inJustDecodeBounds=false;
            Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.big,bmpFactoryOptions);
            ivTest.setImageBitmap(bmp);
            
        }
    }
    

}

//picActivity

package com.example.showsmallandbigpic;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Display;
import android.widget.ImageView;

public class picActivity extends Activity {
    private ImageView ivTest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pic);
        ivTest=(ImageView)findViewById(R.id.iv);
        Intent intent=getIntent();
        String s=intent.getStringExtra("type");
        if(s.equals("small")){
            Bitmap bm=BitmapFactory.decodeResource(getResources(), R.drawable.small);
            ivTest.setImageBitmap(bm);
        }
        else{
            Display currenDisplay=getWindowManager().getDefaultDisplay();
            int dw=currenDisplay.getWidth();
            int dh=currenDisplay.getHeight();
            BitmapFactory.Options bmpFactoryOptions=new BitmapFactory.Options();
            bmpFactoryOptions.inJustDecodeBounds=true;        
            Bitmap bmpBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.big,bmpFactoryOptions);
            int heightRatio=(int)Math.ceil(bmpFactoryOptions.outHeight/(float)dh);
            int widthRatio=(int)Math.ceil(bmpFactoryOptions.outWidth/(float)dw);
            System.out.println("heightRation="+heightRatio);
            System.out.println("widthRatio="+widthRatio);            
            if(heightRatio>1&&widthRatio>1){
                if (heightRatio>widthRatio)
                    bmpFactoryOptions.inSampleSize=heightRatio;
            
            else {
                bmpFactoryOptions.inSampleSize=widthRatio;
            }
            }
            bmpFactoryOptions.inJustDecodeBounds=false;
            Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.big,bmpFactoryOptions);
            ivTest.setImageBitmap(bmp);
            
        }
    }
    

}

//activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="107dp"
        android:layout_marginTop="103dp"
        android:text="small" />

    <Button
        android:id="@+id/big"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/small"
        android:layout_centerVertical="true"
        android:text="big" />

</RelativeLayout>

//pic.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />
</RelativeLayout>
原文地址:https://www.cnblogs.com/sklww/p/3659469.html