-
-
Save woshidan/3e7b97e695d8e1a414b3d598b3cd7a0f to your computer and use it in GitHub Desktop.
View Animation vs Property Animation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:gravity="bottom" | |
tools:context="com.example.woshidan.animationtest.MainActivity"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
android:gravity="center" | |
android:orientation="horizontal" | |
> | |
<TextView | |
android:id="@+id/testView" | |
android:text="Text" | |
android:padding="16dp" | |
android:background="@android:color/holo_blue_dark" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
/> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
> | |
<Button | |
android:id="@+id/view_animation_translate" | |
android:textSize="10dp" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="View Translate" | |
/> | |
<Button | |
android:id="@+id/view_animation_scale" | |
android:textSize="10dp" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="View Scale" /> | |
<Button | |
android:id="@+id/view_animation_rotate" | |
android:textSize="10dp" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="View Rotate" /> | |
<Button | |
android:id="@+id/view_animation_alpha" | |
android:textSize="10dp" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="View Alpha" /> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
> | |
<Button | |
android:id="@+id/property_animation_translate" | |
android:textSize="10dp" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Prop Translate" | |
/> | |
<Button | |
android:id="@+id/property_animation_scale" | |
android:textSize="10dp" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Prop Scale" /> | |
<Button | |
android:id="@+id/property_animation_rotate" | |
android:textSize="10dp" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Prop Rotate" /> | |
<Button | |
android:id="@+id/property_animation_alpha" | |
android:textSize="10dp" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Prop Alpha" /> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
> | |
<Button | |
android:id="@+id/check_props_position" | |
android:textSize="10dp" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Check Position" | |
/> | |
<Button | |
android:id="@+id/check_props_rotate_alpha" | |
android:textSize="10dp" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Check α, rotate" /> | |
<Button | |
android:id="@+id/check_props_size" | |
android:textSize="10dp" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Check Size" /> | |
<Button | |
android:id="@+id/check_view_invalidate" | |
android:textSize="10dp" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Invalidate" /> | |
</LinearLayout> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
final String TAG = "ANIMATION_TEST"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
final boolean IS_ENABLE_FILL_AFTER = true; | |
final TextView testView = (TextView) findViewById(R.id.testView); | |
// view animation | |
findViewById(R.id.view_animation_translate).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Log.d(TAG, "view_animation_translate"); | |
TranslateAnimation animation = new TranslateAnimation(0, 0, 100, 180); | |
animation.setDuration(100); | |
animation.setFillAfter(IS_ENABLE_FILL_AFTER); | |
testView.startAnimation(animation); | |
} | |
}); | |
findViewById(R.id.view_animation_scale).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Log.d(TAG, "view_animation_scale"); | |
ScaleAnimation animation = new ScaleAnimation(0, 2.0f, 0, 3.0f); | |
animation.setDuration(100); | |
animation.setFillAfter(IS_ENABLE_FILL_AFTER); | |
testView.startAnimation(animation); | |
} | |
}); | |
findViewById(R.id.view_animation_rotate).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Log.d(TAG, "view_animation_rotate"); | |
RotateAnimation animation = new RotateAnimation(0, 90f, 30, 100); | |
animation.setDuration(100); | |
animation.setFillAfter(IS_ENABLE_FILL_AFTER); | |
testView.startAnimation(animation); | |
} | |
}); | |
findViewById(R.id.view_animation_alpha).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Log.d(TAG, "view_animation_alpha"); | |
AlphaAnimation animation = new AlphaAnimation(1, 0.3f); | |
animation.setDuration(100); | |
animation.setFillAfter(IS_ENABLE_FILL_AFTER); | |
testView.startAnimation(animation); | |
} | |
}); | |
// property animation | |
findViewById(R.id.property_animation_translate).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Log.d(TAG, "property_animation_translate"); | |
testView.setTranslationX(100); | |
testView.setTranslationY(180); | |
} | |
}); | |
findViewById(R.id.property_animation_scale).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Log.d(TAG, "property_animation_scale"); | |
testView.setScaleX(2.0f); | |
testView.setScaleY(3.0f); | |
} | |
}); | |
findViewById(R.id.property_animation_rotate).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Log.d(TAG, "property_animation_rotate"); | |
testView.setRotation(30); | |
testView.setPivotX(100); | |
testView.setPivotY(150); | |
} | |
}); | |
findViewById(R.id.property_animation_alpha).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Log.d(TAG, "property_animation_alpha"); | |
testView.setAlpha(0.5f); | |
} | |
}); | |
findViewById(R.id.property_animation_translate).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Log.d(TAG, "property_animation_translate"); | |
testView.setTranslationX(100); | |
testView.setTranslationY(180); | |
} | |
}); | |
// check | |
findViewById(R.id.check_props_position).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Log.d(TAG, "x: " + testView.getX() + " y: " + testView.getY()); | |
} | |
}); | |
findViewById(R.id.check_props_rotate_alpha).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Log.d(TAG, "alpha: " + testView.getAlpha() + " rotate: " + testView.getRotation()); | |
} | |
}); | |
findViewById(R.id.check_props_size).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Log.d(TAG, "width: " + testView.getWidth() + " height: " + testView.getHeight()); | |
} | |
}); | |
findViewById(R.id.check_view_invalidate).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Log.d(TAG, "invalid view!"); | |
testView.invalidate(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment