你也可以这样做:аможноещетак:
public class MyTranslateAnimation extends TranslateAnimation {
private long mTimePause, mTimeTotal;
private boolean mPause;
public MyTranslateAnimation(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean getTransformation(long currentTime, Transformation outTransformation) {
updateTime(currentTime);
return super.getTransformation(mTimeTotal - mTimePause, outTransformation);
}
private void updateTime(long currentTime) {
long dt = currentTime - mTimeTotal;
mTimeTotal += dt;
if (mPause) {
mTimePause += dt;
}
}
public void pause() {
mPause = true;
}
public void resume() {
mPause = false;
}
}
要从 XML 创建动画,您可以创建自己的AnimationUtils
子类,如下所示:для создания анимации из XML можно сделать свой AnimationUtils:
public class MyAnimationUtils {
public static Animation loadAnimation(Context context, int id) throws Resources.NotFoundException {
XmlResourceParser parser = null;
try {
parser = context.getResources().getAnimation(id);
return createAnimationFromXml(context, parser);
} catch (XmlPullParserException ex) {
Resources.NotFoundException rnf = new Resources.NotFoundException("Can't load animation resource ID #0x" + Integer.toHexString(id));
rnf.initCause(ex);
throw rnf;
} catch (IOException ex) {
Resources.NotFoundException rnf = new Resources.NotFoundException("Can't load animation resource ID #0x" + Integer.toHexString(id));
rnf.initCause(ex);
throw rnf;
} finally {
if (parser != null) parser.close();
}
}
private static Animation createAnimationFromXml(Context c, XmlPullParser parser) throws XmlPullParserException, IOException {
return createAnimationFromXml(c, parser, null, Xml.asAttributeSet(parser));
}
private static Animation createAnimationFromXml(Context c, XmlPullParser parser, AnimationSet parent, AttributeSet attrs) throws XmlPullParserException, IOException {
Animation anim = null;
// Make sure we are on a start tag.
int type;
int depth = parser.getDepth();
while (((type=parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
&& type != XmlPullParser.END_DOCUMENT) {
if (type != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("set")) {
anim = new AnimationSet(c, attrs);
createAnimationFromXml(c, parser, (AnimationSet)anim, attrs);
} else if (name.equals("alpha")) {
anim = new AlphaAnimation(c, attrs);
} else if (name.equals("scale")) {
anim = new ScaleAnimation(c, attrs);
} else if (name.equals("rotate")) {
anim = new RotateAnimation(c, attrs);
} else if (name.equals("translate")) {
//anim = new TranslateAnimation(c, attrs);
anim = new MyTranslateAnimation(c, attrs); // отредактировали только эту строчку, остальное взяли как было
} else {
throw new RuntimeException("Unknown animation name: " + parser.getName());
}
if (parent != null) {
parent.addAnimation(anim);
}
}
return anim;
}
}
然后你像这样构建动画:и вот так создаем анимацию:
MyTranslateAnimation cloud1 = (MyTranslateAnimation) MyAnimationUtils.loadAnimation(this, R.anim.main_cloud1);
希望这可以帮助。Пользуйтесь наздоровье!