zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    Mysql的utf8与utf8mb4区别,utf8mb4_bin、utf8mb4_general_ci、utf8mb4_unicode_ci区别
    【.net 深呼吸】细说CodeDom(7):索引器
    【.net 深呼吸】细说CodeDom(6):方法参数
    【.net 深呼吸】细说CodeDom(5):类型成员
    【.net 深呼吸】细说CodeDom(4):类型定义
    【.net 深呼吸】细说CodeDom(3):命名空间
    【.net 深呼吸】细说CodeDom(2):表达式、语句
    【.net 深呼吸】细说CodeDom(1):结构大观
    【.net 深呼吸】序列化中的“引用保留”
    【.net 深呼吸】设置序列化中的最大数据量
  • 原文地址:https://www.cnblogs.com/sklww/p/3659469.html
Copyright © 2011-2022 走看看