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

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

XML片段

     <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:orientation="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:contentDescription="set pure color"/>
   </LinearLayout>
  
    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="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>
    <!-- 对下面的图,我们设定图片缩小的方式,fitXY,填满整个size。此外我还是试验了两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中,如下图所示:

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,系统会报以下错误:

其他

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

相关链接: 我的Android开发相关文章

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值