ANDROID_MARS学习笔记_S01_006ImageView

时间:2022-02-21 06:17:27

一、ImageView介绍

设置scalType

Must be one of the following constant values.

Constant Value Description
matrix 0 Scale using the image matrix when drawing. See setImageMatrix(Matrix).
fitXY 1 Scale the image using FILL.
fitStart 2 Scale the image using START.
fitCenter 3 Scale the image using CENTER.
fitEnd 4 Scale the image using END.
center 5 Center the image in the view, but perform no scaling.
centerCrop 6 Scale the image uniformly (maintain the image's aspect ratio) so both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding). The image is then centered in the view.
centerInside 7 Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding). The image is then centered in the view.

This corresponds to the global attribute resource symbol scaleType.

二、
1.activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <ImageView
android:id="@+id/imageViewId"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/images"
android:scaleType="fitXY"
android:background="#00FF00"/> <ImageView
android:id="@+id/imageView2Id"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/wo"
android:background="#FF0000"
android:scaleType="fitXY"/> </LinearLayout>

2.MainActivity.java

     private ImageView iv;

     @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imageview_layout); iv = (ImageView) findViewById(R.id.imageViewId);
iv.setImageResource(R.drawable.images);
iv.setScaleType(ScaleType.CENTER_INSIDE);