当前位置: 移动技术网 > 移动技术>移动开发>Android > Android开发实现的图片点击切换功能示例

Android开发实现的图片点击切换功能示例

2019年07月23日  | 移动技术网移动技术  | 我要评论

本文实例讲述了android开发实现的图片点击切换功能。分享给大家供大家参考,具体如下:

java 代码

public class mainactivity extends appcompatactivity {
  //定义一个访问图片的数组
  int[] images = new int[]{
      r.drawable.java,
      r.drawable.javaee,
      r.drawable.swift,
      r.drawable.ajax,
      r.drawable.html,
  };
  //用于图片切换
  int currenimg = 0;
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    //获取linearlayout布局容器
    linearlayout main = (linearlayout) findviewbyid(r.id.root);
    //创建imageview组件
    final imageview image = new imageview(this);
    //将imageview组建添加到linearlayout布局中
    main.addview(image);
    //初始化显示第一张图片
    image.setimageresource(images[0]);
    image.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view v) {
        image.setimageresource(images[++currenimg % images.length]);
      }
    });
  }
}

xml 文件

<?xml version="1.0" encoding="utf-8"?>
<linearlayout
  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:id="@+id/root"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context=".mainactivity">
</linearlayout>

效果

更多关于android相关内容感兴趣的读者可查看本站专题:《android图形与图像处理技巧总结》、《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网