Android中的图片查看器
16lz
2021-01-26
本案例,使用Eclipse来开发Android2.1版本的图片查看器。
1)首先,打开Eclipse,新建一个Android2.1版本的项目ShowTu,打开res/values中目录下的strings.xml,将其中代码替换成一下代码:
路径:ShowTu/res/valus/string.xml
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, ShowTuActivity!</string> <string name="app_name">ShowTu</string><string name="pre">上一副</string><string name="after">下一副</string></resources>
2)打开res/layout文件下的main.xml,布局文件中有两个按钮控件和一个ImageView控件。其中的代码如下:
路径:ShowTu/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" ><!-- 水平线性布局 --><LinearLayout android:id="@+id/LinearLayout01" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_gravity="center" android:layout_height="wrap_content"> <!-- 按钮控件 --><Button android:text="@string/pre" android:id="@+id/Button01" android:textColor="#000000" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button><Button android:text="@string/after" android:id="@+id/Button02" android:textColor="#000000" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button></LinearLayout><ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"> </ImageView> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
3)接着,主Activity文件ShowTuActivity.java,代码如下:
路径:src/com.tu.ui/ShowTuActivity.java
package com.tu.ui;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;public class ShowTuActivity extends Activity { /** Called when the activity is first created. */ImageView iv;//声明ImageView控件int count;//计数器int drawableIds[]={//图片Id数组R.drawable.pic0,R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,R.drawable.pic5,R.drawable.pic6,R.drawable.pic7,R.drawable.pic8,R.drawable.pic9}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);//设置布局 iv=(ImageView)findViewById(R.id.ImageView01);//获取ImageView的引用 iv.setImageResource(R.drawable.pic0);//设置图像 Button pre_btn=(Button)findViewById(R.id.Button01);//获取前一个按钮的引用 pre_btn.setOnClickListener(//添加监听器 new OnClickListener() {@Overridepublic void onClick(View v){if(count>0){iv.setImageResource(drawableIds[--count]);//设置图像}else{Toast.makeText(ShowTuActivity.this,"这是第一幅图片",Toast.LENGTH_SHORT).show();}} } ); Button after_btn=(Button)findViewById(R.id.Button02);//获取下一个按钮的引用 after_btn.setOnClickListener(//添加监听器 new OnClickListener() {@Overridepublic void onClick(View v){if(count<drawableIds.length-1){iv.setImageResource(drawableIds[++count]);//设置图像}else{Toast.makeText(ShowTuActivity.this,"这是最后一幅图片",Toast.LENGTH_SHORT).show();}} } ); }}
效果如下:
图(1)
图(2)
更多相关文章
- Android实现手写签名
- ScrollView嵌套RecycleView相关问题
- AS(android studio) 添加第三方库时报,Error: Failed to resolve:
- 在android studio 2.3.3中使用安卓弹窗 SweetAlertDialog 1.3的
- Android侧拉框Demo
- Android的showDialog()、onPrepareDialog()和onCreateDialog()的
- 五、Fresco——ScaleType focusCrop属性详解
- Android中UDP发送报文
- Android日常知识收集与总结之EditText篇