参考自《疯狂android讲义》2.4节

效果如下:

当点击图上某点时,将之附近放大至下图。

布局文件:

<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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <Button
android:id="@+id/bt_plus_alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/plus_alpha"
android:textSize="12sp" /> <Button
android:id="@+id/bt_minus_alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/minus_alpha"
android:textSize="12sp" /> <Button
android:id="@+id/bt_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next_picture"
android:textSize="12sp" />
</LinearLayout> <ImageView
android:id="@+id/iv_full_pic"
android:layout_width="match_parent"
android:layout_height="0sp"
android:layout_weight="3"
android:src="@drawable/shuangta"
android:contentDescription="@string/big_image"/> <ImageView
android:id="@+id/iv_zoom_pic"
android:layout_width="match_parent"
android:layout_height="0sp"
android:layout_weight="1"
android:src="@drawable/shuangta"
android:contentDescription="@string/small_image"/> </LinearLayout>

类文件:

package com.ljh.imageviewdemo;

import com.example.imageviewdemo.R;

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;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable; public class MainActivity extends Activity {
private float alpha = 1.0f; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); final ImageView ivFullPic = (ImageView) findViewById(R.id.iv_full_pic);
final ImageView ivZoomPic = (ImageView) findViewById(R.id.iv_zoom_pic);
final Button btIncreaseAlpha = (Button) findViewById(R.id.bt_plus_alpha);
final Button btDecreaseAlpha = (Button) findViewById(R.id.bt_minus_alpha);
final Button btNextPic = (Button) findViewById(R.id.bt_next); final int[] images = new int[] { R.drawable.lijiang, R.drawable.qiao,
R.drawable.shuangta, R.drawable.shui, R.drawable.xiangbi }; btNextPic.setOnClickListener(new OnClickListener() {
private int currentImage = 2; @Override
public void onClick(View v) {
currentImage++;
ivFullPic
.setImageResource(images[currentImage % images.length]);
}
}); btIncreaseAlpha.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
if (alpha > 1) {
//在API11以后,建议使用setAlpha(float),而setAlpha(int) 已经 deprecated。前者取值范围0~1,后者取值范围0~255.
ivFullPic.setAlpha(1.0f);
} else
ivFullPic.setAlpha(alpha += 0.01);
} }); btDecreaseAlpha.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
if (alpha < 0) {
ivFullPic.setAlpha(0.0f);
} else
ivFullPic.setAlpha(alpha -= 0.01);
} }); ivFullPic.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View view, MotionEvent event)
{
BitmapDrawable bitmapDrawable = (BitmapDrawable) ivFullPic
.getDrawable();
// 获取第一个图片显示框中的位图
Bitmap bitmap = bitmapDrawable.getBitmap();
// bitmap图片实际大小与第一个ImageView的缩放比例
double scale = bitmap.getWidth() / 480.0;
// 获取需要显示的图片的开始点
int x = (int) (event.getX() * scale);
int y = (int) (event.getY() * scale);
if (x + 120 > bitmap.getWidth())
{
x = bitmap.getWidth() - 120;
}
if (y + 120 > bitmap.getHeight())
{
y = bitmap.getHeight() - 120;
}
// 显示图片的指定区域
ivZoomPic.setImageBitmap(Bitmap.createBitmap(bitmap
, x, y, 120, 120));
ivZoomPic.setAlpha(alpha);
return false;
}
});
} }

几个知识点:

1、根据比例调整图像大小

android:layout_height="0sp"

        android:layout_weight="3"

android:layout_height="0sp"

        android:layout_weight="1"

2、注意典型的用匿名内部类作监听器的做法。

3、setAlpha(float)与setAlpha(int)的区别。

4、setImageResource(int)

