Android API之ImageView.ScaleType代码演示

本文通过一个GridView演示了ImageView.ScaleType的八种类型:CENTER、CENTER_CROP、CENTER_INSIDE、FIT_CENTER、FIT_START、FIT_END、FIT_XY、MATRIX。通过点击不同类型的ImageButton可以在单独的Activity中查看每种类型的效果。
摘要由CSDN通过智能技术生成

为了全面演示ImageView.ScaleType的八种类型即CENTERCENTER_CROPCENTER_INSIDEFIT_CENTERFIT_STARTFIT_ENDFIT_XYMATRIX,我在这里通过一个GridView进行显示,可通过点击每一种类型的ImageButton进行详细查看、比较。

背景图片为一个像素宽度(443px)×高度(500px),大于默认的Android模拟器320×480,这样可以清晰的看出ImageView是否做了比例缩放,及所处位置的差异。

 

(1)      AndroidManifest.xml添加各种ScaleTypeActivity

<activity android:name=".ImageViewScaleTypeDemo1"/>

     <activity android:name=".ImageViewScaleTypeDemo2"/>

     <activity android:name=".ImageViewScaleTypeDemo3"/>

     <activity android:name=".ImageViewScaleTypeDemo4"/>

     <activity android:name=".ImageViewScaleTypeDemo5"/>

     <activity android:name=".ImageViewScaleTypeDemo6"/>

     <activity android:name=".ImageViewScaleTypeDemo7"/>

     <activity android:name=".ImageViewScaleTypeDemo8"/>

(2)     res/layout/main.xmlGridView主视图布局

  <GridView

   android:id="@+id/gridView"

   android:layout_width="fill_parent"

   android:layout_height="fill_parent"

   android:numColumns="auto_fit"

   android:verticalSpacing="10dp"

   android:horizontalSpacing="10dp"

   android:columnWidth="90dp"

   android:stretchMode="columnWidth"

   android:gravity="center"

   />

(3)     res/layout/image.xml  ScaleType视图布局

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

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

   android:orientation="vertical"

   android:layout_width="fill_parent"

   android:layout_height="fill_parent"

   >

   

   <ImageView

    android:id="@+id/imageView1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

   />

</LinearLayout>

(4)     ImageAdapter.java 继承BaseAdapter,并载入GridView的图片资源

public class ImageAdapter extends BaseAdapter

{

   // 定义Context

   private Context     mContext;

   // 定义整型数组即图片资源

   private Integer[] mImageIds  =

   {

          R.drawable.center,

          R.drawable.centercrop,

          R.drawable.centerinside,

          R.drawable.fitcenter,

          R.drawable.fitstart,

          R.drawable.fitend,

          R.drawable.fitxy,

         R.drawable.matrix,

   };

 

   public ImageAdapter(Context c)

   {

      mContext = c;

   }

 

   // 获取图片的个数

   public int getCount()

   {

      return mImageIds.length;

   }

 

   // 获取图片在库中的位置

   public Object getItem(int position)

   {

      return position;

   }

 

 

   // 获取图片ID

   public long getItemId(int position)

   {

      return position;

   }

 

 

   public View getView(int position, View convertView, ViewGroupparent)

   {

      ImageView imageView;

      if(convertView ==null)

      {

          // ImageView设置资源

          imageView = new ImageView(mContext);

          // 设置布局图片显示

          imageView.setLayoutParams(new GridView.LayoutParams(85,85));

          // 设置显示比例类型

          imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

      }

      else

      {

          imageView = (ImageView) convertView;

      }

 

      imageView.setImageResource(mImageIds[position]);

      return imageView;

   }

 

}

(5)     MainActivity.java  GridView主视图类

public class MainActivity extends Activity {

   private GridView gridView;

   

   @Override

