San*_*ese 29 android android-animation objectanimator
我正在使用objectAnimator在Android中从下到上动画一个按钮.现在我使用下面的代码
ObjectAnimator transAnimation = ObjectAnimator.ofFloat(button,"translationY",0,440);
transAnimation.setDuration(440);
transAnimation.start();
Run Code Online (Sandbox Code Playgroud)
我也试过下面的示例代码.但问题仍然存在
ObjectAnimator transAnimation = ObjectAnimator.ofFloat(loginLayout, "translationY",0f,0.8f);
transAnimation.setDuration(480);
transAnimation.start();
Run Code Online (Sandbox Code Playgroud)
它适用于大屏幕设备.但是当谈到小屏幕设备时,它就会离开屏幕.无论屏幕大小不同,我都希望将其保留在屏幕顶部.我想我必须给出百分比值(比如0%到100%或0到100%p).所以我的问题是如何在Android中的objectAnimator中给出百分比值.我还注意到这个objectAnimator只在HoneyComb中引入了一件事.那么是否有任何向后兼容库可以在低版本中运行它.任何人都可以指导我找到解决方案.
我也试过扩展View并为该属性编写getter和setter offset().它仍然没有完全移动到屏幕顶部.这是我用过的代码.
@SuppressLint("NewApi") public float getXFraction()
{
int width = context.getWindowManager().getDefaultDisplay().getHeight();
return (width == 0) ? 0 : (getY());
}
@SuppressLint("NewApi") public void setXFraction(float xFraction) {
int width = context.getWindowManager().getDefaultDisplay().getWidth();
setY((width > 0) ? (width) : 0);
}
Run Code Online (Sandbox Code Playgroud)
提前致谢
vas*_*art 10
要ObjectAnimator在预蜂窝设备上使用,请在项目中添加NineOldAndroids库并更改要使用的导入com.nineoldandroids.animation.ObjectAnimator.
在使用百分比值ObjectAnimator,而不是getXFraction和setXFraction你有添加getYFraction和setYFraction这样的方法
public float getYFraction() {
final WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
int height = wm.getDefaultDisplay().getHeight();
return (height == 0) ? 0 : getY() / (float) height;
}
public void setYFraction(float yFraction) {
final WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
int height = wm.getDefaultDisplay().getHeight();
setY((height > 0) ? (yFraction * height) : 0);
}
Run Code Online (Sandbox Code Playgroud)
然后你可以project-folder/res/animator/move_bottom.xml像这样创建xml文件
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:propertyName="yFraction"
android:valueFrom="0"
android:valueTo="0.8"
android:valueType="floatType" />
Run Code Online (Sandbox Code Playgroud)
或者在代码中创建动画
final LoginLayout loginLayout = (LoginLayout) findViewById(R.id.login_layout);
final ObjectAnimator oa = ObjectAnimator.ofFloat(loginLayout, "yFraction", 0f, 0.8f);
oa.start();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23686 次 |
| 最近记录: |