ImageView一例的更多相关文章

  1. ImageView一例 分类: H1_ANDROID 2013-10-30 23:02 1812人阅读 评论(0) 收藏

    参考自<疯狂android讲义>2.4节 效果如下: 当点击图上某点时,将之附近放大至下图. 布局文件: <LinearLayout xmlns:android="http ...

  2. Button、ImageButton及ImageView详解

    Button.ImageButton及ImageView详解 在应用程序开发过程中,很多时候需要将View的background或者src属性设置为图片,即美观又支持点击等操作.常见的有Button. ...

  3. 一种简单的实现:Android一键换肤功能

    现在的APP开发,通常会提供APP的换肤功能,网上流传的换肤代码和实现手段过于复杂,我把原作者的代码重新整理抽取出来,转换成Eclipse项目,重新整理成正确.可直接运行的项目. 代码运行结果如图. ...

  4. UIMenuController搭配UIPasteboard,执行拷贝-黏贴操作-b

    一.基本概念 UIKit框架中,可以直接执行拷贝黏贴操作的有:UITextView.UITextField和UIWebView,其他控件需要实现相关方法. 关于UIPasteboard ·黏贴板是ap ...

  5. 【腾讯Bugly干货分享】Android内存优化总结&实践

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/2MsEAR9pQfMr1Sfs7cPdWQ 导语 智 ...

  6. LeakCanary 内存泄漏 监测 性能优化 简介 原理 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  7. Android 自定义可拖拽View,界面渲染刷新后不会自动回到起始位置

    以自定义ImageView为例: /** * 可拖拽ImageView * Created by admin on 2017/2/21. */ public class FloatingImageVi ...

  8. android 获取屏幕的高度和宽度、获取控件在屏幕中的位置、获取屏幕中控件的高度和宽度

    (一)获取屏幕的高度和宽度 有两种方法: 方法1: WindowManager wm = (WindowManager) getContext().getSystemService(Context.W ...

  9. Android一键换肤功能:一种简单的实现

     Android一键换肤功能:一种简单的实现 现在的APP开发,通常会提供APP的换肤功能,网上流传的换肤代码和实现手段过于复杂,这里有一个开源实现,我找了一大堆,发现这个项目相对较为简洁:htt ...

随机推荐

  1. 混入模式(max-in)实现继承

    混入模式并不是一种复制完整的对象,而是从多个对象中复制出任意的成员并将这些成员组合成一个新的对象. 实现如下: function mix(){ var arg,prop,child = {}; for ...

  2. HTML中元素水平居中。

    一丶margin:0 auto; 试用最多的方法,简单实用. 二丶vertical-align:middle; 只适用于内嵌元素,比如说一个div中有一个图片和文字,要让图片和文字中线对齐. < ...

  3. 列表:一个打了激素的数组 - 零基础入门学习Python010

    列表:一个打了激素的数组 让编程改变世界 Change the world by program 列表:一个打了激素的数组 有时候我们需要把一堆东西暂时存储起来,因为他们有某种直接或者间接的联系,我们 ...

  4. 异步编程设计模式Demo - AsyncComponentSample

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...

  5. [UVA] 784 - Maze Exploration

      Maze Exploration  A maze of rectangular rooms is represented on a two dimensional grid as illustra ...

  6. 简单的使用php多线程抓取网页

    PHP 利用 Curl Functions 可以完成各种传送文件操作,比如模拟浏览器发送GET,POST请求等等,受限于php语言本身不支持多线程,所以开发爬虫程序效率并不高,这时候往往需 要借助Cu ...

  7. 转:Asp.Net MVC中DropDownListFor的用法

    在Asp.Net MVC中可以用DropDownListFor的方式来让用户选择已定列表中的一个数值.用法不复杂,这里简单做一个记录. 首先我们要定义一个 Model ,用户在 DropDownLis ...

  8. UVA12186--树型DP

    树型DP第一题...就是从boss到底层员工是一个树型结构,底层员工想加薪,如果每个boss都有超过T%的员工要求加薪,他就会往更高的bOSs传达,问如果让根节点的大boss接到加薪要求,最少要有多少 ...

  9. c语言所有的errno枚举值含义

    可以通过以下代码,获取所有的错误码信息: #include <string.h> /* for strerror */ #include <errno.h> #include ...

  10. Jndi and c3p0 in Tomcat

    Tomcat 中Jndi是使用Tomcat自带的连接池抛弃Tomcat自带的连接池.使用c3p0 . 环境:Tomcat 5.5.20下面配置只适合Tomcat 5.5.X 下面来看Jndi 与 c3 ...