安卓常用的基本动画代码
2015-01-27 16:16
225 查看
安卓常用的动画
translationX and translationY
rotation, rotationX, and rotationY
scaleX and scaleY
pivotX and pivotY
x and y
alpha
主要是上诉几种代码效果,直接贴代码吧:
[java] view
plaincopy
package com.example.test;
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView iv;
private float mScreenHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
iv = (ImageView) findViewById(R.id.iv);
DisplayMetrics outMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
mScreenHeight = outMetrics.heightPixels;
}
public void click(View view) {
// 平移动画
ObjectAnimator transX = ObjectAnimator.ofFloat(iv, "translationX", 0f,
300f);
transX.setDuration(2000);
transX.setRepeatCount(5);
transX.setRepeatMode(ObjectAnimator.RESTART);
transX.start();
// 平移动画
ObjectAnimator transY = ObjectAnimator.ofFloat(iv, "translationY", 0f,
300f);
transY.setDuration(1000);
transY.setRepeatCount(3);
transY.setRepeatMode(ObjectAnimator.RESTART);
transY.start();
// 旋转动画
ObjectAnimator rotation = ObjectAnimator.ofFloat(iv, "rotation", 0f,
360f);
rotation.setDuration(1000);
rotation.setRepeatCount(7);
rotation.setRepeatMode(ObjectAnimator.RESTART);
rotation.start();
// 缩放
ObjectAnimator scaleX = ObjectAnimator.ofFloat(iv, "scaleX", 0f, 1.6f);
scaleX.setDuration(1000);
scaleX.setRepeatCount(5);
scaleX.setRepeatMode(ObjectAnimator.RESTART);
scaleX.start();
// 缩放动画
ObjectAnimator scaleY = ObjectAnimator.ofFloat(iv, "scaleY", 0f, 1.6f);
scaleY.setDuration(1000);
scaleY.setRepeatCount(5);
scaleY.setRepeatMode(ObjectAnimator.RESTART);
scaleY.start();
// 缩放动画
ObjectAnimator alpha = ObjectAnimator.ofFloat(iv, "alpha", 0f, 1f);
alpha.setDuration(1000);
alpha.setRepeatCount(5);
alpha.setRepeatMode(ObjectAnimator.RESTART);
alpha.start();
transX.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
Toast.makeText(MainActivity.this, "终于结束了...", Toast.LENGTH_LONG)
.show();
}
@Override
public void onAnimationCancel(Animator animation) {
}
});
}
// 属性动画
public void click1(View view) {
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f,
0f, 1f);
// 属性动画Y值
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 0,
mScreenHeight / 2, 0);
// 属性动画X值
PropertyValuesHolder pvhXx = PropertyValuesHolder.ofFloat("x", 0,
mScreenHeight / 2, 0);
ObjectAnimator.ofPropertyValuesHolder(iv, pvhX, pvhY, pvhXx)
.setDuration(1000).start();
}
@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) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
界面代码:
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true" >
<Button
android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="动画" />
<Button
android:id="@+id/but1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click1"
android:text="动画2" />
</LinearLayout>
</RelativeLayout>
translationX and translationY
rotation, rotationX, and rotationY
scaleX and scaleY
pivotX and pivotY
x and y
alpha
主要是上诉几种代码效果,直接贴代码吧:
[java] view
plaincopy
package com.example.test;
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView iv;
private float mScreenHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
iv = (ImageView) findViewById(R.id.iv);
DisplayMetrics outMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
mScreenHeight = outMetrics.heightPixels;
}
public void click(View view) {
// 平移动画
ObjectAnimator transX = ObjectAnimator.ofFloat(iv, "translationX", 0f,
300f);
transX.setDuration(2000);
transX.setRepeatCount(5);
transX.setRepeatMode(ObjectAnimator.RESTART);
transX.start();
// 平移动画
ObjectAnimator transY = ObjectAnimator.ofFloat(iv, "translationY", 0f,
300f);
transY.setDuration(1000);
transY.setRepeatCount(3);
transY.setRepeatMode(ObjectAnimator.RESTART);
transY.start();
// 旋转动画
ObjectAnimator rotation = ObjectAnimator.ofFloat(iv, "rotation", 0f,
360f);
rotation.setDuration(1000);
rotation.setRepeatCount(7);
rotation.setRepeatMode(ObjectAnimator.RESTART);
rotation.start();
// 缩放
ObjectAnimator scaleX = ObjectAnimator.ofFloat(iv, "scaleX", 0f, 1.6f);
scaleX.setDuration(1000);
scaleX.setRepeatCount(5);
scaleX.setRepeatMode(ObjectAnimator.RESTART);
scaleX.start();
// 缩放动画
ObjectAnimator scaleY = ObjectAnimator.ofFloat(iv, "scaleY", 0f, 1.6f);
scaleY.setDuration(1000);
scaleY.setRepeatCount(5);
scaleY.setRepeatMode(ObjectAnimator.RESTART);
scaleY.start();
// 缩放动画
ObjectAnimator alpha = ObjectAnimator.ofFloat(iv, "alpha", 0f, 1f);
alpha.setDuration(1000);
alpha.setRepeatCount(5);
alpha.setRepeatMode(ObjectAnimator.RESTART);
alpha.start();
transX.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
Toast.makeText(MainActivity.this, "终于结束了...", Toast.LENGTH_LONG)
.show();
}
@Override
public void onAnimationCancel(Animator animation) {
}
});
}
// 属性动画
public void click1(View view) {
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f,
0f, 1f);
// 属性动画Y值
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 0,
mScreenHeight / 2, 0);
// 属性动画X值
PropertyValuesHolder pvhXx = PropertyValuesHolder.ofFloat("x", 0,
mScreenHeight / 2, 0);
ObjectAnimator.ofPropertyValuesHolder(iv, pvhX, pvhY, pvhXx)
.setDuration(1000).start();
}
@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) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
界面代码:
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true" >
<Button
android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="动画" />
<Button
android:id="@+id/but1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click1"
android:text="动画2" />
</LinearLayout>
</RelativeLayout>
相关文章推荐
- AS3动画效果公式,常用处理公式代码,基本运动公式,三角公式
- AS3动画效果公式,常用处理公式代码,基本运动公式,三角公式
- 安卓开发基础之tween动画基本使用,代码教学
- AS3动画效果公式,常用处理公式代码,基本运动公式,三角公式
- 安卓开发20:动画之Animation 详细使用-主要通过java代码实现动画效果
- Silverlight后台CS代码中创建四种常用的动画效果
- Silverlight实用窍门系列:20.后台CS代码中创建四种常用的动画效果【附带源码实例】
- 20.后台CS代码中创建四种常用的动画效果
- ASP.NET网站建设基本常用代码
- [ios开发基础知识之]界面常用基本动画
- Jquery选择器的概念以及选择器的学习一(基本、层级、简单,另有简单动画效果代码)
- ios常用的几个动画代码
- ASP.NET网站建设基本常用代码
- ios常用的几个动画代码
- ios常用的几个动画代码
- Silverlight后台CS代码中创建四种常用的动画效果
- flash动画制作常用代码
- ASP.NET网站建设基本常用代码
- .NET几个常用的基本概念--CLR\受管制代码\MSIL\CTS\CLS\JIT\GC
- 【代码片段】HTML5基本结构及常用默认模版