Android特效第一篇:下拉刷新(仿网易) .

源码下载http://download.csdn.net/detail/jefry_xdz/4511460 

                                               

代码部分如下:

[java] view plain copy print ?
  1. package com.jefry;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.util.Log;  
  7. import android.view.MotionEvent;  
  8. import android.view.View;  
  9. import android.widget.ImageView;  
  10. import android.widget.ProgressBar;  
  11. import android.widget.ScrollView;  
  12. import android.widget.TextView;  
  13.   
  14. public class NetNewDemo extends Activity {  
  15.     public static ImageView fresh = null;  
  16.     View mRefreshView;  
  17.     int top = 0;  
  18.     int first = 0;  
  19.     TextView mRefreshViewText;  
  20.     ImageView mRefreshViewImage;  
  21.     ImageView mImageView;  
  22.     ProgressBar mRefreshViewProgress;  
  23.     TextView mRefreshViewLastUpdated;  
  24.       
  25.     Handler hand = new Handler(){  
  26.         public void handleMessage(android.os.Message msg) {  
  27.              mRefreshViewImage.setVisibility(View.GONE);  
  28.              mImageView.setVisibility(View.GONE);  
  29.              mRefreshView.setPadding(mRefreshView.getPaddingLeft(),0,mRefreshView.getPaddingRight(),mRefreshView.getPaddingBottom());  
  30.         }  
  31.     };  
  32.     @Override  
  33.     public void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.main);  
  36.         mRefreshView = (View)findViewById(R.id.layout);  
  37.         mRefreshViewImage = (ImageView) findViewById(R.id.pull_to_refresh_image);  
  38.         mImageView = (ImageView)findViewById(R.id.going);  
  39.         ScrollView sc = (ScrollView)findViewById(R.id.scroll);  
  40.         sc.setOnTouchListener(new View.OnTouchListener() {  
  41.             public boolean onTouch(View arg0, MotionEvent event) {  
  42.                 Log.i("TAG""=============onTouchEvent=================");  
  43.                 //当前手指的Y值   
  44.                 final int y = (int) event.getY();  
  45.                // mRefreshViewImage.setImageResource(R.drawable.fresh_going);   
  46.                 switch (event.getAction()) {  
  47.                     case MotionEvent.ACTION_UP:  
  48.                        try{  
  49.                            first = 0;  
  50.                            mRefreshViewImage.setImageResource(R.drawable.fresh_going);  
  51.                            mRefreshView.setPadding(mRefreshView.getPaddingLeft(),0,mRefreshView.getPaddingRight(),mRefreshView.getPaddingBottom());  
  52.                            new Thread(new Runnable() {  
  53.                             public void run() {  
  54.                                 try {  
  55.                                     Thread.sleep(1500);  
  56.                                 } catch (InterruptedException e) {  
  57.                                 }  
  58.                                 hand.sendEmptyMessage(1);  
  59.                             }  
  60.                           }).start();  
  61.                         }catch (Exception e) {}  
  62.                          break;  
  63.                     case MotionEvent.ACTION_DOWN:  
  64.                         first = y;              
  65.                         break;  
  66.                       
  67.                     case MotionEvent.ACTION_MOVE:  
  68.                         mImageView.setVisibility(View.GONE);  
  69.                         int instance = y-first;  
  70.                         if(instance>0 && instance < 30) {  
  71.                             mRefreshViewImage.setVisibility(View.VISIBLE);  
  72.                             mRefreshViewImage.setImageResource(R.drawable.down);  
  73.                         } else if(instance >=30){  
  74.                             mRefreshViewImage.setVisibility(View.VISIBLE);  
  75.                             mRefreshViewImage.setImageResource(R.drawable.fresh_before);  
  76.                         }  
  77.                           
  78.                         mRefreshView.setPadding(mRefreshView.getPaddingLeft(),instance,mRefreshView.getPaddingRight(),mRefreshView.getPaddingBottom());  
  79.                         //mRefreshViewImage.setImageResource(R.drawable.fresh_going);   
  80.                         break;  
  81.                 }  
  82.                 return true;  
  83.             }  
  84.         });  
  85.           
  86.     }  
  87.       
  88.       
  89.     @Override  
  90.     public boolean onTouchEvent(MotionEvent event) {  
  91.         Log.i("TAG""=============onTouchEvent=================");  
  92.         //当前手指的Y值   
  93.         final int y = (int) event.getY();  
  94.         switch (event.getAction()) {  
  95.             case MotionEvent.ACTION_UP:  
  96.                  first = 0;  
  97.                  prepareForRefresh();  //准备刷新   
  98.                 break;  
  99.             case MotionEvent.ACTION_DOWN:  
  100.                 first = y;              
  101.                 break;  
  102.               
  103.             case MotionEvent.ACTION_MOVE:  
  104.                 //更行头视图的toppadding 属性   
  105.                 Log.i("TAG""=============ACTION_MOVE============" + (y - first));  
  106.                 mRefreshView.setPadding(mRefreshView.getPaddingLeft(),(y - first),mRefreshView.getPaddingRight(),mRefreshView.getPaddingBottom());  
  107.                 //applyHeaderPadding(event);   
  108.                 break;  
  109.         }  
  110.         return super.onTouchEvent(event);  
  111.     }  
  112.   
  113.   
  114.     public void prepareForRefresh() {  
  115.         resetHeaderPadding();   //初始化,头部文件   
  116.   
  117.         mRefreshViewImage.setVisibility(View.GONE);  
  118.         mRefreshViewImage.setImageDrawable(null);  
  119.         mRefreshViewProgress.setVisibility(View.VISIBLE);  
  120.   
  121.        mRefreshViewText.setText("Jefry ");  
  122.         
  123.     }  
  124.       
  125.     private void resetHeaderPadding() {  
  126.         mRefreshView.setPadding(  
  127.                 mRefreshView.getPaddingLeft(),  
  128.                 0,  
  129.                 mRefreshView.getPaddingRight(),  
  130.                 mRefreshView.getPaddingBottom());  
  131.     }  
  132.       
  133. }  


你可能感兴趣的:(android,exception,image,null,UP,action)