源码教程 2025年06月7日
0 收藏 0 点赞 613 浏览 2636 个字
摘要 :

ImageView 是用于显示图片的组件,是Android开发中最为常用的控件之一。ImageView 可以显示任意图像,加载各种来源的图片(如资源或图片库)。它还可以负责计算图片的尺……

ImageView 是用于显示图片的组件,是Android开发中最为常用的控件之一。

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:

<activity android:name=".ImageButtonActivity"></activity>

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

Android图片视图控件ImageView和图片按钮控件ImageButton使用

图 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"/>

  16. </LinearLayout>

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

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

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

Android图片视图控件ImageView和图片按钮控件ImageButton使用

图 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 组件对图片的缩放功能。

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/953.html

管理员

相关推荐
2025-07-05

对于一些使用WordPress进行外贸建站的商家来说,大部分人会通过在WordPress中添加JS代码和CSS样式表…

701
2025-07-05

当商家遇到WordPress独立站改版或者域名到期等问题时,不免会涉及到WordPress域名的更改,那么这一…

715
2025-07-05

用户在使用WooCommerce进行跨境电商独立站搭建工作时,有可能会借助WooCommerce短代码实现更加醒目…

307
2025-07-05

随着外贸建站行业的不断深入发展,WordPress的多语言功能也显得越来越重要,一个具有多语言的独立站…

1,039
2025-07-05

WooCommerce作为WordPress外贸建站生态的一部分,WooCommerce运费设置是商家在建站过程中不可忽视的…

835
2025-07-05

在外贸建站的过程中,有些商家会选择使用WordPress幻灯片为网站页面增加一定的动感和可观性,进而提…

723
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号