Android缩放动画视图
我想使用ScaleAnimation(以编程方式而不是xml)将视图的高度从父高度的0%更改为60%。视图的宽度是恒定的,为50px。视图为空,仅设置了背景色 有人能给我使用代码中的ScaleAnimation的Android缩放动画视图,android,animation,Android,Animation,我想使用ScaleAnimation(以编程方式而不是xml)将视图的高度从父高度的0%更改为60%。视图的宽度是恒定的,为50px。视图为空,仅设置了背景色 有人能给我使用代码中的ScaleAnimation的scaleAnim代码吗 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_w
scaleAnim
代码吗
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layContainer
>
<View
android:layout_width="50px"
android:layout_height="fill_parent"
android:id="@+id/viewContainer"
android:background:"#00f00"
/>
</LinearLayout>
ScaleAnimation scaleAnim = new ScaleAnimation(...);
尝试使用此代码不使用xml创建缩放动画
ScaleAnimation animation = new ScaleAnimation(fromXscale, toXscale, fromYscale, toYscale, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
在XML中,这就是我用来实现相同结果的方法。也许这更直观
放大.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="200"
android:fromXScale="1.0"
android:fromYScale="0.0"
android:pivotX="50%"
android:pivotY="100%"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="200"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="100%"
android:toXScale="1.0"
android:toYScale="0.0" />
</set>
请看,X轴上的动画是从1.0->1.0
开始的,这意味着您在该方向上没有任何缩放,并且保持在全宽,而在Y轴上,您得到0.0->1.0
缩放,如问题中的图形所示。希望这对别人有帮助
有些人可能想知道我们所要求的java代码。
将动画文件放在anim
文件夹中,然后加载并设置类似的动画文件
Animation scaleDown = AnimationUtils.loadAnimation(youContext, R.anim.scale_down);
ImagView v = findViewById(R.id.your_image_view);
v.startAnimation(scaleDown);
这里有一个代码剪报来实现这一点
public void scaleView(View v, float startScale, float endScale) {
Animation anim = new ScaleAnimation(
1f, 1f, // Start and end values for the X axis scaling
startScale, endScale, // Start and end values for the Y axis scaling
Animation.RELATIVE_TO_SELF, 0f, // Pivot point of X scaling
Animation.RELATIVE_TO_SELF, 1f); // Pivot point of Y scaling
anim.setFillAfter(true); // Needed to keep the result of the animation
anim.setDuration(1000);
v.startAnimation(anim);
}
这里使用的ScaleAnimation构造函数包含8个参数,4个与处理我们不关心的X-scale相关的参数(1f,1f,…Animation.RELATIVE_to_SELF,0f,…)
其他4个参数用于我们确实关心的Y缩放
startScale,endScale
-在您的情况下,您将使用0f,0.6f
Animation.RELATIVE_TO_SELF,1f
-指定视图收缩折叠到的位置(在文档中称为轴)。在这里,我们将float值设置为1f
,因为我们希望动画从底部开始增长条。如果我们希望它从顶部向下增长,我们会使用0f
最后,同样重要的是调用anim.setFillAfter(true)
。如果希望动画结果在动画完成后保持不变,则必须在执行动画之前在动画制作程序上运行此操作
所以在你的情况下,你可以这样做:
View v = findViewById(R.id.viewContainer);
scaleView(v, 0f, .6f);
resize(
view1,
1.0f,
0.0f,
1.0f,
0.0f,
0.0f,
0.0f,
150,
null,
null,
null);
return null;
}
使用辅助方法和开始-重复-结束处理程序调整大小,如下所示:
View v = findViewById(R.id.viewContainer);
scaleView(v, 0f, .6f);
resize(
view1,
1.0f,
0.0f,
1.0f,
0.0f,
0.0f,
0.0f,
150,
null,
null,
null);
return null;
}
助手方法:
/**
* Resize a view.
*/
public static void resize(
View view,
float fromX,
float toX,
float fromY,
float toY,
float pivotX,
float pivotY,
int duration) {
resize(
view,
fromX,
toX,
fromY,
toY,
pivotX,
pivotY,
duration,
null,
null,
null);
}
/**
* Resize a view with handlers.
*
* @param view A view to resize.
* @param fromX X scale at start.
* @param toX X scale at end.
* @param fromY Y scale at start.
* @param toY Y scale at end.
* @param pivotX Rotate angle at start.
* @param pivotY Rotate angle at end.
* @param duration Animation duration.
* @param start Actions on animation start. Otherwise NULL.
* @param repeat Actions on animation repeat. Otherwise NULL.
* @param end Actions on animation end. Otherwise NULL.
*/
public static void resize(
View view,
float fromX,
float toX,
float fromY,
float toY,
float pivotX,
float pivotY,
int duration,
Callable start,
Callable repeat,
Callable end) {
Animation animation;
animation =
new ScaleAnimation(
fromX,
toX,
fromY,
toY,
Animation.RELATIVE_TO_SELF,
pivotX,
Animation.RELATIVE_TO_SELF,
pivotY);
animation.setDuration(
duration);
animation.setInterpolator(
new AccelerateDecelerateInterpolator());
animation.setFillAfter(true);
view.startAnimation(
animation);
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
if (start != null) {
try {
start.call();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void onAnimationEnd(Animation animation) {
if (end != null) {
try {
end.call();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void onAnimationRepeat(
Animation animation) {
if (repeat != null) {
try {
repeat.call();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
使用此方法(无需创建xml文件)
如果您希望按比例缩放到四分之一(半x,半y)
如果要缩放,请移动到右下角
view.animate().scaleX(0.5f).scaleY(0.5f)
.translationY((view.height/4).toFloat()).translationX((view.width/4).toFloat())
如果要移动到顶部请使用(-view.height/4)
,对于左侧(-view.width/4)
如果要在动画结束后执行操作,请使用withEndAction(Runnable Runnable)
函数
您可以使用一些其他属性,如alpha和旋转
完整代码
view.animate()
.scaleX(0.5f).scaleY(0.5f)//scale to quarter(half x,half y)
.translationY((view.height/4).toFloat()).translationX((view.width/4).toFloat())// move to bottom / right
.alpha(0.5f) // make it less visible
.rotation(360f) // one round turns
.setDuration(1000) // all take 1 seconds
.withEndAction(new Runnable() {
@Override
public void run() {
//animation ended
}
});
在值动画中添加此代码
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="@android:integer/config_longAnimTime"
android:fromXScale="0.2"
android:fromYScale="0.2"
android:toXScale="1.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"/>
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="@android:integer/config_longAnimTime"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"/>
</set>
方法上的设置:
alertDialog.getWindow().getAttributes().windowAnimations = type;
感谢您的回答,但是
(from,to,from,to,Animation.RELATIVE_to_SELF,(float)0.5,Animation.RELATIVE_to_SELF,(float)0.5)
是我的问题:)我真正的问题是变量的xscale、toXscale、fromYscale、toYscale和pivot值,谢谢为什么动画也会旋转视图?我不想让它尽可能高效,因此,我想使用ViewPropertyAnimator。如何使用ViewPropertyAnimator执行此操作?我不知道如何正确指定动画的起始位置,相对定位似乎是不可能的(这是最好的,因为这样我可以重用我的一些复杂的坐标)展开展开展开展开展开展开?比其他解决方案更简单:)
public void onClick(View v) {
fab_onclick(R.style.DialogScale, "Scale" ,(Activity) context,getWindow().getDecorView().getRootView());
// Dialogs.fab_onclick(R.style.DialogScale, "Scale");
}
alertDialog.getWindow().getAttributes().windowAnimations = type;