Android ImageView和ImageButton:图片视图和图片按钮

待我称王封你为后i 2021-12-09 23:21 651阅读 0赞

ImageView 是用于显示图片的组件,在很多场合都有比较普遍的使用。

ImageView 可以显示任意图像,加载各种来源的图片(如资源或图片库)。它还可以负责计算图片的尺寸,以便在任意的布局中使用,并且可以提供缩放或者着色等选项供开发者使用。

ImageButton 是 ImageView 的子类,相当于一个表明是图片而不是文字的 Button。其使用方法和 Button 完全相同。

下面通过一个实例来了解一下这两个组件的使用方法。在工程 WidgetDemo 的布局文件 main.xml 中添加一个名为 ImageButtonDemo 的 Button,用以启动 ImageButtonActivity。

在 main.xml 中添加代码如下:

  1. <Button
  2. android:id=”@+id/button10”
  3. android:layout_width=”wrap_content”
  4. android:layout_height=”wrap_content”
  5. android:text=”ImageButtonDemo” />

单击Button并启动RatingBarActivity的代码如下:

  1. Button imgbtn = (Button)this.findViewById(R.id.button10);
  2. mgbtn.setOnClickListener(new View.OnClickListener(){
  3. @Override
  4. public void onClick(View v){
  5. Intent intent;
  6. intent = new Intent(MainActivity.this, ImageButtonActivity.class);
  7. startActivity(intent);
  8. }
  9. });

同时在 AndroidManifest.xml 文件中声明该 Activity:

ImageButtonActivity 的运行效果如图 1 所示。

ImageButtonActivity的运行效果
图 1 ImageButtonActivity 的运行效果

ImageButtonActivity 的布局文件 imgbtn.xml 内容如下:

  1. <?xml version=”1.0” encoding=”utf-8”?>
  2. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android“
  3. android:layout_width=”match_parent”
  4. android:layout_height=”match_parent”
  5. android:orientation=”vertical”>
  6. <ImageView
  7. android:id=”@+id/imageView1”
  8. android:layout_width=”250dp”
  9. android:layout_height=”250dp”
  10. android:src=”@drawable/girl”/>
  11. <ImageButton
  12. android:id=”@+id/imageButton1”
  13. android:layout_width=”wrap_content”
  14. android:layout_height=”wrap_content”
  15. android:src=”@drawable/ic_launcher”/>

该文件使用 LinearLayout 布局,其中放入了一个 ImageView 组件和一个 ImageButton 组件。两个组件都通过 android:src 属性指定了显示的图片。

该实例用到了两个图片资源,一个为 girl,另一个为 ic_launcher,如图 2 所示。

由于 Android 会根据手机设备的配置高低选择不同的资源,因此为了应用程序的通用性,在三个 drawable 文件夹下都放置了 girl.gif 图像。ic_launcher.png 是系统自带的资源文件。

工程中的图片资源
图 2 工程中的图片资源

ImageButtonActivity.java 的代码如下:

  1. package introduction.android.widgetdemo;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.ViewGroup.LayoutParams;
  6. import android.widget.ImageButton;
  7. import android.widget.ImageView;
  8. public class ImageButtonActivity extends Activity {
  9. private ImageButton imgbtn;
  10. private ImageView imgview;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. // TODO Auto-generated method stub
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.imgbtn);
  16. imgbtn = (ImageButton) this.findViewById(R.id.imageButton1);
  17. imgview = (ImageView) this.findViewById(R.id.imageView1);
  18. imgbtn.setOnClickListener(new View.OnClickListener() {
  19. @Override
  20. public void onClick(View v) {
  21. // TODO Auto-generated method stub
  22. LayoutParams params = imgview.getLayoutParams();
  23. params.height += 3;
  24. params.width += 3;
  25. imgview.setLayoutParams(params);
  26. }
  27. });
  28. }
  29. }

ImageButtonActivity 为 ImageButton 添加了单击监听器,对用户单击 imgbtn 的事件进行了处理。用户每次单击图片按钮,都把 ImageView 组件的宽和高增大 3。随着用户的不断单击,ImageView 中显示的图片越来越大,显示了 ImageView 组件对图片的缩放功能。

发表评论

表情:
评论列表 (有 0 条评论,651人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android 图像视图ImageView

    ImageView是用来显示图像的控件,它和图形显示有关的属性主要有下面两个: 1.scaleType :图形的拉伸类型,默认是fitCenter。 2.色彩:图形来源,s