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>

 

android 显示大图片,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。