0
点赞
收藏
分享

微信扫一扫

Pro Android学习笔记(十六):用户界面和控制(4):ImageView控件


ImageView是基础的控件,它是android.widget.ImageView的继承类。

XML片段

Pro Android学习笔记(十六):用户界面和控制(4):ImageView控件_Android

<LinearLayout android:layout_width="wrap_content"
         android:layout_height="wrap_content" 
         android:orientatinotallow="horizontal"> 
       <!--  指定资源id: @drawable/xxxxx  -->
         <ImageView android:layout_width="wrap_content"
             android:layout_height="wrap_content" 
             android:id="@+id/ui_image1" 
             android:src="@drawable/ic_launcher" />  

         <!-- 显示色块 --> 
         <ImageView android:layout_width="125dip" 
             android:layout_height="25dip" 
             android:id="@+id/ui_image2" 
            android:src="#555555" 
             android:cnotallow="set pure color"/> 
   </LinearLayout> 
    
    <LinearLayout android:layout_width="wrap_content"
         android:layout_height="wrap_content" 
         android:orientatinotallow="horizontal">  
        <!-- 这三个图来源于同一个128×128的png图标,前两者指定长、宽时,皆比原图要小,系统采用等比缩小的方式适配指定size -->
         <ImageView android:layout_width="25dip" 
             android:layout_height="25dip" 
             android:src="@drawable/png0441"/> 
         <ImageView android:layout_width="48dip" 
             android:layout_height="48dip" 
             android:src="@drawable/png0441"/> 
         <ImageView android:layout_width="wrap_content" 
             android:layout_height="wrap_content" 
              android:src="@drawable/png0441" /> 
    </LinearLayout>


此外我还是试验了两layout_width和layout_height设置为wrap_content,而另外设置了maxWidth和maxHeight,但是发现maxWidth/Height并不起作用,仍是原图大小呈现,这点和Pro Android 4.0书中所言不同,关于此功能,慎用

<ImageView android:layout_width="60dip" 
         android:layout_height="30dip" 
         android:src="@drawable/png0441" 
         android:scaleType="fitXY"  />  
     <!-- 这里我们没有设置android:src,但是给了一个id号,用于等会在代码进行设置 --> 
     <ImageView android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:id="@+id/ui_image3"  />

代码设置ImageView

在XML中ui_image3没有设置具体的src,可以在代码中通过若干中方法设置。

方式1:设置资源的ID

ImageView image = (ImageView)findViewById(R.id.ui_image3); 
image.setImageResource(R.drawable.ic_launcher);

方式2:通过Bitmap

ImageView image = (ImageView)findViewById(R.id.ui_image3);
Bitmap bm = BitmapFactory.decodeResource(this.getResources(), R.drawable.png02);
//在这里可以加入对Bitmap的处理代码 ... ...
image.setImageBitmap(bm);

方式3:通过文件

对于模拟器,我们通过命令行以adb push的方式将图片文件放入文件系统的某个位置,例如sdcard中,如下图所示:

Pro Android学习笔记(十六):用户界面和控制(4):ImageView控件_本地存储_02

ImageView image = (ImageView)findViewById(R.id.ui_image3);
try{
    String filename = Environment.getExternalStorageDirectory()+ "/wei/sunflower.jpg"; 
   image.setImageDrawable(Drawable.createFromPath(filename)); }catch(Exception e){
    Log.e("wei",e.toString());
}

方式4:通过Uri方式

ImageView image = (ImageView)findViewById(R.id.ui_image3);
image.setImageURI(Uri.parse("file://mnt/sdcard/wei/logo.jpg")); //只能是本地存储

注意URI方式只限于本地存储,不能是远端存储,如果我们设置了web URI,系统会报以下错误:

Pro Android学习笔记(十六):用户界面和控制(4):ImageView控件_android_03

其他

如果我们希望图片来自remote,可以利用BitmapFactory.decodeStream(InputStream is),然后将Bitmap放入ImageView中。


举报

相关推荐

0 条评论