Android transfer animation of view in custom viewgroup

ⅰ亾dé卋堺 提交于 2020-02-29 07:03:07

问题


I want animation like expanding of the photos when I click folder of photos at gallery, like in this video about Android gallery.

  • i have two views in same custom viewgroup

    • view1 is in 0,0
    • view2 is in 100,100
  • since click "start" view1 will move to 100,0 and view2 will move to 0,100

My solution so far:

I use timer for refresh layout with new position by requestlayout.

the views' position will refresh by onLayout:

It works but it's not a native function and it is very slow with 100 views moving at same time.

Full code:

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

回答1:


http://android-developers.blogspot.fr/2011/05/introducing-viewpropertyanimator.html

myView.animate().x(500).y(500);



回答2:


we can use "LayoutTransition" as

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

and when we click "start" addview

full code

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


}


来源:https://stackoverflow.com/questions/10683032/android-transfer-animation-of-view-in-custom-viewgroup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!