0

当我单击画廊中的照片文件夹时,我想要像扩展照片这样的动画,就像在这个关于 Android 画廊的视频中一样。

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

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

2 回答 2

1

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

myView.animate().x(500).y(500);
于 2012-05-23T07:35:56.383 回答
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);
    }


}
于 2012-05-22T10:25:24.573 回答