【Android开发】基本组件-图像视图

图像视图ImageView:
用于在屏幕中显示任何Drawable对象,通常用来显示图片。

ImageView支持的常用XML属性:

属性名称

描述

android:adjustViewBounds

是否保持宽高比。需要与maxWidth、MaxHeight一起使用,否则单独使用没有效果。

android:cropToPadding

是否截取指定区域用空白代替。单独设置无效果,需要与scrollY一起使用,效果如下,实现代码见代码部分:

android:maxHeight

设置View的最大高度,单独使用无效,需要与setAdjustViewBounds一起使用。如果想设置图片固定大小,又想保持图片宽高比,需要如下设置:

1) 设置setAdjustViewBounds为true;

2) 设置maxWidth、MaxHeight;

3) 设置设置layout_width和layout_height为wrap_content。

android:maxWidth

设置View的最大宽度。同上。

android:scaleType

设置图片的填充方式。

matrix

0

用矩阵来绘图

fitXY

1

拉伸图片(不按比例)以填充View的宽高

layout_height:30px


layout_width:120px

fitStart

2

按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的左边

fitCenter

3

按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的中间

fitEnd

4

按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的右边

center

5

按原图大小显示图片,但图片宽高大于View的宽高时,截图图片中间部分显示

 

layout_height:60px


layout_width:80px


padding:10px

centerCrop

6

按比例放大原图直至等于某边View的宽高显示。

centerInside

7

当原图宽高或等于View的宽高时,按原图大小居中显示;反之将原图缩放至View的宽高居中显示。

android:src

设置View的drawable(如图片,也可以是颜色,但是需要指定View的大小)

android:tint

将图片渲染成指定的颜色。见下图:

左边为原图,右边为设置后的效果,见后面代码。

 

实例效果:


实现代码:
package com.example.test;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;


public class MainActivity extends Activity {
	private OnCheckedChangeListener checkBox_listener;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	    
	}
}

res/layout/main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:orientation="horizontal"
	android:screenOrientation="landscape"
	android:background="#FFFFFF">
	<ImageView android:src="@drawable/music"
	    android:id="@+id/img1"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_margin="5px"/>
	<ImageView android:src="@drawable/music"
	    android:id="@+id/img2"
	    android:maxWidth="60px"
	    android:maxHeight="70px"
	    android:adjustViewBounds="true"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_margin="5px"/>
	<ImageView android:src="@drawable/music"
	    android:id="@+id/img3"
	    android:scaleType="fitEnd"
	    android:layout_width="60px"
	    android:layout_height="70px"
	    android:layout_margin="5px"/>
	<ImageView android:src="@drawable/music"
	    android:id="@+id/img4"
	    android:tint="#77ff0000"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_margin="5px"/>
</LinearLayout>

转载请注明出处:http://blog.csdn.net/acmman/article/details/44805159
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

光仔December

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值