android imageview布局问题

来源:互联网 发布:汉罗塔c语言 编辑:程序博客网 时间:2024/06/04 20:12

第一种情况:图片属性设置为红色字体部分的图如下

imageview宽高属性设置为fill_parent的图

布局文件如下

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" 

    android:id="@+id/line"

    >

 

    <ImageView

        android:id="@+id/imageView"

      android:layout_width="fill_parent"

       android:layout_height="fill_parent"

        />

  <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="下一个" />

    </LinearLayout>

第二种情况,布局界面图如下

imageview属性为wrap_content的效果图

三张分别为同一布局文件,但图片位置大小不一样

布局文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" 

    android:id="@+id/line"

    >

 

    <ImageView

        android:id="@+id/imageView"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

       

       

        />

 

 

    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="下一个" />

    

</LinearLayout>

第三中固定高度的布局效果图

布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:id="@+id/line"
    >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="400dp"
       
       
        />


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下一个" />
    
</LinearLayout>

主代码程序
001package com.bitmap;
002 
003 
004import android.app.Activity;
005import android.graphics.Bitmap;
006import android.graphics.BitmapFactory;
007import android.graphics.Color;
008import android.graphics.drawable.BitmapDrawable;
009import android.os.Bundle;
010import android.view.View;
011import android.view.View.OnClickListener;
012import android.widget.Button;
013import android.widget.ImageView;
014import android.widget.LinearLayout;
015 
016public classBitmapTest extends Activity {
017    privateLinearLayout  line;
018    privateImageView  imageView;
019    privateButton button1;
020    Bitmap  bmp1,bmp2,bmp3,bmp4,bmp5;
021    privateint i=0;
022    @Override
023    publicvoid onCreate(Bundle savedInstanceState) {
024        super.onCreate(savedInstanceState);
025        setContentView(R.layout.main);
026        button1=(Button)findViewById(R.id.button1);
027        line=(LinearLayout)findViewById(R.id.line);
028        imageView=(ImageView)findViewById(R.id.imageView);
029        BitmapFactory.Options opts =new BitmapFactory.Options();
030        opts.inJustDecodeBounds =true;
031        BitmapFactory.decodeFile("/sdcard/picturetest/a1.jpg", opts);
032 
033        opts.inSampleSize = computeSampleSize(opts, -1,480*800);
034        opts.inJustDecodeBounds =false;
035        try{
036             bmp1 = BitmapFactory.decodeFile("/sdcard/picturetest/a1.jpg", opts);
037             bmp2 = BitmapFactory.decodeFile("/sdcard/picturetest/a2.jpg", opts);
038             bmp3 = BitmapFactory.decodeFile("/sdcard/picturetest/a3.jpg", opts);
039             bmp4 = BitmapFactory.decodeFile("/sdcard/picturetest/a4.jpg", opts);
040             bmp5 = BitmapFactory.decodeFile("/sdcard/picturetest/a5.jpg", opts);
041            imageView.setImageBitmap(bmp1);
042            }catch (OutOfMemoryError err) {
043        }
044        
045         
046            button1.setOnClickListener(newOnClickListener(){
047 
048            @Override
049            publicvoid onClick(View arg0) {
050                // TODO Auto-generated method stub
051                if(i==0){
052                imageView.setImageBitmap(bmp2);
053                if(!bmp1.isRecycled() ){
054                    bmp1.recycle() ; //回收图片所占的内存
055                    java.lang.System.gc(); //提醒系统及时回收
056                    }
057                }
058                 
059                if(i==1){
060                    imageView.setImageBitmap(bmp3);
061                     
062                }
063                if(i==2){
064                    imageView.setImageBitmap(bmp4);
065                    if(!bmp3.isRecycled() ){
066                        bmp3.recycle() ; //回收图片所占的内存
067                        java.lang.System.gc(); //提醒系统及时回收
068                        }  
069                }
070                if(i==3){
071                    imageView.setImageBitmap(bmp5);
072                    if(!bmp4.isRecycled() ){
073                        bmp4.recycle() ; //回收图片所占的内存
074                      //  java.lang.System.gc();  //提醒系统及时回收
075                        }  
076                }
077                i++;
078                 
079            }
080             
081             
082             
083             
084        });
085 
086        
087         
088         
089    }
090     
091    
092 
093    publicstatic int computeSampleSize(BitmapFactory.Options options,
094            intminSideLength, int maxNumOfPixels) {
095        intinitialSize = computeInitialSampleSize(options, minSideLength,maxNumOfPixels);
096 
097        introundedSize;
098        if(initialSize <= 8 ) {
099            roundedSize =1;
100            while(roundedSize < initialSize) {
101                roundedSize <<=1;
102            }
103        } else {
104            roundedSize = (initialSize +7) / 8* 8;
105        }
106 
107        returnroundedSize;
108    }
109 
110    privatestatic int computeInitialSampleSize(BitmapFactory.Options options,intminSideLength, int maxNumOfPixels) {
111        doublew = options.outWidth;
112        doubleh = options.outHeight;
113 
114        intlowerBound = (maxNumOfPixels == -1) ?1 :
115                (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
116        intupperBound = (minSideLength == -1) ?128 :
117                (int) Math.min(Math.floor(w / minSideLength),
118                Math.floor(h / minSideLength));
119        if(upperBound < lowerBound) {
120            returnlowerBound;
121        }
122 
123        if((maxNumOfPixels == -1) &&
124                (minSideLength == -1)) {
125            return1;
126        } else if (minSideLength == -1) {
127            returnlowerBound;
128        } else {
129            returnupperBound;
130        }
131         
132          
133    
134     
135     
136}

问题一:第一种情况图片为什么没有填充整个屏幕?
问题二:如何才能使第二种情况的图片大小一致呢?
问题三:第三种情况的图片上边缘为什么没有紧挨屏幕的上边缘?
恳求大家帮忙解答疑惑,谢谢!
0 0