Android部分開源項目源碼分析之--圖片滑動的效果

前言:

  我們時常可以看到一張圖片在一個固定大小的佈局中,慢慢的滑動圖片,這樣的一個效果看起來有一定的靈動性,感覺很好看的,特別是用一張天空雲,作爲背景看去來,還不錯的。很有吸引力的。

 我在https://github.com/jpardogo/ListBuddies 中看到了這樣的效果,現在把這個code摘錄出來。

詳見: https://github.com/dblackde/BackgroundSlide

Code:

/**
 * 參考源碼  https://github.com/jpardogo/ListBuddies中的about界面
 * @author Administrator
 *
 */
public class MainActivity extends Activity {

	ImageView imageView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		imageView=(ImageView) findViewById(R.id.image);
		moveBackground();
		
	}
    private  Matrix mMatrix = new Matrix();
    float mScaleFactor;
	private void moveBackground() {
		// TODO Auto-generated method stub
		imageView.post(new Runnable() {
			
			@Override
			public void run() {
				//imageView控件所佔的屏幕的高度
				int imgHeight=imageView.getHeight();
				// 返回這個imageview中的圖片的實際的高度
				int imgIntrinsicHeight=imageView.getDrawable().getIntrinsicHeight();
				
				//imageView控件所佔的屏幕的寬度
				int imgWidth=imageView.getWidth();
				
				int imgIntrinsicimgWidth=imageView.getDrawable().getIntrinsicWidth();
			
				/*
				 * 通過img 所在屏幕上的像素,以及圖片實際的大小(單位像素) 的高度的 比值來 確定圖片的縮放 
				 * 以保證,圖片在高上面能夠填滿圖片的位置
				 */
				mScaleFactor=((float)imgHeight/(float)imgIntrinsicHeight);
				mMatrix.postScale(mScaleFactor,mScaleFactor);
				
				/**
				 * 圖片設置    android:scaleType="matrix" 
				 * 
				 */
				 imageView.setImageMatrix(mMatrix);
				 animate();
			}
		});
		
	}
    private RectF mDisplayRect = new RectF();
    private static final int RightToLeft = 1;
    private static final int LeftToRight = 2;
	private int mDirection = RightToLeft;

    private void updateDisplayRect() {
    	mDisplayRect.set(0, 0, imageView.getDrawable().getIntrinsicWidth(), imageView.getDrawable().getIntrinsicHeight());
    	System.out.println("矩形"+mDisplayRect.toString());
    	mMatrix.mapRect(mDisplayRect);
    }
	protected void animate() {
		updateDisplayRect();
		if (mDirection == RightToLeft) {
            animate(mDisplayRect.left, mDisplayRect.left - (mDisplayRect.right - imageView.getWidth()));
        } else {
            animate(mDisplayRect.left, 0.0f);
        }
	}
    private ValueAnimator mCurrentAnimator;

	private void animate(float from, float to) {
		// TODO Auto-generated method stub
		System.out.println(from+"  "+to);
		
		mCurrentAnimator=ValueAnimator.ofFloat(from,to);
		mCurrentAnimator.addUpdateListener(new AnimatorUpdateListener() {
			
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				// TODO Auto-generated method stub
                float value = (Float) animation.getAnimatedValue();
                mMatrix.reset();
                mMatrix.postScale(mScaleFactor, mScaleFactor);
                mMatrix.postTranslate(value, 0);
                imageView.setImageMatrix(mMatrix);
			}
		});
        mCurrentAnimator.setDuration(30000);
        mCurrentAnimator.addListener(new AnimatorListener() {
			@Override
			public void onAnimationStart(Animator animation) {
			}
			@Override
			public void onAnimationRepeat(Animator animation) {
			}
			@Override
			public void onAnimationEnd(Animator animation) {
				if (mDirection== RightToLeft) {
					mDirection=LeftToRight;
				}else {
					mDirection=RightToLeft;
				}
				animate();
			}
			
			@Override
			public void onAnimationCancel(Animator animation) {
				
			}
		});

        mCurrentAnimator.start();
	}
	

}

本例子,只可用與API11+





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章