2012-05-21 155 views
0

当我点击图库中的照片文件夹时,我需要展开照片等动画,就像在video about Android gallery中一样。自定义视图组中的视图的Android传输动画

  • 我有相同的自定义的ViewGroup

    • 厂景两种观点是0,0
    • 视图2是在100,100
  • 因为点击 “开始” 厂景将移师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(); 
     } 
    } ; 
} 
+0

我们可以使用LayoutTransition或myView.animate()。x(500).y(500); – eyeeye

回答

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); 
    } 


}