原創:android模仿iphone 騰訊愛看文章列表放大縮小 帶動畫


產品一直拿着iphone app應用的效果,來強行讓android實現同樣的功能,android碼農表示很悲催。

 

看效果:(以圖片放大縮小為例)

          

 

package controls;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import Interface.ScalingChangeListener;
import android.os.Handler;
import android.view.View;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public  class ScalingBig {
    
     private ScalingChangeListener changeListener;
    
     public ScalingChangeListener getChangeListener() {
         return changeListener;
    }

     public  void setChangeListener(ScalingChangeListener changeListener) {
         this.changeListener = changeListener;
    }

    Handler handel =  new Handler();
     int oldHeight=0;
     int newHeight=0;
    
     int currentHeight=0;
    
     int topMargin=0; // 根View 可變
     int topHeight=0;  // 根view高度 不可變
    View rootView;
    View view;
    RelativeLayout.LayoutParams rowViewParams;
    RelativeLayout.LayoutParams rootViewParas;
     int topTime; // 向上移動的次數
     int topStep; // 向上移動每次的步長
    
     int timeHeight=100; // 每次設置高度的步長
    
     public ScalingBig(View rootView,RelativeLayout.LayoutParams rootViewParas,
            View view,RelativeLayout.LayoutParams rowViewParams
            , int topHeight, int oldHeight, int newHeight){
         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  void startScaling(){
        handel.post(run);
    }
    
     private Runnable run =  new Runnable() {
        
         public  void run() {
             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);
            }
        }
    };
}

 

package com.list;

import java.util.ArrayList;

import controls.ScalingBig;
import controls.ScalingSmall;
import Interface.ScalingChangeListener;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.AdapterView.OnItemClickListener;

public  class ScaleViewActivity  extends Activity  implements View.OnClickListener{
    
     private ImageView img1;
     private ImageView img2;
     private ImageView img3;
     private ImageView img4;
     private ScrollView rootLayout;
     private  enum ItemState{change,finish};
    
     private  boolean[] listitemCheck= new  boolean[4];
     private  int[] listitemheight= new  int[4];
     private ItemState[] listitemState= new ItemState[4];
    
     int ScreenHeight;

    @Override
     public  void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        setContentView(R.layout.scaleview);
        
        DisplayMetrics dm =  new DisplayMetrics();
         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 ( int i = 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  void onClick(View v) {
         // Rect frame = new Rect();  
        
// getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  
        
// int statusBarHeight = frame.top;   // 狀態欄高度
         //// ScreenHeight=ScreenHeight-statusBarHeight;
        
// ScreenHeight=frame.bottom-statusBarHeight;
        
         switch(v.getId()){
             case R.id.img1:
                 if(listitemState[0]==ItemState.finish){
                    listitemState[0]=ItemState.change;
                    scaleImageView(img1,0,0);    
                }
                 break;
             case R.id.img2:
                 if(listitemState[1]==ItemState.finish){
                    listitemState[1]=ItemState.change;
                    scaleImageView(img2,1,img1.getHeight());    
                }
                 break;
             case R.id.img3:
                 if(listitemState[2]==ItemState.finish){
                    listitemState[2]=ItemState.change;
                    scaleImageView(img3,2,img1.getHeight()+img2.getHeight());    
                }
                 break;
             case R.id.img4:
                 if(listitemState[3]==ItemState.finish){
                    listitemState[3]=ItemState.change;
                    scaleImageView(img4,3,img1.getHeight()+img2.getHeight()+img3.getHeight());    
                }
                 break;
        }
    }
    
     private  void scaleImageView(ImageView img, int i, int topHeight){
         final  int nIndex=i;
        RelativeLayout.LayoutParams rootViewParas=(RelativeLayout.LayoutParams)rootLayout.getLayoutParams();
        RelativeLayout.LayoutParams imgViewParams=(RelativeLayout.LayoutParams)img.getLayoutParams();
         int height=0;
        ScalingBig big= null;
        ScalingSmall small= null;        
         if(!listitemCheck[nIndex]== true){
            height=imgViewParams.height;
            listitemheight[nIndex]=height;
            listitemCheck[nIndex]= true;
            big= new ScalingBig(rootLayout,rootViewParas,img,imgViewParams,topHeight,height,ScreenHeight);
            big.setChangeListener( new ScalingChangeListener(){
                @Override
                 public  void finish() {
                    listitemState[nIndex]=ItemState.finish;
                }
            });
            big.startScaling();    
        } else{
            listitemCheck[nIndex]= false;
            small= new ScalingSmall(rootLayout,rootViewParas,img,imgViewParams,topHeight,ScreenHeight,
                    listitemheight[nIndex]);
            small.setChangeListener( new ScalingChangeListener(){
                @Override
                 public  void finish() {
                    listitemState[nIndex]=ItemState.finish;
                }
            });
            small.startScaling();    
        }
    }
}

 

 

 

源碼:

/Files/neil-zhao/myScaleList.rar

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM