AndroidStudio-图像显示

一、图像视图ImageView

图像视图展示的图片通常位于res/drawable目录,设置图像视图的显示图片有两种方式:

1.在XML文件中,通过属性android:src设置图片资源,属性值格式形如"@drawable/不含扩展名的图片名称”

例如:

<?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/iv_scale"
        android:layout_width="match_parent"
        android:layout_height="220dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/xiao"/>

</LinearLayout>

2.在Java代码中,调用setlmageResource方法设置图片资源,方法参数格式形如“R.drawable.不
含扩展名的图片名称”

package com.example.chapter01;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ImageView;

public class ImageScaleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_scale);
        ImageView iv_scale = findViewById(R.id.iv_scale);
        iv_scale.setImageResource(R.drawable.xiao);
    }
}

二、图像视图的缩放类型

lmageView本身默认图片居中显示,若要改变图片的显示方式,可通过scaleType属性设定,该属性的取值说明如下:

1.fitXY

2.fitStart

3.fitCenter

4.center

5.centerCrop

6.centerInside

centerInside和center的显示效果一模一样,这缘于它们的缩放规则设定。表面上fitCenter、centerInside、center三个类型都是居中显示,且均不越过图像视图的边界。它们之间的区别在于:fitcenter既允许缩小图片、也允许放大图片,centerinside只允许缩小图片、不允许放大图标,而center自始至终保持原始尺寸(既不允许缩小图片、也不允许放大图片)。因此,当图片尺寸大于视图宽高,centerInside与fitCenter都会缩小图片,此时它俩的显示效果相同;当图片尺寸小于视图宽高,centerInside与center都保持图片大小不变,此时它俩的显示效果相同。

三、Java代码中设置图片大小

例如:

package com.example.chapter01;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ImageView;

public class ImageScaleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_scale);
        ImageView iv_scale = findViewById(R.id.iv_scale);
        iv_scale.setImageResource(R.drawable.xiao);
        iv_scale.setScaleType(ImageView.ScaleType.CENTER);
    }
}

四、图像按钮ImageButton

lmageButton是显示图片的图像按钮,但它继承自lmageView,而非继承Button。

lmageButton和Button之间的区别有:

1.Button既可显示文本也可显示图片,ImageButton只能显示图片不能显示文本。

2.lmageButton上的图像可按比例缩放,而Button通过背景设置的图像会拉伸变形

3.Button只能靠背景显示一张图片,而lmageButton可分别在前景和背景显示图片,从而实现两张图片叠加的效果。

例如:

<?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">

    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:src="@drawable/sqrt"
        android:scaleType="fitCenter"/>
</LinearLayout>

lmageButton的使用场合:在某些场合,有的字符无法由输入法打出来,或者某些文字以特殊字体展示,就适合先切图再放到ImageButton。例如:开平方符号、,等等。

lmageButton与lmageView之间的区别有:

1.lmageButton有默认的按钮背景,lmageView默认无背景:

2.lmageButton默认的缩放类型为center,而ImageView默认的缩放类型为fitCenter。

五、同时展示文本和图像

同时展示文本与图像的可能途径包括:

1.利用LinearLayout对lmageView和TextView组合布局

2.通过按钮控件Button的drawable属性设置文本周围的图标。

(1)drawableTop:指定文字上方的图片。

(2)drawableBottom:指定文字下方的图片。

(3)drawableLeft:指定文字左边的图片。

(4)drawableRight:指定文字右边的图片

(5)drawablePadding:指定图片与文字的间距。

设置如下视图:

代码如下:

1.drawableLeft:

<?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">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/img"
        android:drawablePadding="5dp"
        android:text="图标在左" />

</LinearLayout>

我们发现背景颜色默认是紫色

如果要修改背景颜色,就要在res/values目录下的themes.xml文件内继承于bridge

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值