【Android】21.4 图片动画缩放示例
分类:C#、Android、VS2015;
创建日期:2016-03-21
一、简介
该例子演示如何动画缩放图片,实现类似“点击看大图”的效果。
二、示例
1、运行截图

2、设计步骤
(1)添加图片
在Resources/no-dpi文件夹下添加4张图片(2个缩略图,2个大图)。
(2)添加ch2104MyImageButton.cs
using Android.Content;
using Android.Widget;
using System.Drawing;
using Android.Graphics.Drawables;
using Android.Util;
using Android.Content.Res;
using Android.Graphics;
using Color = Android.Graphics.Color; namespace MyDemos.SrcDemos
{
/// <summary>
/// 演示drawable-nodpi文件夹下的图片资源缩放(点击看大图)
/// </summary>
public class ch2104MyImageButton : ImageButton
{
private Rectangle cachedBounds;
private Drawable foregroundDrawable; public ch2104MyImageButton(Context context)
: this(context, null)
{
} public ch2104MyImageButton(Context context, IAttributeSet attrs)
: this(context, attrs, 0)
{
} public ch2104MyImageButton(Context context, IAttributeSet attrs, int defStyle)
: base(context, attrs, defStyle)
{
Init();
} protected override void DrawableStateChanged()
{
base.DrawableStateChanged();
if (foregroundDrawable.IsStateful)
{
foregroundDrawable.SetState(GetDrawableState());
}
Invalidate();
} protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
base.OnDraw(canvas);
foregroundDrawable.SetBounds(cachedBounds.Left, cachedBounds.Top, cachedBounds.Right, cachedBounds.Bottom);
foregroundDrawable.Draw(canvas);
} protected override void OnSizeChanged(int w, int h, int oldw, int oldh)
{
base.OnSizeChanged(w, h, oldw, oldh);
cachedBounds = new Rectangle(0, 0, w, h);
} private void Init()
{
SetBackgroundColor(Color.White);
SetPadding(0, 0, 0, 0); TypedArray a = Context.ObtainStyledAttributes(new[] { Android.Resource.Attribute.SelectableItemBackground });
foregroundDrawable = a.GetDrawable(0);
foregroundDrawable.SetCallback(this);
a.Recycle();
}
}
}
(3)添加ch2104Zoom.axml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
style="?android:textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提示:单击缩略图【放大/缩小】该图片。" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="horizontal">
<MyDemos.SrcDemos.ch2104MyImageButton
android:id="@+id/thumb_button_1"
android:layout_width="100dp"
android:layout_height="75dp"
android:layout_marginRight="1dp"
android:src="@drawable/ch2103thumb1"
android:scaleType="centerCrop"
android:contentDescription="缩略图1" />
<MyDemos.SrcDemos.ch2104MyImageButton
android:id="@+id/thumb_button_2"
android:layout_width="100dp"
android:layout_height="75dp"
android:src="@drawable/ch2103thumb2"
android:scaleType="centerCrop"
android:contentDescription="缩略图2" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/expanded_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:contentDescription="这是放大后的图片(点击消失)" />
</FrameLayout>
(4)添加ch2104ZoomActivity.cs
using System;
using System.Collections.Generic;
using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using Android.Animation;
using Android.Graphics;
using Android.Views.Animations; namespace MyDemos.SrcDemos
{
[Activity(Label = "【例21-4】图片动画缩放示例")]
public class ch2104ZoomActivity : Activity
{
private Animator currentAnimator;
private int shortAnimationDuration;
private Dictionary<int, AnimatorSet> expandingAnimators;
private Dictionary<int, AnimatorSet> shrinkingAnimators; protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle); SetContentView(Resource.Layout.ch2104Zoom); shortAnimationDuration = Resources.GetInteger(Android.Resource.Integer.ConfigShortAnimTime);
expandingAnimators = new Dictionary<int, AnimatorSet>(2);
shrinkingAnimators = new Dictionary<int, AnimatorSet>(2); View thumb1View = FindViewById(Resource.Id.thumb_button_1);
thumb1View.Tag = Resource.Drawable.ch2103image1;
thumb1View.Click += ZoomImageFromThumb; View thumb2View = FindViewById(Resource.Id.thumb_button_2);
thumb2View.Tag = Resource.Drawable.ch2103image2;
thumb2View.Click += ZoomImageFromThumb; } /// <summary>
/// 控制ImageView的放大速率
/// </summary>
/// <param name="startBounds">缩略图的矩形区域</param>
/// <param name="finalBounds">大图的可见区域</param>
/// <returns></returns>
private static float CalculateStartScale(Rect startBounds, Rect finalBounds)
{
float startScale;
float finalBoundsRatio = finalBounds.Width() / (float)finalBounds.Height();
float startBoundsRatio = startBounds.Width() / (float)startBounds.Height(); if (finalBoundsRatio > startBoundsRatio)
{
// 横向展开
startScale = (float)startBounds.Height() / finalBounds.Height();
float startWidth = startScale * finalBounds.Width();
float deltaWidth = (startWidth - startBounds.Width()) / 2;
startBounds.Left -= (int)deltaWidth;
startBounds.Right += (int)deltaWidth;
}
else
{
// 纵向展开
startScale = (float)startBounds.Width() / finalBounds.Width();
float startHeight = startScale * finalBounds.Height();
float deltaHeight = (startHeight - startBounds.Height()) / 2;
startBounds.Top -= (int)deltaHeight;
startBounds.Bottom += (int)deltaHeight;
}
return startScale;
} /// <summary>
/// 创建展开的动画集合 - 让缩略图看起来逐渐变大
/// </summary>
/// <param name="expandedView">缩略图放大用的ImageView</param>
/// <param name="startBounds">缩略图的可见区域(全局坐标)</param>
/// <param name="finalBounds">放大后的矩形区域(全局坐标)</param>
/// <param name="startScale"></param>
/// <returns></returns>
private AnimatorSet BuildExpandingAnimatorSet(ImageView expandedView, Rect startBounds, Rect finalBounds, float startScale)
{
// 按顺序缓存每次展开的动画集合,这些实例都是从初始位置开始
int key = startBounds.GetHashCode();
if (expandingAnimators.ContainsKey(key))
{
return expandingAnimators[key];
} AnimatorSet expandSet = new AnimatorSet();
expandSet.Play(ObjectAnimator.OfFloat(expandedView, View.X, startBounds.Left, finalBounds.Left))
.With(ObjectAnimator.OfFloat(expandedView, View.Y, startBounds.Top, finalBounds.Top))
.With(ObjectAnimator.OfFloat(expandedView, "ScaleX", startScale, 1f))
.With(ObjectAnimator.OfFloat(expandedView, "ScaleY", startScale, 1f));
expandSet.SetDuration(shortAnimationDuration);
expandSet.SetInterpolator(new DecelerateInterpolator());
expandSet.AnimationEnd += NullOutCurrentAnimator;
expandSet.AnimationCancel += NullOutCurrentAnimator; expandingAnimators.Add(key, expandSet);
return expandSet;
} private void NullOutCurrentAnimator(object sender, EventArgs eventArgs)
{
if (currentAnimator == null)
{
return;
}
currentAnimator = null;
} /// <summary>
/// 创建从大图逐步缩小到缩略图的动画集合
/// </summary>
/// <param name="bigView">大图的视图</param>
/// <param name="thumbView">缩略图的视图</param>
/// <param name="startBounds">缩略图变为可见的区域</param>
/// <param name="scale">缩放速率</param>
/// <returns></returns>
private AnimatorSet BuildShrinkingAnimatorSet(View bigView, View thumbView, Rect startBounds, float scale)
{
if (shrinkingAnimators.ContainsKey(thumbView.Id))
{
return shrinkingAnimators[thumbView.Id];
} AnimatorSet shrinkSet = new AnimatorSet();
shrinkSet.Play(ObjectAnimator.OfFloat(bigView, View.X, startBounds.Left))
.With(ObjectAnimator.OfFloat(bigView, View.Y, startBounds.Top))
.With(ObjectAnimator.OfFloat(bigView, "ScaleX", scale))
.With(ObjectAnimator.OfFloat(bigView, "ScaleY", scale));
shrinkSet.SetDuration(shortAnimationDuration);
shrinkSet.SetInterpolator(new DecelerateInterpolator());
shrinkSet.AnimationEnd += (sender1, args1) =>
{
thumbView.Alpha = 1.0f;
bigView.Visibility = ViewStates.Gone;
NullOutCurrentAnimator(sender1, args1);
}; shrinkSet.AnimationCancel += (sender1, args1) =>
{
thumbView.Alpha = 1.0f;
bigView.Visibility = ViewStates.Gone;
NullOutCurrentAnimator(sender1, args1);
}; shrinkingAnimators.Add(thumbView.Id, shrinkSet);
return shrinkSet;
} /// <summary>
/// 获取控制展开图片的ImageView的引用
/// </summary>
/// <param name="thumbView"></param>
/// <returns></returns>
private ImageView GetExpandedImageView(View thumbView)
{
ImageView expandedImageView = FindViewById<ImageView>(Resource.Id.expanded_image);
int finalImageResourceId = (int)thumbView.Tag; // In this example we store the resource id of the big image in the tag of the thumbnail.
expandedImageView.SetImageResource(finalImageResourceId);
thumbView.Alpha = 0.2f; //0f;
expandedImageView.Visibility = ViewStates.Visible;
expandedImageView.PivotX = 0f;
expandedImageView.PivotY = 0f;
return expandedImageView;
} private void ZoomImageFromThumb(object sender, EventArgs eventArgs)
{
View thumbView = (View)sender;
ImageView expandedImageView = GetExpandedImageView(thumbView); if (currentAnimator != null)
{
currentAnimator.Cancel();
}
Rect startBounds = new Rect();
Rect finalBounds = new Rect();
Point globalOffset = new Point(); thumbView.GetGlobalVisibleRect(startBounds); FindViewById(Resource.Id.container).GetGlobalVisibleRect(finalBounds, globalOffset);
startBounds.Offset(-globalOffset.X, -globalOffset.Y);
finalBounds.Offset(-globalOffset.X, -globalOffset.Y); float startScale = CalculateStartScale(startBounds, finalBounds); AnimatorSet expandSet = BuildExpandingAnimatorSet(expandedImageView, startBounds, finalBounds, startScale);
expandSet.Start();
currentAnimator = expandSet; expandedImageView.Click += (o, args) =>
{
if (currentAnimator != null)
{
currentAnimator.Cancel();
} AnimatorSet shrinkSet = BuildShrinkingAnimatorSet(expandedImageView, thumbView, startBounds, startScale);
shrinkSet.Start();
currentAnimator = shrinkSet;
};
}
}
}
【Android】21.4 图片动画缩放示例的更多相关文章
- Android实现对图片的缩放、剪切、旋转、存储
转载:http://www.cnblogs.com/jerehedu/p/4464870.html 一.问题描述: 在开发中,当我们需要的有一张大图片同时还需要一些小图片时,我们只需要通过代码对此图片 ...
- Android 图片的缩放与旋转
本文实现Android中的图片的缩放效果 首先设计布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res ...
- 解决android:background背景图片被拉伸问题
ImageView中XML属性src和background的区别: background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸.src是图片内容(前 ...
- Android动画及图片的缩放和旋转
Android动画有2种,一种是Tween Animation,另一种是Frame Animation,先说说Tween动画吧. Tween动画是对视图对象中的内容进行一系列简单的转换,比如位置的移动 ...
- Android仿微信图片上传,可以选择多张图片,缩放预览,拍照上传等
仿照微信,朋友圈分享图片功能 .可以进行图片的多张选择,拍照添加图片,以及进行图片的预览,预览时可以进行缩放,并且可以删除选中状态的图片 .很不错的源码,大家有需要可以下载看看 . 微信 微信 微信 ...
- Android实战简易教程-第九枪(BitmapFactory.Options对资源图片进行缩放)
我们知道,我们编写的应用程序都是有一定内存限制的.程序占用了过高的内存就easy出现OOM(OutOfMemory)异常.因此在展示高分辨率图片的时候,最好先将图片进行压缩,压缩后的图片大小应该和用来 ...
- Android多点触控(图片的缩放Demo)
本文主要介绍Android的多点触控,使用了一个图片缩放的实例,来更好的说明其原理.须要实现OnTouchListener接口,重写当中的onTouch方法. 实现效果图: 源码: 布局文 ...
- Android图片采样缩放
为什么要对Android中的图片进行采样缩放呢? 是为了更加高效的加载Bitmap.假设通过imageView来显示图片,很多时候ImageView并没有图片的原始尺寸那么大,这时候把整张图片加载进来 ...
- Android 旋转、平移、缩放和透明度渐变的补间动画
补间动画就是通过对场景里的对象不断进行图像变化来产生动画效果.在实现补间动画时,只需要定义开始和结束的“关键帧”,其他过渡帧由系统自动计算并补齐.在Android中,提供了以下4种补间动画. **1. ...
随机推荐
- nodejs检查已安装模块
命令行 npm ls --depth 0
- php之表单-2(表单验证)
PHP 表单验证 本章节我们将介绍如何使用PHP验证客户端提交的表单数据. PHP 表单验证 在处理PHP表单时我们需要考虑安全性. 本章节我们将展示PHP表单数据安全处理,为了防止黑客及垃圾信息我们 ...
- HTTP请求响应码
① 客户方错误 100 继续 101 交换协议 ② 成功 200 OK 201 已创建 202 接收 203 非认证信息 204 无内容 205 重置内容 206 部分内容 ③ 重定向 300 多路选 ...
- 解决sqlplus: command not found
export ORACLE_BASE=/u01/app/oracle export ORACLE_HOME=$ORACLE_BASE/product//db_1 su - root ln -s $OR ...
- My Magic Android Tour —— 处女作
近期考试什么的都已经结束了,闲在家也没什么事做,就想着学点什么,于是便选择了学一下Android开发.一直在使用Android手机.要是自己能为自己的手机开发一个APP就好了. 好了,也不扯些废话了, ...
- COCOS学习笔记--粒子系统
一.粒子系统的简单介绍 粒子系统是指计算机图形学中模拟特定现象的技术,它在模仿自然现象.物理现象及空间扭曲上具备得天独厚的优势,为我们实现一些真实自然而又带有随机性的特效(如爆炸.烟花.水流)提供了方 ...
- te
var option = {}; $(function() { /* var taskId = ${pd.taskId}; */ var taskId = "1470880530369&qu ...
- HDUOJ--点球大战
点球大战 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- 两个有序数组求中位数log(m+n)复杂度
leetcode 第4题 中位数技巧: 对于长度为L的有序数组,它的中位数是(a[ceil((L+1)/2)]+a[floor((L+1)/2)])/2 算法原理: 类似三分法求极值 两个人都前进,谁 ...
- Mybatis中的@SelectKey注解
一.创建Maven项目 在pom.xml中,添加mybatis依赖,mysql-jdbc依赖,把编译版本改为1.8 你问,为啥mybatis不会自动依赖mysql-jdbc,需要手动写明?答:因为my ...