Android实现点击缩略图放大效果
本文实例为大家分享了Android点击缩略图放大效果的具体代码,供大家参考,具体内容如下
importandroid.animation.Animator;
importandroid.animation.AnimatorListenerAdapter;
importandroid.animation.AnimatorSet;
importandroid.animation.ObjectAnimator;
importandroid.graphics.Point;
importandroid.graphics.Rect;
importandroid.os.Bundle;
importandroid.support.v7.app.AppCompatActivity;
importandroid.view.View;
importandroid.view.animation.DecelerateInterpolator;
importandroid.widget.ImageView;
publicclassMainActivityextendsAppCompatActivity{
//持有这个动画的引用,让他可以在动画执行中途取消
privateAnimatormCurrentAnimator;
privateintmShortAnimationDuration;
privateViewimageView1;
privateViewimageView2;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
imageView1.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
zoomImageFromThumb(imageView1,R.mipmap.ic_launcher);
}
});
imageView2.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
zoomImageFromThumb(imageView2,R.mipmap.ic_launcher);
}
});
//系统默认的短动画执行时间200
mShortAnimationDuration=getResources().getInteger(
android.R.integer.config_shortAnimTime);
}
privatevoidinitView(){
imageView1=(ImageView)findViewById(R.id.imageView1);
imageView2=(ImageView)findViewById(R.id.imageView2);
}
privatevoidzoomImageFromThumb(finalViewthumbView,intimageResId){
//如果有动画正在运行,取消这个动画
if(mCurrentAnimator!=null){
mCurrentAnimator.cancel();
}
//加载显示大图的ImageView
finalImageViewexpandedImageView=(ImageView)findViewById(
R.id.expanded_image);
expandedImageView.setImageResource(imageResId);
//计算初始小图的边界位置和最终大图的边界位置。
finalRectstartBounds=newRect();
finalRectfinalBounds=newRect();
finalPointglobalOffset=newPoint();
//小图的边界就是小ImageView的边界,大图的边界因为是铺满全屏的,所以就是整个布局的边界。
//然后根据偏移量得到正确的坐标。
thumbView.getGlobalVisibleRect(startBounds);
findViewById(R.id.imageView1).getGlobalVisibleRect(finalBounds,globalOffset);
startBounds.offset(-globalOffset.x,-globalOffset.y);
finalBounds.offset(-globalOffset.x,-globalOffset.y);
//计算初始的缩放比例。最终的缩放比例为1。并调整缩放方向,使看着协调。
floatstartScale=0;
if((float)finalBounds.width()/finalBounds.height()
>(float)startBounds.width()/startBounds.height()){
//横向缩放
floatstartWidth=startScale*finalBounds.width();
floatdeltaWidth=(startWidth-startBounds.width())/2;
startBounds.left-=deltaWidth;
startBounds.right+=deltaWidth;
}else{
//竖向缩放
floatstartHeight=startScale*finalBounds.height();
floatdeltaHeight=(startHeight-startBounds.height())/2;
startBounds.top-=deltaHeight;
startBounds.bottom+=deltaHeight;
}
//隐藏小图,并显示大图
thumbView.setAlpha(0f);
expandedImageView.setVisibility(View.VISIBLE);
//将大图的缩放中心点移到左上角。默认是从中心缩放
expandedImageView.setPivotX(0f);
expandedImageView.setPivotY(0f);
//对大图进行缩放动画
AnimatorSetset=newAnimatorSet();
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(newDecelerateInterpolator());
set.addListener(newAnimatorListenerAdapter(){
@Override
publicvoidonAnimationEnd(Animatoranimation){
mCurrentAnimator=null;
}
@Override
publicvoidonAnimationCancel(Animatoranimation){
mCurrentAnimator=null;
}
});
set.start();
mCurrentAnimator=set;
//点击大图时,反向缩放大图,然后隐藏大图,显示小图。
finalfloatstartScaleFinal=startScale;
expandedImageView.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewview){
if(mCurrentAnimator!=null){
mCurrentAnimator.cancel();
}
AnimatorSetset=newAnimatorSet();
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(newDecelerateInterpolator());
set.addListener(newAnimatorListenerAdapter(){
@Override
publicvoidonAnimationEnd(Animatoranimation){
thumbView.setAlpha(1f);
expandedImageView.setVisibility(View.GONE);
mCurrentAnimator=null;
}
@Override
publicvoidonAnimationCancel(Animatoranimation){
thumbView.setAlpha(1f);
expandedImageView.setVisibility(View.GONE);
mCurrentAnimator=null;
}
});
set.start();
mCurrentAnimator=set;
}
});
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。