   public void onCreate(Bundle savedInstanceState){

       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

       

     gridView=(GridView)findViewById(R.id.gridView);

     gridView.setAdapter(newImageAdapter(this));

     gridView.setBackgroundResource(R.drawable.image);

     

     gridView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

 

      @Override

      public void onItemClick(AdapterView<?>arg0, View arg1, int arg2,

             long arg3) {

          // TODO Auto-generated method stub

          Intent intent=new Intent();

          switch(arg2){

 //GridView第一个ID,对应CENTER

          case 0:

             intent.setClass(MainActivity.this,ImageViewScaleTypeDemo1.class);

             startActivity(intent);

             MainActivity.this.finish();

             break;

   

//GridView第二个ID,对应CENTER_CROP 

          case 1:

             intent.setClass(MainActivity.this,ImageViewScaleTypeDemo2.class);

             startActivity(intent);

             MainActivity.this.finish();

             break;

             

          case 2:

             intent.setClass(MainActivity.this,ImageViewScaleTypeDemo3.class);

             startActivity(intent);

             MainActivity.this.finish();

             break;

             

          case 3:

             intent.setClass(MainActivity.this,ImageViewScaleTypeDemo4.class);

             startActivity(intent);

             MainActivity.this.finish();

             break;

             

          case 4:

             intent.setClass(MainActivity.this,ImageViewScaleTypeDemo5.class);

             startActivity(intent);

             MainActivity.this.finish();

             break;

             

          case 5:

             intent.setClass(MainActivity.this,ImageViewScaleTypeDemo6.class);

             startActivity(intent);

             MainActivity.this.finish();

             break;

             

          case 6:

             intent.setClass(MainActivity.this,ImageViewScaleTypeDemo7.class);

             startActivity(intent);

             MainActivity.this.finish();

             break;

             

          case 7:

             intent.setClass(MainActivity.this,ImageViewScaleTypeDemo8.class);

             startActivity(intent);

             MainActivity.this.finish();

             break;

             

          }

      }

   });

   }

}    

(6)     ImageViewScaleTypeDemo1.java显示imageView1显示类型为CENTER,其它ImageViewScaleTypeDemo2-8分别为CENTER_CROPCENTER_INSIDEFIT_CENTERFIT_STARTFIT_ENDFIT_XYMATRIX,此处略去代码

 

public class ImageViewScaleTypeDemo1extends Activity {

   private ImageView imageView1;

   

   @Override

   public void onCreate(Bundle savedInstanceState){

       super.onCreate(savedInstanceState);

       setContentView(R.layout.image);

       

       //显示imageView1

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

       

       //设置imageView1背景资源

       imageView1.setImageResource(R.drawable.image);

       

       //设置imageView1显示类型为CENTER

       imageView1.setScaleType(ImageView.ScaleType.CENTER);

          

   }

   

   @Override

   public boolean onKeyDown(int keyCode, KeyEvent event) {

      // TODO Auto-generated method stub

      

      //响应返回按键,退出到主界面

      if(keyCode==KeyEvent.KEYCODE_BACK){

          Intent intent=new Intent();

          intent.setClass(ImageViewScaleTypeDemo1.this, MainActivity.class);

          startActivity(intent);

          ImageViewScaleTypeDemo1.this.finish();

      }

      return super.onKeyDown(keyCode,event);

   }

}

(7) 效果截图:

   主界面:

[转载]Android <wbr>API之ImageView.ScaleType代码演示

 CENTER效果:

 [转载]Android <wbr>API之ImageView.ScaleType代码演示

 

CENTER_CROP效果:

 [转载]Android <wbr>API之ImageView.ScaleType代码演示

 

CENTER_INSIDE效果:

 [转载]Android <wbr>API之ImageView.ScaleType代码演示

 

FIT_CENTER效果:

 [转载]Android <wbr>API之ImageView.ScaleType代码演示

 
FIT_START效果:

 [转载]Android <wbr>API之ImageView.ScaleType代码演示

 
FIT_END效果:

 [转载]Android <wbr>API之ImageView.ScaleType代码演示


FIT_XY效果:

 [转载]Android <wbr>API之ImageView.ScaleType代码演示

 

MATRIX效果:

 

 [转载]Android <wbr>API之ImageView.ScaleType代码演示

(8)结果分析:

我选取的原始图片明显标志如下:左上角有蝴蝶、花朵,右下角为一个蝗虫,且图片像素大于320×480,各种类型的差异通过查看图片的明显标志即可窥见一斑。比如FIT_START,填充于屏幕上方,并等比例缩放;相反,FIT_END置于屏幕下方,而FIT_CENTER则在中间。CENTER_CROP裁剪了图片的中间部分填充屏幕。其它类型可自行查看分析。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值