動畫的變換:
/*
* 編碼實作View Animation
* 1. Code方式
* 2. Xml方式
*/
public class VAActivity extends Activity {
private ImageView iv_animation;
private TextView tv_animation_msg;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animation_va);
iv_animation = (ImageView) findViewById(R.id.iv_animation);
tv_animation_msg = (TextView) findViewById(R.id.tv_animation_msg);
}
/*
* 1.1 編碼實作: 縮放動畫
* ScaleAnimation
*/
/*
//1. 建立動畫對象
//2. 設定
//3. 啟動動畫
*/
public void startCodeScale(View v) {
tv_animation_msg.setText("Code縮放動畫: 寬度從0.5到1.5, 高度從0.0到1.0, 縮放的圓心為頂部中心點,延遲1s開始,持續2s,最終還原");
//1. 建立動畫對象
ScaleAnimation animation = new ScaleAnimation(0.5f, 1.5f, 0, 1,
Animation.ABSOLUTE, iv_animation.getWidth()/2, Animation.ABSOLUTE, 0);
animation = new ScaleAnimation(0.5f, 1.5f, 0, 1,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0);
//2. 設定
//延遲1s開始
animation.setStartOffset(1000);
//持續2s
animation.setDuration(2000);
//最終還原
animation.setFillBefore(true);
//3. 啟動動畫
iv_animation.startAnimation(animation);
}
/*
* 1.2 xml實作: 縮放動畫
* <scale>
*/
/*
1. 定義動畫檔案
2. 加載動畫檔案得到動畫對象
3. 啟動動畫
*/
public void startXmlScale(View v) {
tv_animation_msg.setText("Xml縮放動畫: Xml縮放動畫: 寬度從0.0到1.5, 高度從0.0到1.0, 延遲1s開始,持續3s,圓心為右下角, 最終固定");
//1. 定義動畫檔案
//2. 加載動畫檔案得到動畫對象
Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale_test);
//3. 啟動動畫
iv_animation.startAnimation(animation);
}
/*
* 2.1 編碼實作: 旋轉動畫
* RotateAnimation
*/
public void startCodeRotate(View v) {
tv_animation_msg.setText("Code旋轉動畫: 以圖檔中心點為中心, 從負90度到正90度, 持續5s");
//1. 建立動畫對象
RotateAnimation animation = new RotateAnimation(-90, 90, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//2. 設定
animation.setDuration(5000);
//3. 啟動動畫
iv_animation.startAnimation(animation);
}
/*
* 2.2 xml實作: 旋轉動畫
* <rotate>
*/
public void startXmlRotate(View v) {
tv_animation_msg.setText("Xml旋轉動畫: 以左頂點為坐标, 從正90度到負90度, 持續5s");
//1. 定義動畫檔案
//2. 加載動畫檔案得到動畫對象
Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_test);
//3. 啟動動畫
iv_animation.startAnimation(animation);
}
/*
* 3.1 編碼實作: 透明度動畫
* 完全透明 : 0
* 完全不透明 : 1
* AlphaAnimation
*/
public void startCodeAlpha(View v) {
tv_animation_msg.setText("Code透明度動畫: 從完全透明到完全不透明, 持續2s");
//1. 建立動畫對象
AlphaAnimation animation = new AlphaAnimation(0, 1);
// 2. 設定
animation.setDuration(4000);
// 3. 啟動動畫
iv_animation.startAnimation(animation);
}
/*
* 3.2 xml實作: 透明度動畫
* <alpha>
*/
public void startXmlAlpha(View v) {
tv_animation_msg.setText("Xml透明度動畫: 從完全不透明到完全透明, 持續4s");
//1. 定義動畫檔案
//2. 加載動畫檔案得到動畫對象
Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha_test);
animation.setFillAfter(true);
//3. 啟動動畫
iv_animation.startAnimation(animation);
}
/*
* 4.1 編碼實作: 平移動畫
* TranslateAnimation
*/
public void startCodeTranslate(View v) {
tv_animation_msg.setText("Code移動動畫: 向右移動一個自己的寬度, 向下移動一個自己的高度, 持續2s");
//1. 建立動畫對象
TranslateAnimation animation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.RELATIVE_TO_SELF, 1, Animation.ABSOLUTE, 0, Animation.RELATIVE_TO_SELF, 1);
//2. 設定
animation.setDuration(2000);
//3. 啟動動畫
iv_animation.startAnimation(animation);
}
/*
* 4.2 xml實作: 平移動畫
* <translate>
*/
public void startXmlTranslate(View v) {
tv_animation_msg.setText("xml移動動畫: 從螢幕的右邊逐漸回到原來的位置, 持續2s"); //***此效果用于界面切換的動畫效果
//1. 定義動畫檔案
//2. 加載動畫檔案得到動畫對象
Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate_test);
//3. 啟動動畫
iv_animation.startAnimation(animation);
}
/*
* 5.1 編碼實作: 複合動畫
* AnimationSet
*/
public void startCodeAnimationSet(View v) {
tv_animation_msg.setText("Code複合動畫: 透明度從透明到不透明, 持續2s, 接着進行旋轉360度的動畫, 持續1s");
//1. 建立透明動畫并設定
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
alphaAnimation.setDuration(2000);
//2. 建立旋轉動畫并設定
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000);
rotateAnimation.setStartOffset(2000);//延遲
//3. 建立複合動畫對象
AnimationSet animationSet = new AnimationSet(true);
//4. 添加兩個動畫
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(rotateAnimation);
//5. 啟動複合動畫對象
iv_animation.startAnimation(animationSet);
}
/*
* 5.2 xml實作: 複合動畫
* <set>
*/
public void startXmlAnimationSet(View v) {
tv_animation_msg.setText("Xml複合動畫: 透明度從透明到不透明, 持續2s, 接着進行旋轉360度的動畫, 持續2s");
//1. 定義動畫檔案
//2. 加載動畫檔案得到動畫對象
Animation animation = AnimationUtils.loadAnimation(this, R.anim.set_test);
//3. 啟動動畫
iv_animation.startAnimation(animation);
}
/*
* 6. 測試動畫監聽
*/
public void testAnimationListener(View v) {
tv_animation_msg.setText("測試動畫監聽");
//tv_animation_msg.setText("Xml旋轉動畫: 以左頂點為坐标, 從正90度到負90度, 持續5s");
//1. 定義動畫檔案
//2. 加載動畫檔案得到動畫對象
Animation animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(1000);
//設定線性變化
animation.setInterpolator(new LinearInterpolator());
//設定動畫重複次數
animation.setRepeatCount(Animation.INFINITE);//重複3次
//設定動畫監聽
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
Log.e("TAG", "動畫開始");
}
@Override
public void onAnimationRepeat(Animation animation) {
Log.e("TAG", "動畫重複");
}
@Override
public void onAnimationEnd(Animation animation) {
Log.e("TAG", "動畫結束");
}
});
//3. 啟動動畫
iv_animation.startAnimation(animation);
}
}
ListView的二層優化:
Viewholder holder = null;
if(converterView==null) {
converterView = View.inflate(R.layout.xxx);
holder = new ViewHolder();
holder.imageView = (ImageView)converterView.findViewById(xxx);
holder.textView = (TextView)converterView.findViewById(yyy);
converterView.setTag(holder);
} else {
holder = (Viewholder )converterView.getTag();
}
Person p = data.get(position);
holder.imageView.setimage(p.getIcon())
holder.textView.setText(p.getName())
static class ViewHolder {
ImageView imageView;
TextView textView;
}