程序员开发实例大全宝库

网站首页 > 编程文章 正文

每日一练之Android——ImageView(打开每日一练)

zazugpt 2024-10-22 18:36:10 编程文章 19 ℃ 0 评论

ImageView(图像视图),就是用来显示图像的一个View或者说控件。

Java代码中设置blackground和src属性:

前景(对应src属性):setImageDrawable( );

背景(对应background属性):setBackgroundDrawable( );


ImageView iv_scale = findViewById(R.id.image_01);

// 延時操作

new Handler().postDelayed(() -> {

// TODO

iv_scale.setImageResource(R.drawable.ic_launcher_foreground);

iv_scale.setBackgroundColor(0xff0000ff);

iv_scale.setScaleType(ImageView.ScaleType.CENTER);

}, 3000);


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

ImageButton 和 Button之间的区别:

ImageButton 可以分别在前景和背景显示图片,只能显示图片不能显示文本,且按比例缩放。

Button 按钮上可显示文本和图片,但通过背景设置的图片会拉伸变形,背景只能显示一张图片

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".ImageViewActivity">

<ImageView

android:id="@+id/image_01"

android:layout_width="match_parent"

android:layout_height="200dp"

android:layout_margin="5dp"

android:layout_marginBottom="24dp"

android:scaleType="centerCrop"

android:src="@mipmap/ic_launcher"

tools:ignore="MissingConstraints"

></ImageView>

<ImageButton

android:id="@+id/imageButton"

android:layout_width="match_parent"

android:layout_height="80dp"

android:scaleType="fitCenter"

android:src="@mipmap/ic_launcher"

app:layout_constraintTop_toBottomOf="@id/image_01"

tools:ignore="MissingConstraints"

></ImageButton>

</androidx.constraintlayout.widget.ConstraintLayout>

如果需要同时展示文本与图像

两种方式

1 利用Layout对ImageView和TextView组合布局

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

- drawableTop:指定文字上方的图片

- drawableBottom:指定文字下方的图片

- drawableLeft:指定文字左方的图片

- drawableRight:指定文字右方的图片

- drawablePadding:指定文字和图片的间距

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_image_view);

ImageView iv_scale = findViewById(R.id.image_01);

// 延時操作

new Handler().postDelayed(() -> {

// TODO

iv_scale.setImageResource(R.drawable.ic_launcher_foreground);

iv_scale.setBackgroundColor(0xff0000ff);

iv_scale.setScaleType(ImageView.ScaleType.CENTER);

}, 3000);

//控制button上图标大小

Button button_01 = findViewById(R.id.button_01);

Drawable drawable1 = getResources().getDrawable(R.drawable.xin_yingxiong);

drawable1.setBounds(0, 0, 40, 40);//第一0是距左边距离,第二0是距上边距离,40分别是长宽

button_01.setCompoundDrawables(drawable1, null, null, null);//只放左边

}

布局

<Button

android:id="@+id/button_01"

android:layout_width="wrap_content"

android:layout_height="60dp"

android:drawableLeft="@drawable/xin_yingxiong"

android:text="图标在左边"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.5"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@id/imageButton"

android:background="#ffffff"

tools:ignore="MissingConstraints"></Button>

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表