android的控件用来显示图片,【Android 开发】:UI控件之显示图片控件 ImageView 的使用方法...

实战案例一:

ImageView控件的基本用法

程序布局文件:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="scaleType: center 为缩放,放在ImageView的中心" />

android:id="@+id/imgaeview"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#F00"

android:scaleType="center"

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

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="scaleType: fitCenter 按照比例来缩放" />

android:id="@+id/imageview2"

android:layout_width="300dp"

android:layout_height="200dp"

android:background="#FFF"

android:padding="10dp"

android:scaleType="fitCenter"

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

[注意 1]:

android:layout_width="wrap_content"

android:layout_height="wrap_content"

这种图片的高度和宽度使用内容包裹属性,则它的宽度和高度是由图片本身的宽度和高度来决定的

android:layout_width="300dp"

android:layout_height="200dp"

第二种 采用固定比例,则图片在ImageView的显示过程中是按这个比例来显示的了

[注意 2]:

android:scaleType属性指定ImageView控件显示图片的方式

center:表示图像以不缩放的方式显示在ImageView控件的中心,

fitCenter:表示图像按照比例缩放至合适的位置,并在ImageView控件的中心

程序主要代码:

package com.android.imageviewbase;

import android.app.Activity;

import android.os.Bundle;

import android.widget.ImageView;

import android.widget.LinearLayout;

public class ImageViewBase extends Activity {

private ImageView imageView;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

initComponent();

/*

* 设置第一个图片的大小 setLayoutParams()表示设置整个布局 表示设置当前的这个布局的是来自线性布局的

* 宽度200,高度100的区域.

*/

imageView.setLayoutParams(new LinearLayout.LayoutParams(200, 100));

setTitle("height" + imageView.getLayoutParams().height + "-- width --"

+ imageView.getLayoutParams().width);

}

private void initComponent() {

imageView = (ImageView) findViewById(R.id.imgaeview);

}

}

程序Demo执行结果:

0818b9ca8b590ca3270a3433284dd417.png

[说明]:可见第一种效果是因为在程序中按长200,宽100 裁剪缩放后的 ImageView 的位置。第二种效果是直接按指定比例长300dp,宽200dp缩放

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值