隐藏和显示异步任务,主要是通过控制内边距Pandding来实现
package com.siyehuazhilian.pushdownrefresh; import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; import android.os.Handler; import android.view.MotionEvent; import android.view.ViewTreeObserver; import android.view.ViewTreeObserver.OnPreDrawListener; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.Toast; import com.siyehuazhilian.utils.DownImageTask; public class MainActivity extends Activity { private RelativeLayout relativeLayout; private LinearLayout linearLayout; private ImageView imageView; private int refreshHeight; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); relativeLayout = (RelativeLayout) findViewById(R.id.relativelayout); linearLayout = (LinearLayout) findViewById(R.id.linearlayout_refresh); imageView = (ImageView) findViewById(R.id.imageview); ViewTreeObserver observer = linearLayout.getViewTreeObserver(); observer.addOnPreDrawListener(new OnPreDrawListener() { @Override public boolean onPreDraw() { if (refreshHeight == 0) { refreshHeight = linearLayout.getMeasuredHeight(); relativeLayout.setPadding(0, -refreshHeight, 0, 0); } return true; } }); } private int startY; private int distance; @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: startY = (int) event.getRawY(); break; case MotionEvent.ACTION_MOVE: distance = (int) ((event.getRawY() - startY) / 1.5); if (distance < linearLayout.getHeight() && distance > 0) { // distance = refreshHeight; relativeLayout.setPadding(0, linearLayout.getHeight() - distance, 0, 0); } break; case MotionEvent.ACTION_UP: // 手指离开,判断是满足刷新的要求 // 要求distance要大于等于linearLayout.getHeight()的一半, // 如果大于,就让刷新界面完全展示出来并开启下载任务,如果小于,就重新隐藏刷新界面 if (distance > linearLayout.getHeight() / 2) { relativeLayout.setPadding(0, 0, 0, 0); DownImageTask downImageTask = new DownImageTask(handler); downImageTask .execute("http://a.hiphotos.baidu.com/image/w%3D2048/sign=487f5fb067380cd7e61ea5ed957cad34/a6efce1b9d16fdfa575066eeb68f8c5494ee7bf6.jpg"); } else { relativeLayout.setPadding(0, -linearLayout.getHeight(), 0, 0); } break; default: break; } return super.onTouchEvent(event); } private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case 1: Bitmap bitmap = (Bitmap) msg.obj; imageView.setImageBitmap(bitmap); Toast.makeText(MainActivity.this, "更新完毕", 1000).show(); // 下载完成还是要隐藏刷新的界面的 relativeLayout.setPadding(0, -linearLayout.getHeight(), 0, 0); break; default: break; } }; }; }
package com.siyehuazhilian.utils; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Handler; import android.os.Message; public class DownImageTask extends AsyncTask<String, Integer, Bitmap> { private Handler handler; public DownImageTask(Handler handler) { this.handler = handler; } @Override protected Bitmap doInBackground(String... parems) { Bitmap bitmap = null; try { URL url = new URL(parems[0]); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); InputStream inputStream = connection.getInputStream(); bitmap = BitmapFactory.decodeStream(inputStream); } catch (Exception e) { e.printStackTrace(); } return bitmap; } @Override protected void onPostExecute(Bitmap result) { super.onPostExecute(result); Message message = handler.obtainMessage(1, result); message.sendToTarget(); } }