欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现点击缩略图放大效果

程序员文章站 2024-02-12 15:03:46
本文实例为大家分享了android点击缩略图放大效果的具体代码,供大家参考,具体内容如下 import android.animation.animator;...

本文实例为大家分享了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, startscale, 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, view.x, startbounds.left))
            .with(objectanimator
                .offloat(expandedimageview,
                    view.y,startbounds.top))
            .with(objectanimator
                .offloat(expandedimageview,
                    view.scale_x, startscalefinal))
            .with(objectanimator
                .offloat(expandedimageview,
                    view.scale_y, 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;
      }
    });
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。