生化本來就不容易啊,而我們的不努力只會讓生活變得更加無賴
這一個月發生的事有點多,有那么一段時間在緩沖。然后課程也開始比較密集,一天到晚不是去上課就是在去上課的路上。但是學習不能停,本來這些個筆記是在之前就應該整理的,但是一直到現在才有心思在學習《Android群英傳》的基礎上總結學習筆記。
今天的筆記是關于安卓的動畫,這個稍微接觸過安卓的朋友都不會陌生,Android的動畫主要分為視圖動畫和屬性動畫,在3.0之前視圖動畫一家獨大,在一開始基本可以滿足我們的需求,但是為什么會出現屬性動畫呢?那肯定是試圖動畫越來越不滿足實際開發的需要了,至于是什么?接下來會涉及。首先我們先來了解一下基本用法。
Android的視圖動畫
說到視圖動畫,得先認識Animation這個類,這個是什么意思呢?可以自行請教英語老師。但我們也還是需認識它的四個擴展類:
- AlphaAnmiation(透明度動畫)
- RotateAnimation(旋轉動畫)
- TranslateAnimation(位移動畫)
- ScaleAnimation(縮放動畫)
這幾個類的用法十分簡單,可以在代碼中直接使用或者在布局文件中定義使用(直接看用法)
- MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnAlpha ,btnRotate,btnTranslate,btnScale ,btnSet;
private AlphaAnimation a;
private RotateAnimation r ;
private TranslateAnimation t;
private ScaleAnimation scale;
private AnimationSet set ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAlpha = (Button) findViewById(R.id.btnAlpha);
btnRotate = (Button) findViewById(R.id.btnRotate);
btnTranslate = (Button) findViewById(R.id.btnTranslate);
btnScale = (Button) findViewById(R.id.btnScale);
btnSet = (Button) findViewById(R.id.btnSet);
btnAlpha.setOnClickListener(this);
btnRotate.setOnClickListener(this);
btnTranslate.setOnClickListener(this);
btnScale.setOnClickListener(this);
btnSet.setOnClickListener(this);
}
/**
* AlphaAnimation
* @param v
*/
private void btnAlpha(View v){
a = new AlphaAnimation(0,1);
a.setDuration(1000);
v.startAnimation(a);
}
/**
* RotateAnimation
* @param v
*/
private void btnRotate(View v){
//參數分別為旋轉角度和旋轉中心
r = new RotateAnimation(0,360,100,100);
r.setDuration(1000);
v.startAnimation(r);
}
/**
* TranslateAnimation
* @param v
*/
private void btnTranslate(View v){
t = new TranslateAnimation(0,200,0,200);
t.setDuration(1000);
Toast.makeText(MainActivity.this,"Button onClick",Toast.LENGTH_SHORT).show();
//動畫結束后不歸位
t.setFillAfter(true);
v.startAnimation(t);
//動畫監聽
t.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
/**
* ScaleAnimation
* @param view
*/
private void btnScale(View view){
scale = new ScaleAnimation(0,2,0,2);
scale.setDuration(1000);
view.startAnimation(scale);
}
/**
* ScaleAnimation
* @param v
*/
private void btnSet(View v){
AnimationSet set = new AnimationSet(true);
set.setDuration(1000);
set.addAnimation(a);
set.addAnimation(r);
set.addAnimation(t);
set.addAnimation(scale);
v.startAnimation(set);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btnAlpha:
btnAlpha(view);
break;
case R.id.btnRotate:
btnRotate(view);
break;
case R.id.btnTranslate:
btnTranslate(view);
break;
case R.id.btnScale:
btnScale(view);
break;
case R.id.btnSet:
btnSet(view);
break;
}
}
}```
* 效果

* 總結:相信大家都會從上面效果圖看到視圖動畫到底有什么問題(請注意TRANSLATE),是的,這件詭異的事情說明視圖動畫**不具備交互性**是越來越滿足不了用戶,更別說什么良好的用戶體驗,所以大家也會明白為什么Google會再3.0推出屬性動畫了吧。那么接下來就愉快地學習屬性動畫的基本用法吧!
### Android的屬性動畫
學習視圖動畫就必須認識這兩個類:
1. ObjectAnimator(兒子)
2. ValueAnimator (爸爸)
#####ObjectAnimator的基本用法
ObjectAnimatior和AnimatorSet的結合就可以一次滿足所有基本需求
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnAlpha ,btnRotate,btnTranslate,btnScale ,btnSet;
private ObjectAnimator animator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAlpha = (Button) findViewById(R.id.btnAlpha);
btnRotate = (Button) findViewById(R.id.btnRotate);
btnTranslate = (Button) findViewById(R.id.btnTranslate);
btnScale = (Button) findViewById(R.id.btnScale);
btnSet = (Button) findViewById(R.id.btnSet);
btnAlpha.setOnClickListener(this);
btnRotate.setOnClickListener(this);
btnTranslate.setOnClickListener(this);
btnScale.setOnClickListener(this);
btnSet.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id = view.getId();
switch (id){
case R.id.btnAlpha:
btnAlpha(view);
break;
case R.id.btnRotate:
btnRotate(view);
break;
case R.id.btnTranslate:
btnTranslate(view);
break;
case R.id.btnScale:
btnScale(view);
break;
case R.id.btnSet:
btnSet(view);
break;
}
}
private void btnSet(View view) {
ObjectAnimator o1 = ObjectAnimator.ofFloat(view,"translationX",0,200);
ObjectAnimator o2 = ObjectAnimator.ofFloat(view,"rotation",0,360);
ObjectAnimator o3 = ObjectAnimator.ofFloat(view,"scaleX",0,2);
AnimatorSet set = new AnimatorSet();
set.setInterpolator(new BounceInterpolator());//插值器
set.setDuration(1000);
// set.playSequentially(o1,o2,o3);//順序
set.playTogether(o1,o2,o3);//一起執行
// set.play(o1).after(o2).with(o3);//制定順序
set.start();
//監聽事件
set.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
showToast();
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
//只關注一個時刻的監聽
set.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
showToast();
}
});
}
private void btnScale(View view) {
animator = ObjectAnimator.ofFloat(view,"scaleX",0,2);
animator.setDuration(1000);
animator.start();
}
private void btnTranslate(View view) {
animator = ObjectAnimator.ofFloat(view,"translationX",0,200);
animator.setDuration(1000);
animator.start();
}
private void btnRotate(View view) {
animator = ObjectAnimator.ofFloat(view,"rotation",0,360);
animator.setDuration(1000);
animator.start();
}
private void btnAlpha(View view) {
animator = ObjectAnimator.ofFloat(view,"alpha",1,0);
animator.setDuration(1000);
animator.start();
}
}```
- 同時也可以通過PropertyValuesHolder來實現類似AnimatorSet的PlayTogether()的效果
PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("translationX",0,200);
PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("scaleX",0,2);
PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("scaleY",0,2);
PropertyValuesHolder p4 = PropertyValuesHolder.ofFloat("rotation",0,360);
ObjectAnimator.ofPropertyValuesHolder(view,p1,p2,p3,p4)
.setDuration(1000).start();```
* 屬性說明
1. ```translationX和translationY```(坐標偏移)
2. ```rotation,rotationX和rotationY```(3D,2D旋轉)
3. ```scaleX和scaleY```(2D縮放)
4. ```pivotX和pivotY```(支點,默認為view的中心點)
5. ```x和y```(最終位置)
6. ```alpha```(透明度,默認值1為不透)
* 效果

#####ValueAnimator的基本用法(數值發生器)
final Button b = (Button) view;
ValueAnimator v = ValueAnimator.ofInt(0, 100);
v.setDuration(5000);
v.setTarget(view);
v.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
Integer i = (Integer) valueAnimator.getAnimatedValue();
b.setText("" + i);
}
});
v.start();```
- 如上實現一個Button在5S內實現計時器。
常用拓展
經常會在app(如QQ音樂)的某些選擇操作會有個底部彈窗給用戶選擇,所以趁著這次學習Android動畫順便學習了一下這個彈窗的做法。順便還復習了一遭RecyclerView
的用法,之前也寫過一篇關于RecyclerView
的筆記RecyclerView的基本用法(一)
代碼如下:
- style.xml
<style name="PopupDialog" parent="android:Theme.Dialog">
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowFrame">@null</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
</style>```
關于style的這里有一篇博文,基本滿足需求:[Style屬性](http://blog.csdn.net/jflex/article/details/7854573)
2. enter_menu.xml(進入動畫)
<objectAnimator
android:propertyName="translationY"
android:valueFrom="100"
android:valueTo="0"
android:duration="300">
</objectAnimator>```
- exit_menu.xml(退出動畫)
android:duration="300"
android:propertyName="translationY"
android:valueFrom="0"
android:valueTo="100" />```
4. item_choice.xml
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:gravity="center"
android:textSize="15sp"
android:textColor="#888"
android:text="@string/app_name">
</TextView>
</LinearLayout>```
- PopupDialog.java
public class Popupdialog extends Dialog implements View.OnClickListener {
private Animator enterAnimator,exitAnimator;//進出動畫
private View mContentView;
private RecyclerView recyclerView;
private boolean isClose;//dialog狀態
private Button btnCancek;
private List<String> list;
private RecyclerAdapter adapter;
public Popupdialog(Context context) {
super(context,R.style.PopupDialog);//設置基本屬性
getWindow().setGravity(Gravity.BOTTOM);//底部彈窗
initView(context);//初始View
initAnimator(context);//初始動畫
}
public Popupdialog(Context context, int themeResId) {
super(context, themeResId);
}
public Popupdialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
private void initView(final Context context) {
mContentView = LayoutInflater.from(context).inflate(item,null);
btnCancek = (Button) mContentView.findViewById(R.id.btnCancel);
btnCancek.setOnClickListener(this);
recyclerView = (RecyclerView) mContentView.findViewById(R.id.popupRecycler);
recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
ItemDecoration item = new ItemDecoration(context, LinearLayoutManager.VERTICAL);
recyclerView.addItemDecoration(item);
recyclerView.setItemAnimator(new DefaultItemAnimator());
list = new ArrayList<>();
adapter = new RecyclerAdapter(context,list);
recyclerView.setAdapter(adapter);
adapter.setListener(new RecyclerAdapter.onItemClickListener() {
@Override
public void onItemClick(View itemView, int adapterPosition) {
showToast(context,adapterPosition);
if (itemClickListener != null){
itemClickListener.itemClick(adapterPosition);
}
dismiss();
}
});
setContentView(mContentView);
}
public Popupdialog addItem(String str){
list.add(str);
return this;
}
@Override
public void show() {
adapter.notifyDataSetChanged();
super.show();
//適配屏幕大小,必須在show()方法后
Window window = getWindow();
WindowManager.LayoutParams wl = window.getAttributes();
wl.width = MainActivity.getMetrics().widthPixels;
window.setAttributes(wl);
//退出動畫
exitAnimator.setTarget(mContentView);
enterAnimator.start();
}
@Override
public void dismiss() {
super.dismiss();
if (isClose){
return;
}
isClose = true;
//動畫進入
exitAnimator.setTarget(mContentView);
exitAnimator.start();
}
public void disMissMe(){
super.dismiss();
isClose = false;
}
private void showToast(Context context, int adapterPosition) {
Toast.makeText(context,adapterPosition + "onClick",Toast.LENGTH_SHORT).show();
}
private void initAnimator(Context context) {
enterAnimator = AnimatorInflater.loadAnimator(context,R.animator.menu_enter);
exitAnimator = AnimatorInflater.loadAnimator(context,R.animator.menu_exit);
exitAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
disMissMe();
}
});
}
@Override
public void onClick(View view) {
onBackPressed();
}
public interface ItemClickListener{
void itemClick(int adapterPosition);
}
private ItemClickListener itemClickListener;
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
}
- 效果
總結
Android的動畫基本如上,但是這些都是建立在巨人的肩膀上的,希望自己以后認真學習,爭取盡快擁有滿足市場要求的能力。另外,這個月的事的確影響到自己,希望自己認識到個中因由,警示自己,不斷向前!
如果你覺得本文有所錯漏,請指出批評,交流學習共同進步