刘俊凯 发表于 2025-1-2 11:27:42

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> https://i-blog.csdnimg.cn/direct/1a7ee4adfa674930ad7392db949d5278.png
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);
    }
} https://i-blog.csdnimg.cn/direct/e30981d28eba434f83d19f35ce18372c.png

二、图像视图的缩放类型
lmageView本身默认图片居中体现,若要改变图片的体现方式,可通过scaleType属性设定,该属性的取值阐明如下:
https://i-blog.csdnimg.cn/direct/91293653699d4b7d98ba9c72660e29ab.png
1.fitXY
https://i-blog.csdnimg.cn/direct/2bfb9ffbf0074ef6a0d8f9330a7e2c80.png
https://i-blog.csdnimg.cn/direct/d80eb56d584345cd8c9d810ccbe53680.png
2.fitStart
https://i-blog.csdnimg.cn/direct/d19d297b2f24401fbc4c0303302636b3.png
https://i-blog.csdnimg.cn/direct/79c45df736c14e9d87c0596398f8bb66.png
3.fitCenter
https://i-blog.csdnimg.cn/direct/a222549663f14767a48dc983d835ea97.png
https://i-blog.csdnimg.cn/direct/4e3823938da74a7681c2390a32f87205.png
4.center
https://i-blog.csdnimg.cn/direct/1b08390ac3234a1b95154a80a3ab8508.png
https://i-blog.csdnimg.cn/direct/c00b42c6a5764be08e76f9e782e23c26.png
5.centerCrop
https://i-blog.csdnimg.cn/direct/b9c8ad619c904016a04c8a29cc37b56a.png
https://i-blog.csdnimg.cn/direct/3320e03c35ee4559af9e592a44f00aa9.png
6.centerInside
https://i-blog.csdnimg.cn/direct/3e9bf729f3fd4d438dfe0b6a9eec0fc6.png
https://i-blog.csdnimg.cn/direct/6a20d00492de49bda62a0afb6b4867d3.png
centerInside和center的体现效果一模一样,这缘于它们的缩放规则设定。表面上fitCenter、centerInside、center三个类型都是居中体现,且均不越过图像视图的边界。它们之间的区别在于:fitcenter既答应缩小图片、也答应放大图片,centerinside只答应缩小图片、不答应放大图标,而center自始至终保持原始尺寸(既不答应缩小图片、也不答应放大图片)。因此,当图片尺寸大于视图宽高,centerInside与fitCenter都会缩小图片,此时它俩的体现效果相同;当图片尺寸小于视图宽高,centerInside与center都保持图片大小不变,此时它俩的体现效果相同。

三、Java代码中设置图片大小
https://i-blog.csdnimg.cn/direct/8ebf633536b74b99bf299eac10d30321.png
例如:
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);
    }
} https://i-blog.csdnimg.cn/direct/bc656da3af8f4113a8eb5263729623d2.png

四、图像按钮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> https://i-blog.csdnimg.cn/direct/70547ba1e5684831993d5e65a512ec09.png
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:指定图片与笔墨的间距。
设置如下视图:
https://i-blog.csdnimg.cn/direct/c9fa8136512d466c963a8bc6ae3449df.png
代码如下:
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> https://i-blog.csdnimg.cn/direct/4e61053c4d884a3196d400b0ec864985.png
https://i-blog.csdnimg.cn/direct/bcd54de6e81b42b98379f2cac7803b0c.png
我们发现配景颜色默认是紫色
如果要修改配景颜色,就要在res/values目录下的themes.xml文件内继续于bridge
https://i-blog.csdnimg.cn/direct/58882a87ba164ae686a3143695e44f7c.png
https://i-blog.csdnimg.cn/direct/2117a1dc203a43ada458e7eb27c7f736.png
https://i-blog.csdnimg.cn/direct/fecef73b31984f7fa15693f2ffc848a6.png





免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: AndroidStudio-图像体现