在定制的ViewGroup来看Android的转移动画 [英] Android transfer animation of view in custom viewgroup
问题描述
我要像动画在此照片的扩大,当我点击画廊的照片文件夹,像的视频Android的画廊。
-
我有相同的自定义两个视图的ViewGroup
- 厂景是0,0
- 视图2是100,100
-
自点击开始厂景将移动至100,0和视图2将移至0,100
我的解决方案至今:
我用定时器与requestlayout新的位置刷新布局。
的意见的位置将被刷新 onLayout
:
它的工作原理,但它不是一个原生的功能,它是用100浏览在同时移动很慢的。
全部code:
私有类MyViewGroup扩展的ViewGroup {
查看视图1,视图2;
按钮BTN;
公共MyViewGroup(上下文的背景下){
超级(上下文);
厂景=新景(背景);
view1.setBackgroundColor(Color.RED);
this.addView(视图1);
视图2 =新景(背景);
view2.setBackgroundColor(Color.BLUE);
this.addView(视图2);
BTN =新按钮(背景);
btn.setText(开始);
btn.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
xLayout = 0;
handler.sendEmptyMessage(0);
}
});
this.addView(BTN);
} @覆盖
保护无效onMeasure(INT widthMeasureSpec,诠释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);
}
私人INT xLayout = 0;
@覆盖
保护无效onLayout(布尔变化,诠释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);
} 私人无效startAnimation(){
定时器定时器=新的Timer();
timer.schedule(新的TimerTask(){
@覆盖
公共无效的run(){
如果(xLayout&小于100){
handler.sendEmptyMessage(0);
}
this.cancel();
} },5);
} 私人处理程序处理程序=新的处理程序(){
@覆盖
公共无效的handleMessage(消息MSG){
xLayout = xLayout + 20;
view1.requestLayout();
startAnimation();
}
};
}
的http://android-developers.blogspot.fr/2011/05/introducing-viewpropertyanimator.html
myView.animate()×(500).Y(500)。
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();
}
} ;
}
http://android-developers.blogspot.fr/2011/05/introducing-viewpropertyanimator.html
myView.animate().x(500).y(500);
这篇关于在定制的ViewGroup来看Android的转移动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!