zoukankan      html  css  js  c++  java
  • 【转】Pro Android学习笔记(十六):用户界面和控制(4):ImageView控件

    目录(?)[-]

    1. XML片段
    2. 代码设置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开发相关文章

  • 相关阅读:
    实验一、DOS使用命令实验
    实验三、进程调度模拟程序
    实验四、存储管理
    实验二、作业调度模拟程序
    简单的DOS命令
    结构化方法和面向对象方法的比较
    jstree 取消选中父节点
    T4 模板代码生成
    基于Open XML 导出数据到Excel
    菜单(列存储转为行存储)
  • 原文地址:https://www.cnblogs.com/blongfree/p/5047892.html
Copyright © 2011-2022 走看看