Android第五天之ImageView图像视图
2017-08-29 22:44
357 查看
描述:往项目导入五张图片,通过点击两个Button上一张和下一张在Activity中切换图片的显示.
布局文件fragment_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="15dp" > <ImageView android:id="@+id/imageView1" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" android:src="@drawable/a1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="70dp" android:text="下一张" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上一张" /> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.21" android:text="" /> </LinearLayout> </LinearLayout>
MainActivity.java 文件:
package com.example.imageview; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends ActionBarActivity { private ImageView imageview; public int count = 0; TextView text1; //创建一个一维数组保存图片id int[] imageid = {R.drawable.a1, R.drawable.a2,R.drawable.a3, R.drawable.a4,R.drawable.a5,}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); //获取TextView的id text1 = (TextView) findViewById(R.id.text1); //获取ImageView的id imageview = (ImageView) findViewById(R.id.imageView1); //获取两个Button的id Button btn1 = (Button) findViewById(R.id.button1); Button btn2 = (Button) findViewById(R.id.button2); //创建监听对象 ClickListener Listener = new ClickListener(); //监听两个Button btn1.setOnClickListener(Listener); btn2.setOnClickListener(Listener); } //创建监听方法 class ClickListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button1: count = (count+1+imageid.length)%imageid.length; text1.setText("第"+(count+1)+"张"); imageview.setImageResource(imageid[count]); break; case R.id.button2: text1.setText("第"+count+"张"); count = (count-1+imageid.length)%imageid.length; imageview.setImageResource(imageid[count]); break; } } } }
相关文章推荐
- Android开发总结笔记 ImageView(图像视图) 1-1-8
- Android图像开源视图:SmartImageView
- Android图像开源视图:SmartImageView
- Android基础入门教程——2.3.4 ImageView(图像视图)
- android-UI组件实例大全(六)------ImageView图像视图
- Android图像开源视图:SmartImageView
- Android开源框架----1. SmartImageView图像视图
- Android--Android图像开源视图:SmartImageView
- Android图像开源视图:SmartImageView
- android-UI组件实例大全(六)------ImageView图像视图
- Android入门---ImageView(图像视图)
- Android学习——ImageView图像视图的属性详情
- Android图像开源视图:SmartImageView
- Android图像开源视图:SmartImageView
- Android练习-----图像视图(ImageView)
- Android图像开源视图:SmartImageView
- 【android学习】imageview通过getDrawingCache获取图像
- ImageView(图像视图)-src属性和background属性的区别:
- android 控件 图片视图(ImageView)
- android-----使用ImageSpan对象在TextView组件中显示图像