android 控件放大缩小效果实现

像android tv中很多应用一样,焦点在控件上的时候,控件放大,失去焦点时,控件还原。

小小的实现一个简单的demo。

public class ScaleRunnable implements Runnable {
	
	private static int ZOOM_ANIMATION_DURATION = 200;
	
	private final View mView;
	
	private float mStartScale;
	private float mTargetScale;
	
	private long mStartTime;
	
	private int scaleAnimTime;
	
	private Boolean mIsRun = false;//Boolean是包装类型,boolean是基本类型
	private Boolean mStop;
	private Boolean mZoomIn;
	
	private float mVelocity;
	
	private onScaleEndListen monScaleEndListen;

	ScaleRunnable(View view){
		this(view, ZOOM_ANIMATION_DURATION);//构造函数不能直接调用
	}
	
	public ScaleRunnable(View view, int Animtime) {
		// TODO Auto-generated constructor stub
		mView = view;
		scaleAnimTime = Animtime;
	}
	
	public void setOnScaleEndListen(onScaleEndListen l){
		monScaleEndListen = l;
	}
	
	public void start(float startScale, float targetScale){
		if(mIsRun){
			return;
		}
		mStartScale = startScale;
		mTargetScale = targetScale;
		mVelocity = (mTargetScale - mStartScale)/scaleAnimTime;
		mZoomIn = (mTargetScale > mStartScale) ? true:false;
		
		mStartTime = System.currentTimeMillis();
		mStop = false;
		mIsRun = true;
		mView.post(this);//在ui线程中运行,会调用run()函数
	}
	
	public void stop(){
		mStop = true;
		mIsRun = false;
		mView.buildDrawingCache();
	}

	@Override
	public void run() {
		// TODO Auto-generated method stubs
		if(mStop){
			return;
		}
		
		long time = System.currentTimeMillis();
		long elapsed = time - mStartTime;
		float newScale = mStartScale + elapsed * mVelocity;
		
		if(newScale == mTargetScale || (mZoomIn == (newScale > mTargetScale))){
			mView.setScaleX(mTargetScale);
			mView.setScaleY(mTargetScale);
			if(null != monScaleEndListen){
				monScaleEndListen.onScaleEnd();
			}
			
			stop();
		}else{
			mView.setScaleX(newScale);
			mView.setScaleY(newScale);
		}
		
		if (!mStop) {
			mView.post(this);//在放大没完成时,继续放大
		}
	}
	
	public static interface onScaleEndListen{
		public void onScaleEnd();
	}

}
ScaleRunnable继承自Runnable,在run()中缩放view


在MainActivity中,

public class MainActivity extends Activity implements OnClickListener{
	
	private Button mBtnA;
	private Button mBtnB;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mBtnA = (Button)findViewById(R.id.btn_a);
		mBtnA.setOnClickListener(this);
		
		mBtnA.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View arg0, boolean arg1) {
				// TODO Auto-generated method stub
				
				ScaleRunnable scale = new ScaleRunnable(mBtnA);
				scale.setOnScaleEndListen(new onScaleEndListen(){

					@Override
					public void onScaleEnd() {
						// TODO Auto-generated method stub
						Log.i("TAG", "ScaleSucceed A");
					}
					
				});
				if(arg1){
					Log.i("TAG", "mBtnA focus");
					scale.start(1.0f, 1.5f);
				}else{
					Log.i("TAG", "mBtnA lost focus");
					scale.start(1.5f, 1.0f);
				}
			}
		});
		
		mBtnB = (Button)findViewById(R.id.btn_b);
		
		mBtnB.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View arg0, boolean arg1) {
				// TODO Auto-generated method stub
				ScaleRunnable scale = new ScaleRunnable(mBtnB);
				scale.setOnScaleEndListen(new onScaleEndListen(){

					@Override
					public void onScaleEnd() {
						// TODO Auto-generated method stub
						Log.i("TAG", "ScaleSucceed B");
					}
					
				});
				if(arg1){
					Log.i("TAG", "mBtnB focus");
					scale.start(1.0f, 1.5f);
				}else{
					Log.i("TAG", "mBtnB lost focus");
					scale.start(1.5f, 1.0f);
				}
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
			
		return super.onOptionsItemSelected(item);
	}
}
就可实现放大缩小的效果。


你可能感兴趣的:(Android,零碎知识)