Android:ImageView应用之图片浏览器
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应用之图片浏览器的更多相关文章
- Android ImageView显示本地图片
Android ImageView 显示本地图片 布局文件 <?xml version="1.0" encoding="utf-8"?> <R ...
- Android ImageView(scaleType属性)图片按比例缩放
<ImageView android:id="@+id/img" android:src="@drawable/logo" android:scaleTy ...
- Android imageview显示圆形图片
需要ImageView显示圆形图片做法如下 public static Bitmap toRoundCorner(Bitmap bitmap, float ratio) { System.out.pr ...
- Android小案例——简单图片浏览器
今天上午休息看Android书,里面有个变化图片的示例引起了我的兴趣. 示例需求: 有N张图片,循环显示图片的内容.如果需求让我写我会使用一个变量count来保存显示图片数据的索引,图片显示时做个判断 ...
- Android ImageView高度根据图片比例自适应
设置adjustViewBounds // 是否保持宽高比 <ImageView android:id="@+id/iv_test" android:layout_width ...
- Android ImageView圆形头像
转载自:http://m.oschina.net/blog/321024 Android ImageView圆形头像 图片完全解析 我们在做项目的时候会用到圆形的图片,比如用户头像,类似QQ.用户在用 ...
- 【转】Android ImageView圆形头像
Android ImageView圆形头像 图片完全解析 我们在做项目的时候会用到圆形的图片,比如用户头像,类似QQ.用户在用QQ更换头像的时候,上传的图片都是矩形的,但显示的时候确是圆形的. 原理: ...
- 一步一步打造自己的Android图片浏览器(原创)
今天我们试着来制作一个自己的Android图片浏览器. 图片浏览器应该具有什么功能呢?鉴于不同的人不同的理解,这里提出一个基本的需求: 搜索手机内的所有图片,展示于一个列表中: 列表中展示的是图片的缩 ...
- Android中轴旋转特效实现,制作别样的图片浏览器
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/10766017 Android API Demos中有很多非常Nice的例子,这些例 ...
随机推荐
- Android ProgressDialog 加载进度
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...
- Meth | ubuntu下安装与卸载软件方法
1.通过deb包安装的情况: 安装.deb包: 代码:sudo dpkg -i package_file.deb反安装.deb包:代码:sudo dpkg -r package_name 2.通过ap ...
- Java基础知识强化79:被遗忘的Java Math类
1. Math类概述 Math类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数. 2. 成员变量 和 成员方法(常用的) (1)成员变量 public static final d ...
- linux下查看文件系统类型
1. df -hT命令 -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G) -T, --pr ...
- 正式进入C#的世界——委托
委托(delegate)1.可以认为是这样的对象,它包含具有相同签名和返回值类型的有序方法列表.2.可以理解为函数的一个包装,它使得C#中的函数可以作为参数来被传递. 委托的定义和方法的定义类似,只是 ...
- 解决 iOS webkit 使用CSS动画时闪烁的问题
-webkit-backface-visibility: hidden;
- mysql的distinct理解
select distinct id,name from route where update_time>=''; 上面的sql语句的逻辑是两条记录的id,name只要有一个不一样,就算不一样. ...
- mysql limit性能问题
offset大的时候的比较 1. SELECT * FROM persons LIMIT 200000,10; 耗时0.109s 2. SELECT *FROM persons WHERE id> ...
- #define 和 typedef场合
#define定义“可读”的常量以及一些宏语句的任务,而typedef则常用来定义关键字.冗长的类型的别名.
- jQuery旋转插件
jQuery旋转插件,支持Internet Explorer 6.0 + .Firefox 2.0.Safari 3.Opera 9.Google Chrome,高级浏览器下使用Transform,低 ...