2012-05-21 81 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); 
    } 


}