我想在Android上创建一个非常具体的动画。
动画如下所示:
我的第一个想法是获得2个图像,一个来自Pizza,另一个来自包装,并像SVG中的clipPath一样动画片段的旋转。
你能给我一些提示吗?
谢谢!
答案 0 :(得分:1)
您可以将<animation-list>
可绘制XML与AnimationDrawable
一起使用:
private ImageButton runSim;
@Override
protected void onCreate(final Bundle savedInstanceState) {
runSim = (ImageButton) findViewById(R.id.runSim);
//....
Tools.executeAsyncTask(new RunSimAnimationTask());
//....
}
private boolean simExecuted = false;
private AnimationDrawable runSimAnimation = null;
private class RunSimAnimationTask extends AsyncTask<Void, Void, AnimationDrawable>{
@Override
protected AnimationDrawable doInBackground(Void... voids) {
AnimationDrawable animations = new AnimationDrawable();
animations = (AnimationDrawable) getResources().getDrawable(R.drawable.animate_sim);
animations.setOneShot(false);
return animations;
}
@Override
protected void onPostExecute(AnimationDrawable animationDrawables) {
super.onPostExecute(animationDrawables);
runSimAnimation = animationDrawables;
registerRunSimClickListener();
}
}
private void registerRunSimClickListener() {
runSim.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
v.performClick();
}
else {
}
return true;
}
});
runSim.setOnClickListener(new OnClickListener(){
@Override
public void onClick(final View v) {
MainChatController.this.runOnUiThread(new Runnable() {
@Override
public void run() {
simExecuted = !simExecuted;
if (simExecuted) {
startSimulationAnimation();
}
else {
stopSimulationAnimation();
}
}
});
}
});
}
public void startSimulationAnimation() {
MainChatController.this.runOnUiThread(new Runnable() {
@Override
public void run() {
runSim.setImageDrawable(runSimAnimation);
runSimAnimation.start();
}
});
}
public void stopSimulationAnimation() {
MainChatController.this.runOnUiThread(new Runnable() {
@Override
public void run() {
runSim.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_data_usage));
runSimAnimation.stop();
}
});
}
Tools.executeAsyncTask():
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
static public <T> void executeAsyncTask(AsyncTask<T, ?, ?> task, T... params) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
}
else {
task.execute(params);
}
}
RES /抽拉/ animate_sim.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- the animation list must have more than one item -->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/animate_sim_rotation" android:duration="0" />
<item android:drawable="@drawable/animate_sim_rotation" android:duration="2000" />
</animation-list>
上面的<animation-list>
会为下面的可绘制<animated-rotate>
制作动画:
RES /抽拉/ animate_sim_rotation.xml:
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/ic_action_data_usage"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="restart"
android:interpolator="@android:anim/linear_interpolator"/>
但是,您可以将<animation-list>
中的项目替换为连续帧中的实际图像文件。