像音樂播放App一樣移動背景

來源:互聯網
上載者:User

標籤:rup   duration   date   imageview   asc   map   rip   demo   ret   

假設你常常聽歌。你會發現歌曲app的背景會隨著音樂移動的,從左至右或者從上到下。這樣的動畫儘管簡單,可是這裡有一個技巧。

假設你還不明確這樣的動效看看以下的demo

(很多其它具體請參考:https://github.com/flavienlaurent/PanningView)
一,使用setImageMatrix播放圖片動畫
以下是官方文檔給出的解釋

你能夠看到這裡的解釋非常easy。就是取代ImageView的映像矩陣。然後configureBounds和invalidate被調用。
在java代碼中我們能夠設定Matrix的scaleType

mImageView.setScaleType(ScaleType.MATRIX)

或者在xml檔案裡設定

android:scaleType="matrix"

以下是ImageView的初始矩陣(matrix)

在x和y方向上放大2倍

final Matrix matrix = new Matrix();matrix.postScale(2, 2);imageView.setImageMatrix(matrix);

final Matrix matrix = new Matrix();matrix.postScale(2, 2);matrix.postRotate(15);imageView.setImageMatrix(matrix);


二,使你的圖片移動
首先我們須要計算ImageView當前方向(水平。縱向)和圖片當前方向的比例,就比方水平方向吧,我們就要讓圖片和view的高度同樣,橫向放大或者縮小。

float scaleFactor = (float)imageView.getHeight() / (float) drawable.getIntrinsicHeight();mMatrix.postScale(scaleFactor, scaleFactor);

這樣我們就能保證圖片的高和ImageView同樣,而且填充滿ImageView.
接下來我們就讓ImageView的圖片移動,我們要用到一個強大的Android動畫架構:ValueAnimator,其原理就是利用ImageView的映像矩陣在x軸方向變換移動。

mAnimator = ValueAnimator.ofFloat(0, 100);mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {  @Override  public void onAnimationUpdate(ValueAnimator animation) {      float value = (Float) animation.getAnimatedValue();      matrix.reset();      matrix.postScale(scaleFactor, scaleFactor);      matrix.postTranslate(-value, 0);      imageView.setImageMatrix(matrix);  }});mAnimator.setDuration(5000);mAnimator.start();

整個代碼例如以下:

package com.testimageview;import android.animation.Animator;import android.animation.AnimatorListenerAdapter;import android.animation.ValueAnimator;import android.app.Activity;import android.graphics.Matrix;import android.graphics.RectF;import android.os.Bundle;import android.widget.ImageView;public class MainActivity extends Activity{    private static final int RightToLeft = 1;    private static final int LeftToRight = 2;    private static final int DURATION = 5000;    private ValueAnimator mCurrentAnimator;    private final Matrix mMatrix = new Matrix();    private ImageView mImageView;    private float mScaleFactor;    private int mDirection = RightToLeft;    private RectF mDisplayRect = new RectF();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);      mImageView = (ImageView) findViewById(R.id.imageView);      mImageView.post(new Runnable() {          @Override          public void run() {              mScaleFactor = (float)  mImageView.getHeight()                       / (float) mImageView.getDrawable().getIntrinsicHeight();              mMatrix.postScale(mScaleFactor, mScaleFactor);              mImageView.setImageMatrix(mMatrix);              animate();          }      });    }    private void animate() {      updateDisplayRect();      if(mDirection == RightToLeft) {          animate(mDisplayRect.left, mDisplayRect.left                  - (mDisplayRect.right - mImageView.getWidth()));      } else {          animate(mDisplayRect.left, 0.0f);      }    }    private void animate(float from, float to) {      mCurrentAnimator = ValueAnimator.ofFloat(from, to);      mCurrentAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {          @Override          public void onAnimationUpdate(ValueAnimator animation) {              float value = (Float) animation.getAnimatedValue();              mMatrix.reset();              mMatrix.postScale(mScaleFactor, mScaleFactor);              mMatrix.postTranslate(value, 0);              mImageView.setImageMatrix(mMatrix);          }      });      mCurrentAnimator.setDuration(DURATION);      mCurrentAnimator.addListener(new AnimatorListenerAdapter() {          @Override          public void onAnimationEnd(Animator animation) {              if(mDirection == RightToLeft)                  mDirection = LeftToRight;              else                  mDirection = RightToLeft;              animate();          }      });      mCurrentAnimator.start();    }    private void updateDisplayRect() {      mDisplayRect.set(0, 0, mImageView.getDrawable().getIntrinsicWidth(),              mImageView.getDrawable().getIntrinsicHeight());      mMatrix.mapRect(mDisplayRect);    }}

像音樂播放App一樣移動背景

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.