如何为自定义视图创建缩放动画效果

时间:2013-07-20 18:21:21

标签: android

目前,我有一个代表饼图的自定义视图。

public class PieChart extends View {
    @Override
    protected void onDraw(Canvas canvas) {

使用以下布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    ... >

    <org.yccheok.jstock.gui.charting.PieChart

看起来像这样(自定义饼图视图是上半部分视图,其宽度为fill_parent,高度为父级的50%)

enter image description here

当活动开始时,我期待有一个与此类似的饼图缩放动画

http://www.youtube.com/watch?v=uwGoSswCZhQ

我读过http://stuff.mit.edu/afs/sipb/project/android/docs/training/animation/zoom.html。该示例使用2个图像(一个是缩小版本,另一个是按比例缩放版本)来实现这种效果。

我不确定如何在我的情况下应用它们?我是否还需要构建2个版本的饼图? (一个是缩小版,另一个是正常尺寸版本)

1 个答案:

答案 0 :(得分:2)

您可以使用优质的ScaleAnimation来实现此效果。这是一个将视图从20%缩放到100%的示例。比例原点设置为视图的中心。

ScaleAnimation scaleAnimation = new ScaleAnimation(0.2f, 1f, 0.2f, 1f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
        ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(600);
pieChart.startAnimation(set);

有关完整文档,请参阅http://developer.android.com/reference/android/view/animation/ScaleAnimation.html

相关问题