Android在自定义视图组中传输视图的动画

时间:2012-05-21 09:52:01

标签: android animation layout transfer viewgroup

当我点击图库中的照片文件夹时,我希望动画像扩展照片,就像在video about Android gallery中一样。

  • 我在同一自定义视图组中有两个视图

    • view1在0,0
    • view2在100,100
  • 因为点击“开始”view1将移至100,0而view2将移至0,100

到目前为止我的解决方案:

我通过requestlayout使用计时器进行刷新布局和新位置。

视图的位置将按onLayout刷新:

它可以工作,但它不是原生功能,并且在100个视图同时移动的情况下非常慢。

完整代码:

private class MyViewGroup extends ViewGroup{
    View view1,view2;
    Button btn;
    public MyViewGroup(Context context) {
        super(context);
        view1=new View(context);
        view1.setBackgroundColor(Color.RED);
        this.addView(view1);
        view2=new View(context);
        view2.setBackgroundColor(Color.BLUE);
        this.addView(view2);
        btn=new Button(context);
        btn.setText("start");
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                xLayout=0;
                handler.sendEmptyMessage(0);
            }
        });
        this.addView(btn);


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int w = MeasureSpec.getSize(widthMeasureSpec);
        int h= MeasureSpec.getSize(heightMeasureSpec);
        int widthSpec = MeasureSpec.makeMeasureSpec(50, MeasureSpec.EXACTLY);
        int heightSpec = MeasureSpec.makeMeasureSpec(50, MeasureSpec.EXACTLY);
        view1.measure(widthSpec,heightSpec);
        view2.measure(widthSpec,heightSpec);
        btn.measure(widthSpec,heightSpec);
        this.setMeasuredDimension(w, h);
    }
    private int xLayout=0;
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        view1.layout(xLayout,0,xLayout+50,50);
        view2.layout(100-xLayout,100,150-xLayout,150);
        btn.layout(0,200,50,250);
    }

    private void startAnimation(){
        Timer timer = new Timer() ;
        timer.schedule(new TimerTask(){
            @Override
            public void run() {
                if(xLayout<100){
                    handler.sendEmptyMessage(0);
                }
                this.cancel();
            }

        },5);
    }

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            xLayout=xLayout+20;
            view1.requestLayout();
            startAnimation();
        }
    }   ;
}

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

我们可以使用“LayoutTransition”作为

 final LayoutTransition transitioner = new LayoutTransition();
 myViewGroup.setLayoutTransition(transitioner);

当我们点击“开始”addview

完整代码

 private class MyViewGroup extends ViewGroup{
    View view1,view2;
    Button btn;
    public MyViewGroup(final Context context) {
        super(context);
        view1=new View(context);
        view1.setBackgroundColor(Color.RED);
        this.addView(view1);
        view2=new View(context);
        view2.setBackgroundColor(Color.BLUE);
        this.addView(view2);
        btn=new Button(context);
        btn.setText("start");
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                xLayout+=100;
              MyViewGroup.this.addView(new View(context));
            }
        });
        this.addView(btn);


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int w = MeasureSpec.getSize(widthMeasureSpec);
        int h= MeasureSpec.getSize(heightMeasureSpec);
        int widthSpec = MeasureSpec.makeMeasureSpec(50, MeasureSpec.EXACTLY);
        int heightSpec = MeasureSpec.makeMeasureSpec(50, MeasureSpec.EXACTLY);
        view1.measure(widthSpec,heightSpec);
        view2.measure(widthSpec,heightSpec);
        btn.measure(widthSpec,heightSpec);
        this.setMeasuredDimension(w, h);
    }
    private int xLayout=0;
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        view1.layout(xLayout,0,xLayout+50,50);
        view2.layout(100-xLayout,100,150-xLayout,150);
        btn.layout(0,200,50,250);
    }


}