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+





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