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. UVa 11063 - B2-Sequence

    题目:给你一组数据{ b1,b2....,bk }中,推断是否随意两个数字的和都不同. 分析:数论.计算出全部结果,排序推断相邻结果是否同样就可以. 说明:注意数据的合法性检查. #include & ...

  2. Java基础知识强化之集合框架笔记01:集合的由来与数组的区别

    1. 集合的由来: 我们学习的是面向对象语言,而面向对象语言对事物的描述是通过对象体现的,为了方便对多个对象进行操作,我们就必须把这多个对象进行存储.而要想存储多个对象,就不能是一个基本的变量,而应该 ...

  3. 与useradd命令相关的两个默认配置文件

      Configuration Files for User Management Defaults   When working with tools as useradd, some defaul ...

  4. (转)java 23种设计模式

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

  5. ASP.NET-FineUI开发实践-6(三)

    自动补全也算是好东西吧,我也不清楚下拉列表可以过滤为啥还有自动补全,其实自动补全用到还是通过jq获取服务端的动态数据补全.我没做动态的例子,其实好写,就不写了. 1.用到了两个js包 <scri ...

  6. 原生js-拉勾网首页效果

    拉勾网首页公司广告位的悬浮划过效果着实很吸引我.如下(不会做动图!--,感兴趣的可以去拉勾看看): 此处最吸引我的地方在于将鼠标划过上面一排公司列表时,感觉像是绿色的区块跟着你的鼠标移动一样,颇有动感 ...

  7. spring源码分析

    编译问题 spring-4.0.5.release编译是用jdk8编译的,为啥可以运行在jdk7的环境? 源码分析 spring源码分析,由一个点各个击破,比如依赖注入,autowired. spri ...

  8. smarty半小时快速上手教程(转)

    来源于:http://www.chinaz.com/program/2010/0224/107006.shtml 一:smarty的程序设计部分: 在smarty的模板设计部分我简单的把smarty在 ...

  9. ORA-00214: controlfile '/u01/app/oracle/oradata/[sid]/control01.ctl' version inconsistent with file '/u01/app/oracle/oradata/[sid]/control03.ctl'

    Sample error: SQL> startupORACLE instance started. Total System Global Area 285212672 bytesFixed ...

  10. ionic开发环境搭建

    Advanced HTML5 mobile development framework and SDK. Build incredible mobile apps with web technolog ...