ImageView控件实现的简单图片浏览器

一.纯显示图片:

引言:

读者在做这个东西的时候,需要自己把图片在源程序中导入。

读者要注意:所有导入的图片之前,图片的命名只可以是小写英文和数字。

效果图

 关键代码片段:

imageView.setOnClickListener(new OnClickListener()

{

public void onClick(View v)

{                 imageView.setImageResource(images[++currentImg%images.length]);

}

});

其中加了黄色背景的代码循环显示图片。

全部代码:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView; public class MainActivity extends Activity { int[] images=new int[]{R.drawable.lrp1,
R.drawable.lrp2,
R.drawable.ls,
R.drawable.mr};
int currentImg=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//LinearLayout main= (LinearLayout) findViewById(R.id.root);
final ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageResource(images[0]);
imageView.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
imageView.setImageResource(images[++currentImg%images.length]);
}
}); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00f"
android:layout_marginTop="10dp"/>
</LinearLayout>

二.通过使用其它控件控制图片:

关键代码:

next.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
image1.setImageResource(images[++currentImg%images.length]);
} });
OnClickListener listener=new OnClickListener()
{
@SuppressWarnings("deprecation")
public void onClick(View v)
{
if(v == plus)
{
alpha += 20;
if(alpha >=255)
{
alpha=255;
}
}
else if(v==minus)
{
alpha -= 20;
if(alpha <= 0)
{
alpha = 0;
}
}
image1.setAlpha(alpha);
} };
plus.setOnClickListener(listener);
minus.setOnClickListener(listener);
}

简而言之:在监听中添加对ImageView属性的控制。

全部代码:

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TabHost; public class MainActivity extends Activity {
private int[] images=new int[]{R.drawable.lrp1,
R.drawable.lrp2,
R.drawable.ls,
R.drawable.mr};
private int currentImg=0;
private int alpha=255;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//得到引用
final ImageView image1 = (ImageView) findViewById (R.id.image1);
final Button plus = (Button) findViewById(R.id.button1);
final Button minus = (Button) findViewById(R.id.button2);
final Button next = (Button) findViewById(R.id.button3);
//设置监听按钮
next.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
image1.setImageResource(images[++currentImg%images.length]);
} });
OnClickListener listener=new OnClickListener()
{
@SuppressWarnings("deprecation")
public void onClick(View v)
{
if(v == plus)
{
alpha += 20;
if(alpha >=255)
{
alpha=255;
}
}
else if(v==minus)
{
alpha -= 20;
if(alpha <= 0)
{
alpha = 0;
}
}
image1.setAlpha(alpha);
} };
plus.setOnClickListener(listener);
minus.setOnClickListener(listener);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增大透明度"
android:gravity="left" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="降低透明度"
android:gravity="left"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="下一张" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
> <ImageView
android:id="@+id/image1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="@drawable/lrp1" /> </LinearLayout>
</LinearLayout>

Android:ImageView应用之图片浏览器的更多相关文章

  1. Android ImageView显示本地图片

    Android ImageView 显示本地图片 布局文件 <?xml version="1.0" encoding="utf-8"?> <R ...

  2. Android ImageView(scaleType属性)图片按比例缩放

    <ImageView android:id="@+id/img" android:src="@drawable/logo" android:scaleTy ...

  3. Android imageview显示圆形图片

    需要ImageView显示圆形图片做法如下 public static Bitmap toRoundCorner(Bitmap bitmap, float ratio) { System.out.pr ...

  4. Android小案例——简单图片浏览器

    今天上午休息看Android书,里面有个变化图片的示例引起了我的兴趣. 示例需求: 有N张图片,循环显示图片的内容.如果需求让我写我会使用一个变量count来保存显示图片数据的索引,图片显示时做个判断 ...

  5. Android ImageView高度根据图片比例自适应

    设置adjustViewBounds // 是否保持宽高比 <ImageView android:id="@+id/iv_test" android:layout_width ...

  6. Android ImageView圆形头像

    转载自:http://m.oschina.net/blog/321024 Android ImageView圆形头像 图片完全解析 我们在做项目的时候会用到圆形的图片,比如用户头像,类似QQ.用户在用 ...

  7. 【转】Android ImageView圆形头像

    Android ImageView圆形头像 图片完全解析 我们在做项目的时候会用到圆形的图片,比如用户头像,类似QQ.用户在用QQ更换头像的时候,上传的图片都是矩形的,但显示的时候确是圆形的. 原理: ...

  8. 一步一步打造自己的Android图片浏览器(原创)

    今天我们试着来制作一个自己的Android图片浏览器. 图片浏览器应该具有什么功能呢?鉴于不同的人不同的理解,这里提出一个基本的需求: 搜索手机内的所有图片,展示于一个列表中: 列表中展示的是图片的缩 ...

  9. Android中轴旋转特效实现,制作别样的图片浏览器

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/10766017 Android API Demos中有很多非常Nice的例子,这些例 ...

随机推荐

  1. gzip优化网络传输量提高传输效率[转]

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;us ...

  2. 5 Java学习之 泛型

    1. 基本概念          泛型是Java SE 1.5的新特性,泛型的本质是 参数化类型 ,也就是说所操作的 数据类型 被指定为一个参数.这种参数类型可以用在类.接口和方法的创建中,分别称为 ...

  3. passwd命令限制用户密码到期时间

    1.passwd命令 [root@rhel7 skel]# passwd -n -w -x rusky Adjusting aging data for user rusky. passwd: Suc ...

  4. sass笔记-2|Sass基础语法之让样式表更具条理性和可读性

    这一篇主要详述保持sass条理性和可读性的3个最基本方法--嵌套.导入和注释. 零. 变量 变量本身的作用是为了保持属性值的可维护性,把所有需要维护的属性值放在同一个地方,快速更改,处处生效,可谓售后 ...

  5. ASP.NET页面周期

    上图为ASP.NET页面生命周期图. 以下详细讲解一下ASP.NET的页面生命周期.   请求页 请求页发生在页生命周期之前.用户请求时,ASP.NET将确定是否需要分析和编译页面,或者是否可以在不运 ...

  6. ICSharpCode.SharpZipLib实现压缩解压缩

    最近,在项目中经常需要处理压缩和解压缩文件的操作.经过查找,发现了ICSharpCode.SharpZipLib.dll ,这是一个完全由c#编写的Zip, GZip.Tar . BZip2 类库,可 ...

  7. PHP Zip File

    安装 如需在服务器上运行 Zip File 函数,必须安装这些库: Guido Draheim 的 ZZIPlib 库: 下载 ZZIPlib 库 Zip PELC 扩展:下载 Zip PELC 扩展 ...

  8. mysql UNIX时间戳与日期的相互转换

    UNIX时间戳转换为日期用函数: FROM_UNIXTIME() select FROM_UNIXTIME(1156219870); 日期转换为UNIX时间戳用函数: UNIX_TIMESTAMP() ...

  9. ASP.NET执行cmd命令

    批处理命令,是执行速度最快效益最高的命令.因为批处理命令,说白了,就是ms-dos环境下的命令,有很多的批处理命令,都是纯DOS下的命令. 然而,批处理命令尽管功能强大,却存在不足之处.批处理命令只能 ...

  10. 学习开发jquery插件

    先学习http://www.cnblogs.com/playerlife/archive/2012/05/11/2495269.html http://www.cnblogs.com/fromeart ...