Jov*_*van 53 animation android
我想使用ScaleAnimation(以编程方式不在xml中)更改高度以查看父高度的0到60%.视图宽度恒定,为50px.视图为空,仅设置背景颜色.
有人可以给我代码scaleAnim从代码中使用ScaleAnimation.
<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(...);
Run Code Online (Sandbox Code Playgroud)

在动画之前和之后查看.谢谢
Jaz*_*zer 115
这是一个完全相同的代码片段.
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);
}
Run Code Online (Sandbox Code Playgroud)
这里使用的ScaleAnimation构造函数需要8个args,4个与处理我们不关心的X-scale相关(1f, 1f, ... Animation.RELATIVE_TO_SELF, 0f, ...).
其他4个参数用于我们关心的Y缩放.
startScale, endScale- 在你的情况下,你会使用0f, 0.6f.
Animation.RELATIVE_TO_SELF, 1f - 指定视图收缩的位置(在文档中称为数据透视).在这里,我们将浮点值设置为,1f因为我们希望动画从底部开始增长条形.如果我们希望它从顶部向下增长,我们就会使用0f.
最后,同样重要的是呼吁anim.setFillAfter(true).如果希望动画结果在动画完成后保持不变,则必须在执行动画之前在动画制作器上运行此动画.
所以在你的情况下,你可以做这样的事情:
View v = findViewById(R.id.viewContainer);
scaleView(v, 0f, .6f);
Run Code Online (Sandbox Code Playgroud)
ila*_*o j 48
尝试使用此代码创建不使用xml的Scale动画
ScaleAnimation animation = new ScaleAnimation(fromXscale, toXscale, fromYscale, toYscale, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
Run Code Online (Sandbox Code Playgroud)
Sub*_*ian 45
在XML中,这是我用来实现相同结果的东西.可能这更直观了.
scale_up.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>
Run Code Online (Sandbox Code Playgroud)
scale_down.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="1.0"
android:pivotX="50%"
android:pivotY="100%"
android:toXScale="1.0"
android:toYScale="0.0" />
</set>
Run Code Online (Sandbox Code Playgroud)
查看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);
Run Code Online (Sandbox Code Playgroud)
Rad*_*esh 10
Use this method (No need to xml file)
If you want scale to quarter(half x,half y)
view.animate().scaleX(0.5f).scaleY(0.5f)
Run Code Online (Sandbox Code Playgroud)
If you want scale and move to bottom right
view.animate().scaleX(0.5f).scaleY(0.5f)
.translationY((view.height/4).toFloat()).translationX((view.width/4).toFloat())
Run Code Online (Sandbox Code Playgroud)
If you want move to top use (-view.height/4) and for left (-view.width/4)
If you want do something after animation ends use withEndAction(Runnable runnable) function.
您可以使用其他一些属性,例如alpha和rotation
完整代码
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
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
135517 次 |
| 最近记录: |