2013-07-20 79 views
3

目前,我有一个代表饼图的自定义视图。如何为自定义视图创建缩放动画效果

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个版本的饼图? (一个是缩小版本,另一个是正常大小版本)

回答

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的完整文档。