android学习之展示图片资源

在大家所使用的android应用中除了文本信息外还有图片信息,而在咱们的android开发过程中为用户展示图片信息,我们通常会使用ImageView,将单词拆开来翻译一下,就很明白图片视图,顾名思义,ImageView是用来显示图片内容的。

在布局当中我们如果想要使用这个控件首先就要将它写到我们的布局容器当中:

布局文件代码如下:

<?xml version="1.0"encoding="utf-8"?>

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

   android:layout_width="match_parent"

   android:layout_height="match_parent" >

   <ImageView

       android:id="@+id/image"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:src="@drawable/ic_launcher"

       />

 

</RelativeLayout>

当前代码显示效果为:

而如果我们为ImageView指定了死的宽高为match_parent时ui界面如图:

 

而其实我们还可以进一步的对图片的展示进行调整:

通过修改android:scaleType的属性值开控制控件的显示:如果将ImageView的代码改为:

 

   <ImageView

       android:id="@+id/image"

       android:layout_width="match_parent"

       android:scaleType="centerInside"

       android:layout_height="match_parent"

       android:src="@drawable/ic_launcher" />

那么,当前的显示效果为:

android:scaleType的值共8种,但不需要记忆,当你需要对图片的显示效果进行调整时你可以将这几个值换着来,看调试的效果。

 

接下来是通过代码设置指定的图片信息展示:

首先你可以将一张你想要展示的图片拷贝到Drawable目录里,但要注意它的命名:

不要有大写字母,不要有空格,不要以数字开头,不要是纯数字,不要有特殊符号,确认图片名称符合标准时,我们将图片拷贝到Drawable目录,

然后在我们的java源文件中代码:

 

import android.os.Bundle;

import android.app.Activity;

import android.widget.ImageView;

import android.widget.TextView;

public class MainActivity extends Activity{

         ImageViewimage;

         @Override

         protectedvoid onCreate(Bundle savedInstanceState) {

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.image);

                   //找到ImageView

                   image=(ImageView)findViewById(R.id.image);

                   //设置展示指定的图片信息

                   image.setImageResource(R.drawable.login);

         }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值