前一篇講到通過開多個線程來啟動每個view 的動畫來實現頁面的逐條重新整理,開銷很大。這裡給出了一個比較合理的方案,就是在頁面上產生一個遮罩,通過遮罩的下移動畫來實現內容的逐步重新整理。
主畫面類:
package com.drawmask;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class DrawMask extends Activity implements OnClickListener {
private LinearLayout reload;
private View mask;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 擷取螢幕寬高
DisplayMetrics dm = new DisplayMetrics();
dm = getApplicationContext().getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
RelativeLayout parent = (RelativeLayout) findViewById(R.id.RelativeLayout01);
// 建立遮罩
mask = new Mask(this, screenWidth, screenHeight);
parent.addView(mask);
startOpenAnimation();
// 點擊螢幕重啟遮罩動畫
reload = (LinearLayout) findViewById(R.id.reload);
reload.setOnClickListener(this);
}
private void startOpenAnimation() {
TranslateAnimation ta = new TranslateAnimation(0, 0, 0, 500);
ta.setDuration(2000);
ta.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
mask.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
}
});
mask.startAnimation(ta);
}
@Override
public void onClick(View v) {
startOpenAnimation();
}
}
遮罩類:
package com.drawmask;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.view.View;
public class Mask extends View{
private int width, higth;
@Override
protected void onDraw(Canvas canvas) {
// 產生可以繪製透明漸層的矩形View的畫筆
Paint paint = new Paint();
Shader mShader=new LinearGradient(0,0,0,50,
new int[]{Color.argb(0, 255, 255, 255),Color.WHITE},
null,Shader.TileMode.CLAMP);
paint.setShader(mShader);
// 以繪製透明漸層的矩形View
Rect rect = new Rect();
rect.set(0, 0, width, higth);
canvas.drawRect(rect, paint);
}
public Mask(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public Mask(Context context, int width, int higth) {
super(context);
this.higth = higth;
this.width = width;
}
}