Android的ImageView介绍-android学习之旅(二十二)
ImageView简介
imageView继承于View,主要用于显示图片,凡是Drawable对象都可以用它显示。
ImageView直接派生了ImageButton和ZoomButton等组件。
ImageView的属性设置
android:sacleType的设置是用来设置图片的显示方式。
图片浏览器实例
package peng.liu.testview;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.ImageView;
public class MainActivity extends Activity implements View.OnClickListener{
private int[] images = new int[]{
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
};
private int currentImg = 2;
private int alpha = 0;
private Button plus,minus,next;
private ImageView image1,image2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
plus = (Button) findViewById(R.id.plus);
minus = (Button) findViewById(R.id.minus);
next = (Button) findViewById(R.id.next);
image1 = (ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
image1.setImageResource(images[++currentImg%images.length]);
}
});
plus.setOnClickListener(this);
minus.setOnClickListener(this);
image1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) image1.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
double scale = bitmap.getWidth()/320.0;
int x = (int) (motionEvent.getX()*scale);
int y = (int) (motionEvent.getY()*scale);
if (x+120>bitmap.getWidth()){
x = bitmap.getWidth()-120;
}
if (y+120>bitmap.getHeight()){
y = bitmap.getHeight()-120;
}
image2.setImageBitmap(Bitmap.createBitmap(bitmap,x,y,120,120));
image2.setAlpha(alpha);
return false;
}
});
}
@Override
public void onClick(View view) {
if (view == plus){
alpha+=20;
}
if (view == minus){
alpha-=20;
}
if (alpha>=255){
alpha = 255;
}
if (alpha <= 0){
alpha = 0;
}
}
}
布局代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/plus"
android:text="plus"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/minus"
android:text="minus"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/next"
android:text="next"/>
</LinearLayout>
<ImageView
android:layout_width="fill_parent"
android:layout_height="240dp"
android:src="@drawable/hello"
android:id="@+id/image1"/>
<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#f00"
android:id="@+id/image2"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
效果图
Android的ImageView介绍-android学习之旅(二十二)的更多相关文章
- Android广播接收器Broadcast Receiver-android学习之旅(十二)
首先继承BroadcastReceiver类,并在manifest中注册 public class MyReceiver extends BroadcastReceiver { public MyRe ...
- Android四大组件之一Service介绍-android学习之旅(十二)
基本概念: service是android四大组件之一,运行在后台执行耗时操作,并不提供用户界面.其他组件如acticity可以通过startService启动该组件,也可以通过bindService ...
- android布局Relative和gridLayout-android学习之旅(十六)
Relative布局简介 相对布局的组件是由兄弟组件和父组价决定的,因此这种布局被称为相对布局. 属性设置介绍 RelativeLayout.Layoutparam中只能设置为true和false的属 ...
- android布局##TableLayout和FrameLayout-android学习之旅(十五)
TableLayout 表格布局 tablelayout简介 表格布局有TableLayout代表,但是它的本质定义仍然是线性管理器.表格布局采用行和列来管理UI,但是不需要明确的定义多少行,多少列, ...
- Android使用局和数据实现天气项目-android学习之旅(十二)
1.首先注册聚合数据账号,下载相应的sdk 2.导入jar包和 so文件 配置Application,初始化sdk <application //自己新建的application类 androi ...
- Python学习之旅(十二)
Python基础知识(11):高级特性 一.分片(切片) 通过索引来获取一定范围内的元素 #字符串 s="Alice" s[0:4:2] 结果: 'Ai' #列表 l=[1,2,3 ...
- Android的Toast介绍-android学习之旅(三十六)
Toast简单介绍 Toast是一个很方便的消息提示框.会在桌面显示一个短暂的消息提示.有两个特点: 1.消息不会获得焦点. 2.过一段时间会自己主动消失. Toast的生成步骤 1.调用构造器或者静 ...
- Android的AdapterViewFlipper和Stackview-android学习之旅(三十)
AdapterViewFlipper简介 AdapterViewFlipper继承了AdapterViewAnimater.每次只能显示一个组件,用showPrevious()和showNext()来 ...
- Android之Gallery和Spinner-Android学习之旅(二十九)
Spinner简介 spinner是竖直方向展开一个列表供选择.和gallery都是继承了AbsSpinner,AbsSpinner继承了AdapterView,因此AdaptyerView的属性都可 ...
随机推荐
- 关于Miller-Rabbin的一点想法
在好久之后终于搞完了miller-rabbin素性测试,谈谈自己的理解 要判断的数设为 a, 主要思想就是运用费马小定理来搞,随机几个数x(x<=a-1),判断x^(a-1)=1(mod a)是 ...
- ChatGirl is an AI ChatBot based on TensorFlow Seq2Seq Model
Introduction [Under developing,it is not working well yet.But you can just train,and run it.] ChatGi ...
- Intellij IDEA自动编译问题
对IDEA的界面很有爱,但是感到他的项目启动速度太慢了.所以查了资料做了优化. 1:开启自动测试 File->setting->compiler 勾选上上面的, 2修改run/de ...
- ubuntu14.04拼音输入法问题的解决方法
14.04的自带的拼音输入法,打字的时候你会发现有很大的问题,打不出来一个完整的字. 解决方法: ibus-daemon -drx //重启ibus进程 在终端里输一次这个命令就ok了. 或者装个 ...
- 高效update方案
--方案1:如果有索引,先把索引删除后,再update,最后把索引重新创建一下因为索引对update影响很大. --方案2:1.create table newA as select id,name, ...
- JS的replace默认只替换第一个匹配项
1. JS的replace默认只替换第一个匹配项. 解决方法: 使用正则表达式进行匹配替换[ ①.replace(new RegExp(②,"g") ,③); ] ①:包含 ...
- A quike guide teaching you how to use matlab to read netCDF file and plot a figure
1. Preparation 2. A brief introduce to netCDF. 4 3. Data Structure. 4 3.1 Attrib ...
- ACM Wooden Stricks
有一堆n根木棍.每根棍子的长度和重量是预先知道的. 这些棒子将被木工机器逐一加工..它需要一些时间,称为安装时间,用于机器准备加工棒.设置时间与机器中的清洁操作和更换工具和形状相关联.木工机械的安装时 ...
- Redis从入门到精通:中级篇
原文链接:http://www.cnblogs.com/xrq730/p/8944539.html,转载请注明出处,谢谢 本文目录 上一篇文章以认识Redis为主,写了Redis系列的第一篇,现在开启 ...
- JAVA Eclipse使用Maven构建web项目详解(SSM框架)
tips: 启动项目后,welcome-file的链接即为测试用例 部署maven web项目 Eclipse使用Maven构建web项目详解 pom.xml添加webapp依赖: <depen ...