android脚步---简单图片浏览器改变图像透明度
图片浏览器调用ImageView的setAlpha方法来实现改变图片透明度。

main.xml文件如下:三个按钮,两个imageview,,界面定义了两个ImageView,一个是显示局部图片的ImageView,android:scaleType="fitCenter表明图片会保持横纵比缩放,并将缩放后图片放在该imageview中央。
<?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:layout_width="match_parent"
android:layout_height="wrap_content" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增大透明度" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="减小透明度" /> <Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一张" />
</LinearLayout>
<!-- 定义显示整体图片的ImageView --> <ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000ff"
android:scaleType="fitCenter"
android:src="@drawable/shuangta" />
<!-- 定义显示局部图片的ImageView --> <ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#0000ff" /> </LinearLayout>
设置监听器,按钮改变图片的Alpha值,在第一个imageview上添加ontouchlistener,发生触摸事件时,程序从原图取出相应部分图片,显示在第二个imageview中。
package com.example.imageview; import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
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; public class MainActivity extends Activity {
// 定义一个访问图片的数组
int[] images = new int[] { R.drawable.lijiang, R.drawable.qiao,
R.drawable.shuangta, R.drawable.shui, R.drawable.xiangbi,
R.drawable.ic_launcher, };
// 定义当前显示的图片
int currentImage = 2;
// 定义图片的初始透明度
private int alpha = 255; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button plusButton = (Button) findViewById(R.id.button1);
final Button minuxButton = (Button) findViewById(R.id.button2);
final Button nextButton = (Button) findViewById(R.id.button3); final ImageView imageview1 = (ImageView) findViewById(R.id.imageView1);
final ImageView imageview2 = (ImageView) findViewById(R.id.imageView2); // 定义查看下一张图片的时间监听器
nextButton.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
if (currentImage >= 5) {
currentImage = -1;
}
BitmapDrawable bitmap = (BitmapDrawable) imageview1
.getDrawable();
// 如果图片还没有回收,先强制回收图片
if (!bitmap.getBitmap().isRecycled()) {
bitmap.getBitmap().recycle();
}
// 改变ImageView的图片
imageview1.setImageBitmap(BitmapFactory.decodeResource(
getResources(), images[++currentImage]));
}
}); // 定义改变图片透明度的方法
OnClickListener listener = new OnClickListener() { @Override
public void onClick(View v) {
if (v == plusButton) {
alpha += 20;
}
if (v == minuxButton) {
alpha -= 20;
}
if (alpha > 255) {
alpha = 255;
}
if (alpha <= 0) {
alpha = 0;
}
// 改变图片的透明度
imageview1.setAlpha(alpha); }
}; // 为2个按钮添加监听器
plusButton.setOnClickListener(listener);
minuxButton.setOnClickListener(listener);
imageview1.setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
BitmapDrawable bitmapDeaw = (BitmapDrawable) imageview1
.getDrawable();
// 获取第一个图片显示框中的位图
Bitmap bitmap = bitmapDeaw.getBitmap();
double scale = bitmap.getWidth();
// 或许需要显示图片的开始点
int x = (int) (arg1.getX() * scale);
int y = (int) (arg1.getY() * scale);
if (x + 120 > bitmap.getWidth()) {
x = bitmap.getWidth() - 120;
}
if (y + 120 > bitmap.getHeight()) {
y = bitmap.getHeight() - 120;
} // 显示图片的指定区域
imageview2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y,
120, 120));
imageview2.setAlpha(alpha);
return false;
}
});
} }
android脚步---简单图片浏览器改变图像透明度的更多相关文章
- javacpp-opencv图像处理之2:实时视频添加图片水印,实现不同大小图片叠加,图像透明度控制,文字和图片双水印
欢迎大家积极开心的加入讨论群 群号:371249677 (点击这里进群) javaCV图像处理系列: javaCV图像处理之1:实时视频添加文字水印并截取视频图像保存成图片,实现文字水印的字体.位置. ...
- Android小案例——简单图片浏览器
今天上午休息看Android书,里面有个变化图片的示例引起了我的兴趣. 示例需求: 有N张图片,循环显示图片的内容.如果需求让我写我会使用一个变量count来保存显示图片数据的索引,图片显示时做个判断 ...
- [Android] 对自定义图片浏览器经常内存溢出的一些优化
首先关于异步加载图片可以参见 夏安明 的博客:http://blog.csdn.net/xiaanming/article/details/9825113 这篇文章最近有了新的更改,大概看了一下,内容 ...
- android调用系统图片浏览器裁切后出现黑边
是这样的:我使用系统的图片浏览器,然后让它自动跳到图片裁切界面,当我们定义了返回的图片大小过大,而我们实际的图片像素达不到时,系统为我们自动地填充了不够的像素成黑色,那么我们怎么样来解决这个问题呢?不 ...
- Android简单图片浏览器
效果如下: 代码编写如下: Crize_demo\app\src\main\res\layout\activity_main.xml <!--定义一个线性布局--> ...
- Android 简单图片浏览器 读取sdcard图片+形成缩略图+Gallery
1.读取SD卡上面的图片信息 //想要的返回值所在的列 String[] projection = { MediaStore.Images.Thumbnails._ID}; //图片信息存储在 and ...
- Ubuntu上Qt之简单图片浏览器
>>主要功能: (1)图片切换浏览,上一张/下一张. (2)图片放大.缩小.包括两种机制:鼠标滚轮和按钮放大/缩小. (3)图片自动循环播放,间隔2s.点击播放后,其他操作均无效,直至点 ...
- Android:ImageView应用之图片浏览器
ImageView控件实现的简单图片浏览器 一.纯显示图片: 引言: 读者在做这个东西的时候,需要自己把图片在源程序中导入. 读者要注意:所有导入的图片之前,图片的命名只可以是小写英文和数字. 效果图 ...
- 【Android 应用开发】AndroidUI设计 之 图片浏览器
图片浏览器效果图 : 源码下载地址 : -- CSDN : http://download.csdn.net/detail/han1202012/6875083 -- GitHub : https:/ ...
随机推荐
- Codeforces Round #364 (Div. 2) E. Connecting Universities
E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes input s ...
- hdu_4826_Labyrinth_2014百度之星(dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4826 题意:中文题,不解释 题解:dp搞,第一列只能从上往下走,所以先算出第一列的dp数组,然后开两个 ...
- Linux 解决 bash ./ 没有那个文件或目录 的方法
在Debian 或 ubuntu 64位下运行 ./xxx 会跳出来说没有这个文件或者目录,但是ls看又有这个文件,很是奇怪. 其实原因很简单,是因为他没有32位的运行库 ia32-libs ,直接安 ...
- LeetCode OJ 226. Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- ios控件 UIControl
< UIControl> 1 处理用户事件的控件的基类,如UIButton,UISlider等 2 一般不直接实例化,而是使用他的子类 3 可以通过跟踪触摸事件来设置和获取控件状态,并且这 ...
- CentOS 7 rsync
CentOS 7 rsync 1)软件简介 Rsync 是一个远程数据同步工具,可通过 LAN/WAN 快速同步多台主机间的文件.Rsync 本来是用以取代rcp 的一个工具,它当前由 Rsync.s ...
- CentOS7 PostgreSQL 主从配置( 三)
postgres 主备切换 主备查看 方法 ps -ef | grep wal (主库 sender)postgres 27873 27864 0 5月06 ? 00:00:10 postgres: ...
- Ansible9:条件语句【转】
在有的时候play的结果依赖于变量.fact或者是前一个任务的执行结果,从而需要使用到条件语句. 一.when 有的时候在特定的主机需要跳过特定的步骤,例如在安装包的时候,需要指定主机的操作系统 ...
- px和sp什么区别
都表示像素,只不过sp通常表示文字大小: <TextView android:layout_width="wrap_content" android:layout_heigh ...
- shell 中的特殊符号的含义
来源:http://blog.sina.com.cn/s/blog_62a151be0100x9rn.html 第四章 基本功 - 特殊符号 学习撰写 script 最迅速的捷径是观摩别人的 scri ...