本文实例为大家分享了AndroID点击缩略图放大效果的具体代码,供大家参考,具体内容如下
import androID.animation.Animator;import androID.animation.AnimatorListenerAdapter;import androID.animation.AnimatorSet;import androID.animation.ObjectAnimator;import androID.graphics.Point;import androID.graphics.Rect;import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;import androID.vIEw.VIEw;import androID.vIEw.animation.DecelerateInterpolator;import androID.Widget.ImageVIEw;public class MainActivity extends AppCompatActivity { // 持有这个动画的引用,让他可以在动画执行中途取消 private Animator mCurrentAnimator; private int mShortAnimationDuration; private VIEw imageVIEw1; private VIEw imageVIEw2; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); initVIEw(); imageVIEw1.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { zoomImageFromThumb(imageVIEw1,R.mipmap.ic_launcher); } }); imageVIEw2.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { zoomImageFromThumb(imageVIEw2,R.mipmap.ic_launcher); } }); // 系统默认的短动画执行时间 200 mShortAnimationDuration = getResources().getInteger( androID.R.integer.config_shortAnimTime); } private voID initVIEw() { imageVIEw1 = (ImageVIEw) findVIEwByID(R.ID.imageVIEw1); imageVIEw2 = (ImageVIEw) findVIEwByID(R.ID.imageVIEw2); } private voID zoomImageFromThumb(final VIEw thumbVIEw,int imageResID) { // 如果有动画正在运行,取消这个动画 if (mCurrentAnimator != null) { mCurrentAnimator.cancel(); } // 加载显示大图的ImageVIEw final ImageVIEw expandedImageVIEw = (ImageVIEw) findVIEwByID( R.ID.expanded_image); expandedImageVIEw.setimageResource(imageResID); // 计算初始小图的边界位置和最终大图的边界位置。 final Rect startBounds = new Rect(); final Rect finalBounds = new Rect(); final Point globalOffset = new Point(); // 小图的边界就是小ImageVIEw的边界,大图的边界因为是铺满全屏的,所以就是整个布局的边界。 // 然后根据偏移量得到正确的坐标。 thumbVIEw.getGlobalVisibleRect(startBounds); findVIEwByID(R.ID.imageVIEw1).getGlobalVisibleRect(finalBounds,globalOffset); startBounds.offset(-globalOffset.x,-globalOffset.y); finalBounds.offset(-globalOffset.x,-globalOffset.y); // 计算初始的缩放比例。最终的缩放比例为1。并调整缩放方向,使看着协调。 float startScale=0; if ((float) finalBounds.wIDth() / finalBounds.height() > (float) startBounds.wIDth() / startBounds.height()) { // 横向缩放 float startWIDth = startScale * finalBounds.wIDth(); float deltaWIDth = (startWIDth - startBounds.wIDth()) / 2; startBounds.left -= deltaWIDth; startBounds.right += deltaWIDth; } else { // 竖向缩放 float startHeight = startScale * finalBounds.height(); float deltaHeight = (startHeight - startBounds.height()) / 2; startBounds.top -= deltaHeight; startBounds.bottom += deltaHeight; } // 隐藏小图,并显示大图 thumbVIEw.setAlpha(0f); expandedImageVIEw.setVisibility(VIEw.VISIBLE); // 将大图的缩放中心点移到左上角。默认是从中心缩放 expandedImageVIEw.setPivotX(0f); expandedImageVIEw.setPivotY(0f); //对大图进行缩放动画 AnimatorSet set = new AnimatorSet(); set.play(ObjectAnimator.offloat(expandedImageVIEw,VIEw.X,startBounds.left,finalBounds.left)) .with(ObjectAnimator.offloat(expandedImageVIEw,VIEw.Y,startBounds.top,finalBounds.top)) .with(ObjectAnimator.offloat(expandedImageVIEw,VIEw.SCALE_X,startScale,1f)) .with(ObjectAnimator.offloat(expandedImageVIEw,VIEw.SCALE_Y,1f)); set.setDuration(mShortAnimationDuration); set.setInterpolator(new DecelerateInterpolator()); set.addListener(new AnimatorListenerAdapter() { @OverrIDe public voID onAnimationEnd(Animator animation) { mCurrentAnimator = null; } @OverrIDe public voID onAnimationCancel(Animator animation) { mCurrentAnimator = null; } }); set.start(); mCurrentAnimator = set; // 点击大图时,反向缩放大图,然后隐藏大图,显示小图。 final float startScaleFinal = startScale; expandedImageVIEw.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { if (mCurrentAnimator != null) { mCurrentAnimator.cancel(); } AnimatorSet set = new AnimatorSet(); set.play(ObjectAnimator .offloat(expandedImageVIEw,startBounds.left)) .with(ObjectAnimator .offloat(expandedImageVIEw,startBounds.top)) .with(ObjectAnimator .offloat(expandedImageVIEw,startScaleFinal)) .with(ObjectAnimator .offloat(expandedImageVIEw,startScaleFinal)); set.setDuration(mShortAnimationDuration); set.setInterpolator(new DecelerateInterpolator()); set.addListener(new AnimatorListenerAdapter() { @OverrIDe public voID onAnimationEnd(Animator animation) { thumbVIEw.setAlpha(1f); expandedImageVIEw.setVisibility(VIEw.GONE); mCurrentAnimator = null; } @OverrIDe public voID onAnimationCancel(Animator animation) { thumbVIEw.setAlpha(1f); expandedImageVIEw.setVisibility(VIEw.GONE); mCurrentAnimator = null; } }); set.start(); mCurrentAnimator = set; } }); }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android实现点击缩略图放大效果全部内容,希望文章能够帮你解决Android实现点击缩略图放大效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)