您好,登錄后才能下訂單哦!
本文實例為大家分享了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; } }); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。