本文實(shí)例為大家分享了Android點(diǎn)擊縮略圖放大效果的具體代碼,供大家參考,具體內(nèi)容如下
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:主機(jī)域名、雅安服務(wù)器托管、營銷軟件、網(wǎng)站建設(shè)、魯?shù)榫W(wǎng)站維護(hù)、網(wǎng)站推廣。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 { // 持有這個動畫的引用,讓他可以在動畫執(zhí)行中途取消 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); } }); // 系統(tǒng)默認(rèn)的短動畫執(zhí)行時間 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) { // 如果有動畫正在運(yùn)行,取消這個動畫 if (mCurrentAnimator != null) { mCurrentAnimator.cancel(); } // 加載顯示大圖的ImageView final ImageView expandedImageView = (ImageView) findViewById( R.id.expanded_image); expandedImageView.setImageResource(imageResId); // 計(jì)算初始小圖的邊界位置和最終大圖的邊界位置。 final Rect startBounds = new Rect(); final Rect finalBounds = new Rect(); final Point globalOffset = new Point(); // 小圖的邊界就是小ImageView的邊界,大圖的邊界因?yàn)槭卿仢M全屏的,所以就是整個布局的邊界。 // 然后根據(jù)偏移量得到正確的坐標(biāo)。 thumbView.getGlobalVisibleRect(startBounds); findViewById(R.id.imageView1).getGlobalVisibleRect(finalBounds, globalOffset); startBounds.offset(-globalOffset.x, -globalOffset.y); finalBounds.offset(-globalOffset.x, -globalOffset.y); // 計(jì)算初始的縮放比例。最終的縮放比例為1。并調(diào)整縮放方向,使看著協(xié)調(diào)。 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); // 將大圖的縮放中心點(diǎn)移到左上角。默認(rèn)是從中心縮放 expandedImageView.setPivotX(0f); expandedImageView.setPivotY(0f); //對大圖進(jìn)行縮放動畫 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; // 點(diǎn)擊大圖時,反向縮放大圖,然后隱藏大圖,顯示小圖。 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; } }); } }
網(wǎng)頁題目:Android實(shí)現(xiàn)點(diǎn)擊縮略圖放大效果-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://jinyejixie.com/article40/ddeceo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、軟件開發(fā)、微信小程序、搜索引擎優(yōu)化、網(wǎng)站設(shè)計(jì)、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容