码迷,mamicode.com
首页 > 移动开发 > 详细

Android API之ImageView.ScaleType代码演示

时间:2015-11-03 22:33:56      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:

摘抄自http://blog.sina.com.cn/s/blog_407abb0d0100mao1.html

为了全面演示ImageView.ScaleType的八种类型即CENTER,CENTER_CROP,CENTER_INSIDE,FIT_CENTER,FIT_START,FIT_END,FIT_XY,MATRIX,我在这里通过一个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.xml GridView主视图布局

  <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, ViewGroup parent)

    {

       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_INSIDE,FIT_CENTER,FIT_START,FIT_END,FIT_XY,MATRIX,此处略去代码

 

public class ImageViewScaleTypeDemo1 extends 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) 效果截图:

   主界面:

技术分享

 CENTER效果:

 技术分享

 

CENTER_CROP效果:

 技术分享

 

CENTER_INSIDE效果:

 技术分享

 

FIT_CENTER效果:

 技术分享

 
FIT_START效果:

 技术分享

 
FIT_END效果:

 技术分享


FIT_XY效果:

 技术分享

 

MATRIX效果:

 

 技术分享

(8) 结果分析:

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

Android API之ImageView.ScaleType代码演示

标签:

原文地址:http://www.cnblogs.com/xidada/p/4934332.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!