Android基础控件

98 阅读3分钟

image.png 前言:学习是一件很辛苦很烦躁的事,这从我们踏入校园的那天起就已经深有体会,但我们需要克服这种情绪,需要静下心来去体会学习的乐趣,要抱有好奇心这才能在学习的过程中感受到学习的快乐还有收获知识的乐趣;真正的幸福,不是狂欢后的落寞,而是努力过后满满的充实感和成就感。

ImageView

  • ImageView是Android开发中常用的组件之一,主要用来显示图片的控件。

  • ImageView在日常使用中是可以设置2个图片属性的,分别为:src和Background;前者就是设置图片显示的,后者设置背景图片的,也就是像在上一篇的按钮控件中设置不同的背景颜色是一个道理的。

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/wanxia2"
        android:src="@mipmap/surgeon" />
    

这是设置了背景和图片的的控件,他们是这样子的

image.png 可以发现当我在控件中添加一个属性的时候src就会覆盖背景: android:adjustViewBounds="true"

<ImageView
    android:id="@+id/imageActivity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
    android:background="@mipmap/wanxia2"
    android:src="@mipmap/surgeon" />

image.png

而当我们需要背景和图片相结合的时候就需要给图片设置一个透明度,这样看起来就很像一张图片了,当然会有那种朦胧感,具体透明度看具体需求。

如果你想设置是src图片属性的透明度就用setImageAlpha,如果想控制背景的透明度可以使用getBackgroud().setAlpha设置。

//        设置src透明度;Api>16;范围0-255
        imageView.setImageAlpha(99);
        imageView2.setImageAlpha(99);
//        已弃用
//        imageView.setAlpha(90);
//        imageView2.setAlpha(80);
//        设置背景透明度
//        imageView2.getBackground().setAlpha(80);

image.png

ImageView常用属性

  • android:adjustViewBounds设置缩放是否保存原图长宽比。
  • android:scaleType用于设置显示的图片如何缩放或者移动以适应ImageView的大小
    • fitXY:对图像的横向与纵向进行独立缩放,使得该图片完全适应ImageView,但是图片的横纵比可能会发生改变.
    • fitStart:保持纵横比缩放图片,知道较长的边与Image的编程相等,缩放完成后将图片放在ImageView的左上角.
    • fitCenter:同上,缩放后放于中间;
    • fitEnd:同上,缩放后放于右下角;
    • center:保持原图的大小,显示在ImageView的中心。当原图的size大于ImageView的size,超过部分裁剪处理。
    • centerCrop:保持横纵比缩放图片,直到完全覆盖ImageView,可能会出现图片的显示不完全
    • centerInside:保持横纵比缩放图片,直到ImageView能够完全地显示图片
    • matrix:默认值,不改变原图的大小,从ImageView的左上角开始绘制原图, 原图超过ImageView的部分作裁剪处理. 为了能更加明显的突出这个属性的效果,我通过设置同一张图片,不同的属性值来显示它。这都是根据从头到尾的顺序来写的
       <ImageView
           android:layout_marginTop="15dp"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:scaleType="fitXY"
           android:src="@mipmap/watermelon" />
       <ImageView
           android:layout_marginTop="15dp"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:scaleType="fitStart"
           android:background="#99FF0000"
           android:src="@mipmap/watermelon" />
       <ImageView
           android:layout_marginTop="15dp"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:scaleType="fitCenter"
           android:background="#996CE3E3"
           android:src="@mipmap/watermelon" />
       <ImageView
           android:layout_marginTop="15dp"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:scaleType="fitEnd"
           android:background="#996CE36E"
           android:src="@mipmap/watermelon" />
       <ImageView
           android:layout_marginTop="15dp"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:scaleType="center"
           android:background="#996CE36E"
           android:src="@mipmap/watermelon" />
       <ImageView
           android:layout_marginTop="15dp"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:scaleType="centerCrop"
           android:background="#996CE36E"
           android:src="@mipmap/watermelon" />
       <ImageView
           android:layout_marginTop="15dp"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:scaleType="centerInside"
           android:background="#996CE36E"
           android:src="@mipmap/watermelon" />
       <ImageView
           android:layout_marginTop="15dp"
           android:layout_marginBottom="20dp"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:scaleType="matrix"
           android:background="#996CE36E"
           android:src="@mipmap/watermelon" />

image.png

胜己者,可精进养识;德盛者,可修身养志;直言者,可正己养心;趣味者,可悦己养人。跟谁在一起,是一种选择,更是一场修行。唯有更好的自己,才能遇见更好的对方。