ImageView的使用方法

1. 图片视图(ImageView)的基本概念

2. <IamgeView/> 与 ImageView

3. 神奇的ScaleType属性

 

1. 图片视图(ImageView)的基本概念

    

2. <IamgeView/> 与 ImageView

     凡是放在drawable下的图片都会在R.java里面生成 Id

    

     因此, 在目录drawable中  复制粘贴  加入图片leaf.png

          

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="first.pack.MainActivity$PlaceholderFragment" >
10 
11     <ImageView
12         android:id="@+id/imageViewId"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:src="@drawable/leaf"/>   //布局文件中添加图片路径
16 
17 </LinearLayout>

       也可在代码中添加

 1 public static class PlaceholderFragment extends Fragment {
 2 
 3         ImageView imageView;  //生成对象
 4         
 5         public PlaceholderFragment() {
 6         }
 7 
 8         @Override
 9         public View onCreateView(LayoutInflater inflater, ViewGroup container,
10                 Bundle savedInstanceState) {
11             View rootView = inflater.inflate(R.layout.fragment_main, container, false);
12             
13             imageView = (ImageView)rootView.findViewById(R.id.imageViewId);
14             
15             imageView.setImageResource(R.drawable.leaf);  //调用方法,参数是Id
16             
17             return rootView;
18         }
19     }

      

3. 神奇的ScaleType属性

     

      fit 就是适应当前设置的ImageView的大小, center表示中心

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="first.pack.MainActivity$PlaceholderFragment" >
10 
11     <ImageView
12         android:id="@+id/imageViewId"
13         android:layout_width="100dp"    //规定是100dp
14         android:layout_height="100dp"
15         android:scaleType="fitCenter"/>    //适应当前的大小, 图片将会自动缩放
16 
17 </LinearLayout>

       显示如下

     

 

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="first.pack.MainActivity$PlaceholderFragment" >
10 
11     <ImageView
12         android:id="@+id/imageViewId"
13         android:layout_width="100dp"
14         android:layout_height="100dp"
15         android:scaleType="center"/>
16 
17 </LinearLayout>

          原图不会缩放, 但是会将图片中心显示在 ImageView 中

          

 1  public static class PlaceholderFragment extends Fragment {
 2 
 3         ImageView imageView;  
 4         
 5         public PlaceholderFragment() {
 6         }
 7 
 8         @Override
 9         public View onCreateView(LayoutInflater inflater, ViewGroup container,
10                 Bundle savedInstanceState) {
11             View rootView = inflater.inflate(R.layout.fragment_main, container, false);
12             
13             imageView = (ImageView)rootView.findViewById(R.id.imageViewId);
14             
15             imageView.setImageResource(R.drawable.leaf);
16             imageView.setScaleType(ScaleType.CENTER);  //同样也可以在代码中进行设置!!!
17             
18             return rootView;
19         }
20     }

 

 

       

posted @ 2014-06-09 10:59  Mirrorhanman  阅读(971)  评论(0编辑  收藏  举报