原创:android模仿iphone 腾讯爱看文章列表放大缩小 带动画
16lz
2021-12-04
产品一直拿着iphone app应用的效果,来强行让android实现同样的功能,android码农表示很悲催。
看效果:(以图片放大缩小为例)
packagecontrols;
importjava.text.DateFormat;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importInterface.ScalingChangeListener;
importandroid.os.Handler;
importandroid.view.View;
importandroid.widget.AbsListView;
importandroid.widget.FrameLayout;
importandroid.widget.RelativeLayout;
importandroid.widget.TextView;
importandroid.widget.Toast;
public classScalingBig{
privateScalingChangeListenerchangeListener;
publicScalingChangeListenergetChangeListener(){
returnchangeListener;
}
public voidsetChangeListener(ScalingChangeListenerchangeListener){
this.changeListener=changeListener;
}
Handlerhandel= newHandler();
intoldHeight=0;
intnewHeight=0;
intcurrentHeight=0;
inttopMargin=0; // 根View可变
inttopHeight=0; // 根view高度不可变
ViewrootView;
Viewview;
RelativeLayout.LayoutParamsrowViewParams;
RelativeLayout.LayoutParamsrootViewParas;
inttopTime; // 向上移动的次数
inttopStep; // 向上移动每次的步长
inttimeHeight=100; // 每次设置高度的步长
publicScalingBig(ViewrootView,RelativeLayout.LayoutParamsrootViewParas,
Viewview,RelativeLayout.LayoutParamsrowViewParams
, inttopHeight, intoldHeight, intnewHeight){
this.view=view;
this.oldHeight=oldHeight;
this.currentHeight=oldHeight;
this.newHeight=newHeight;
this.rowViewParams=rowViewParams;
this.rootViewParas=rootViewParas;
this.rootView=rootView;
this.topHeight=topHeight;
topTime=(newHeight-oldHeight)%timeHeight==0?(newHeight-oldHeight)/timeHeight:(newHeight-oldHeight)/timeHeight+1;
topStep=topHeight%topTime==0?topHeight/topTime:topHeight/topTime+1;
}
public voidstartScaling(){
handel.post(run);
}
privateRunnablerun= newRunnable(){
public voidrun(){
if(rowViewParams.height>=newHeight){
handel.removeCallbacks(run);
if(changeListener!= null){
changeListener.finish();
}
} else{
currentHeight=currentHeight+timeHeight;
if(topHeight>0){
topMargin=topMargin+topStep;
if(topMargin>topHeight)topMargin=topHeight;
rootViewParas.topMargin=-topMargin;
rootView.requestLayout();
rootView.setLayoutParams(rootViewParas);
}
rowViewParams.height=currentHeight;
System.out.println("currentHeight:"+currentHeight+"rootViewParas.topMargin:"+-topMargin);
// view.setLayoutParams(rowViewParams);
view.requestLayout();
handel.postDelayed(run,1);
}
}
};
} packagecom.list;
importjava.util.ArrayList;
importcontrols.ScalingBig;
importcontrols.ScalingSmall;
importInterface.ScalingChangeListener;
importandroid.app.Activity;
importandroid.graphics.Rect;
importandroid.os.Bundle;
importandroid.util.DisplayMetrics;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.view.animation.Animation;
importandroid.view.animation.AnimationSet;
importandroid.view.animation.ScaleAnimation;
importandroid.widget.AbsListView;
importandroid.widget.Button;
importandroid.widget.FrameLayout;
importandroid.widget.HorizontalScrollView;
importandroid.widget.ImageView;
importandroid.widget.ListView;
importandroid.widget.RelativeLayout;
importandroid.widget.ScrollView;
importandroid.widget.AdapterView.OnItemClickListener;
public classScaleViewActivity extendsActivity implementsView.OnClickListener{
privateImageViewimg1;
privateImageViewimg2;
privateImageViewimg3;
privateImageViewimg4;
privateScrollViewrootLayout;
private enumItemState{change,finish};
private boolean[]listitemCheck= new boolean[4];
private int[]listitemheight= new int[4];
privateItemState[]listitemState= newItemState[4];
intScreenHeight;
@Override
public voidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.scaleview);
DisplayMetricsdm= newDisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
ScreenHeight=dm.heightPixels;
img1=(ImageView)findViewById(R.id.img1);
img2=(ImageView)findViewById(R.id.img2);
img3=(ImageView)findViewById(R.id.img3);
img4=(ImageView)findViewById(R.id.img4);
rootLayout=(ScrollView)findViewById(R.id.rootLayout);
for( inti=0;i<4;i++){
listitemCheck[i]= false;
listitemheight[i]=0;
listitemState[i]=ItemState.finish;
}
img1.setOnClickListener( this);
img2.setOnClickListener( this);
img3.setOnClickListener( this);
img4.setOnClickListener( this);
}
@Override
public voidonClick(Viewv){
// Rectframe=newRect();
// getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
// intstatusBarHeight=frame.top; // 状态栏高度
//// ScreenHeight=ScreenHeight-statusBarHeight;
// ScreenHeight=frame.bottom-statusBarHeight;
switch(v.getId()){
caseR.id.img1:
if(listitemState[0]==ItemState.finish){
listitemState[0]=ItemState.change;
scaleImageView(img1,0,0);
}
break;
caseR.id.img2:
if(listitemState[1]==ItemState.finish){
listitemState[1]=ItemState.change;
scaleImageView(img2,1,img1.getHeight());
}
break;
caseR.id.img3:
if(listitemState[2]==ItemState.finish){
listitemState[2]=ItemState.change;
scaleImageView(img3,2,img1.getHeight()+img2.getHeight());
}
break;
caseR.id.img4:
if(listitemState[3]==ItemState.finish){
listitemState[3]=ItemState.change;
scaleImageView(img4,3,img1.getHeight()+img2.getHeight()+img3.getHeight());
}
break;
}
}
private voidscaleImageView(ImageViewimg, inti, inttopHeight){
final intnIndex=i;
RelativeLayout.LayoutParamsrootViewParas=(RelativeLayout.LayoutParams)rootLayout.getLayoutParams();
RelativeLayout.LayoutParamsimgViewParams=(RelativeLayout.LayoutParams)img.getLayoutParams();
intheight=0;
ScalingBigbig= null;
ScalingSmallsmall= null;
if(!listitemCheck[nIndex]== true){
height=imgViewParams.height;
listitemheight[nIndex]=height;
listitemCheck[nIndex]= true;
big= newScalingBig(rootLayout,rootViewParas,img,imgViewParams,topHeight,height,ScreenHeight);
big.setChangeListener( newScalingChangeListener(){
@Override
public voidfinish(){
listitemState[nIndex]=ItemState.finish;
}
});
big.startScaling();
} else{
listitemCheck[nIndex]= false;
small= newScalingSmall(rootLayout,rootViewParas,img,imgViewParams,topHeight,ScreenHeight,
listitemheight[nIndex]);
small.setChangeListener( newScalingChangeListener(){
@Override
public voidfinish(){
listitemState[nIndex]=ItemState.finish;
}
});
small.startScaling();
}
}
}
源码:
/Files/neil-zhao/myScaleList.rar
更多相关文章
- android 计量单位
- android Progressbar使用
- 如何去掉ListView底部的ListDivider
- android默认字体大小、高度、宽度
- 获取Android(安卓)手机屏幕宽度和高度以及获取Android手机序列号
- Android(安卓)图片缩放
- 如何去掉ListView底部的ListDivider
- android获得屏幕高度和宽度
- Android(安卓)获取标题栏的高度