Android控件之ImageView探究

news/2024/9/22 7:21:09

ImageView控件是一个图片控件,负责显示图片。

以下模拟手机图片查看器

目录结构

2011022121265932.png

main.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent">
<ImageView android:id="@+id/imageView"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_gravity
="center_horizontal"
android:src
="@drawable/p1"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="horizontal"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:layout_gravity
="center_horizontal">
<Button android:id="@+id/previous"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="上一张"
android:layout_gravity
="center_horizontal"/>
<Button android:id="@+id/alpha_plus"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="透明度增加"
android:layout_gravity
="center_horizontal"/>
<Button android:id="@+id/alpha_minus"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="透明度减少"
android:layout_gravity
="center_horizontal"/>
<Button android:id="@+id/next"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="下一张"
android:layout_gravity
="center_horizontal"/>
</LinearLayout>
</LinearLayout>

ImageViewActivity类

package com.ljq.iv;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class ImageViewActivity extends Activity {
private ImageView imageView=null;
private Button previous=null;//上一张
private Button next=null;//下一张
private Button alpha_plus=null;//透明度增加
private Button alpha_minus=null;//透明度减少
private int currentImgId=0;//记录当前ImageView显示的图片id
private int alpha=255;//记录ImageView的透明度
int [] imgId = { //ImageView显示的图片数组
R.drawable.p1,
R.drawable.p2,
R.drawable.p3,
R.drawable.p4,
R.drawable.p5,
R.drawable.p6,
R.drawable.p7,
R.drawable.p8,
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

imageView
=(ImageView)findViewById(R.id.imageView);
previous
=(Button)findViewById(R.id.previous);
next
=(Button)findViewById(R.id.next);
alpha_plus
=(Button)findViewById(R.id.alpha_plus);
alpha_minus
=(Button)findViewById(R.id.alpha_minus);

previous.setOnClickListener(listener);
next.setOnClickListener(listener);
alpha_plus.setOnClickListener(listener);
alpha_minus.setOnClickListener(listener);
}

private View.OnClickListener listener = new View.OnClickListener(){

public void onClick(View v) {
if(v==previous){
currentImgId
=(currentImgId-1+imgId.length)%imgId.length;
imageView.setImageResource(imgId[currentImgId]);
}
if(v==next){
currentImgId
=(currentImgId+1)%imgId.length;
imageView.setImageResource(imgId[currentImgId]);
}
if(v==alpha_plus){
alpha
+=10;
if(alpha>255){
alpha
=255;
}
imageView.setAlpha(alpha);
}
if(v==alpha_minus){
alpha
-=10;
if(alpha<0){
alpha
=0;
}
imageView.setAlpha(alpha);
}
}

};

}

运行结果

2011022121280152.png

转载于:https://www.cnblogs.com/linjiqin/archive/2011/02/21/1960185.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pgtn.cn/news/17515.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

使用make_blob,KNeighborsClassifier-K近邻算法进行分类

写这篇博客源于博友的提问&#xff1a; 1. 效果图 输入 100 5 3 7 得到结果 [2] 可视化效果图如下&#xff1a;待预测点红色x展示&#xff0c; 输入 88 2 1 9 得到结果&#xff1a; [1] 可视化效果图如下&#xff1a;待预测点红色x展示&#xff0c; 2. 源码 # KNeighbo…

一生受益的三个小故事

转载于:https://www.cnblogs.com/88223100/archive/2011/02/22/three_stories.html

使用matplotlib绘制定制化饼图(图例比例标签支持中文等)

写这篇博客源于博友的提问 1. 效果图 效果图如下&#xff1a; 2. 原理 autopct‘%0.1f%%’ 自动添加百分比显示&#xff0c;格式化保留1位小数labeldistance 设置各扇形标签&#xff08;图例&#xff09;与圆心的距离&#xff08;labeldistance&#xff09;为1.1shadowTrue…

Python字母数字下划线生成田字格随机密码

写这篇博客源于博友的提问1&#xff0c;提问2 1. 效果图 10行随机密码&#xff0c;首字母不同&#xff0c;效果图如下&#xff1a; 田字格随机字符串如下&#xff1a; 2. 源码 # 生成随机密码 import randomimport numpy as np# 1. 生成随机密码,密码首字母不同 np.rando…

NHibernate从入门到精通系列(7)——多对一关联映射

内容摘要 多对一关联映射概括 多对一关联映射插入和查询 多对一关联映配置介绍 一、多对一关联映射概括 关联关系是实体类与实体类之间的结构关系&#xff0c;分别为“多对一”、“一对一”、“多对多”。然而“多对一”是怎样描述的呢&#xff1f;让我们参考图1.1所示&#xf…

使用Python爬取信息403解决,并统计汇总绘制直方图,柱状图,折线图

使用Python爬取信息403解决&#xff0c;并统计汇总绘制直方图&#xff0c;柱状图&#xff0c;折线图 写这篇博客源于博友的提问&#xff1a; 1. 效果图 拟录取专业-人数分布直方图效果图如下&#xff1a; 拟录取专业-人数效果图如下&#xff1a; 拟录取专业-人数柱状图…

使用Python对图像进行不同级别量化QP,使用RLE计算压缩比,并计算对应的PSNR

写这篇博客源于 博友的提问&#xff1a; 1.效果图 原图 VS QP2 VS QP4 VS QP8效果图如下&#xff1a; QP量化是指把原始图像按像素级别划分取值。如QP2&#xff0c;则<128 取0&#xff0c;>128取128. QP4&#xff0c;则<64取0&#xff0c;<128取64&#xff0c;&…

使用Python,SMTP发邮件到qq邮箱(文本/超链接/图片/表格/附件表格)

使用Python&#xff0c;SMTP发邮件到qq邮箱&#xff08;文本/超链接/图片/表格/附件表格&#xff09; 这篇博客将介绍如何利用SMTP&#xff0c;使用Python向qq邮箱自动发邮件&#xff0c;包括正文文本超链接图片表格&#xff0c;附件图片表格&#xff0c;收件人可多个&#xf…