標籤:高斯模糊 下拉變大清晰 android
轉載請標明出處:http://blog.csdn.net/yianemail/article/details/47019383
一:我們可以看到APP"在路上"上面的 高斯的模糊的處理的效果,在下拉的時候,映像就會變的清晰並且變大
鬆開的時候自動回復模糊效果 。
效果分析。
我開始在想直接用高斯模糊處理圖片,在圖片不斷的下拉的過程中也對圖片的進行高斯處理達到即時對圖片進行高斯模糊處 理,達到 下拉到最下面的 圖片變的clear,在拉動的過程中,圖片的模糊度也隨之變化。
但是,高斯模糊處理圖片是比較消耗時間的。so 上面這樣處理將會是很卡的。然而在“在路上”的這個APP卻一點也不存在卡的 現象。
於是我想的實現思路是:
1 ,布局有兩張重疊的imageView(都不指定src)。(這個布局作為主布局的listview 的頭部布局)
2, 代碼實現對圖片的高斯處理。並且對布局兩個imageView 做setImageBitmap。其中模糊圖片覆蓋清晰圖片之上。
3, 對模糊圖片做setAlpha並且做放大處理。
二:主代碼處理:
package com.roger.listimgdemo;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Matrix;import android.graphics.PointF;import android.os.Handler;import android.os.Message;import android.util.AttributeSet;import android.util.DisplayMetrics;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.view.WindowManager;import android.widget.ImageView;import android.widget.ListAdapter;import android.widget.ListView;import android.widget.RelativeLayout;import com.roger.listimgdemo.R;/** * listview頭圖片下拉放大, 使用時務必調用方法setImageId或setImageBitmap設定圖片 否則報錯 Created by Roger * on 14-4-30. */public class ImgListView extends ListView {float mLastY;private static final int BACK_SCALE = 0;private boolean isHaveHead = false;// 頭部是否有圖片private float scaleY = 0;private boolean isBacking = false;// 是否處在回彈狀態private int displayWidth;private Context mContext;private Bitmap bmp;private View headerView;private ImageView imageView;/** 用於記錄拖拉圖片移動的座標位置 */private Matrix matrix = new Matrix();/** 用於記錄圖片要進行拖拉時候的座標位置 */private Matrix currentMatrix = new Matrix();private Matrix defaultMatrix = new Matrix();private float imgHeight, imgWidth;/** 記錄是拖拉照片模式還是放大縮小照片模式 0:拖拉模式,1:放大 */private int mode = 0;// 初始狀態/** 拖拉照片模式 */private final int MODE_DRAG = 1;/** 用於記錄開始時候的座標位置 */private PointF startPoint = new PointF();private int mImageId;private AttributeSet attrs;private ImageView mohu;private float alpha;public ImgListView(Context context) {super(context);this.mContext = context;initView();}public ImgListView(Context context, AttributeSet attrs) {super(context, attrs);this.mContext = context;this.attrs = attrs;initView();}public ImgListView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);this.mContext = context;this.attrs = attrs;initView();}public void setAdapter(ListAdapter adapter) {super.setAdapter(adapter);}public void addHeaderView(View v) {super.addHeaderView(v);}public void setImageId(int id) {this.mImageId = id;bmp = BitmapFactory.decodeResource(getResources(), mImageId);if (isHaveHead)this.removeHeaderView(headerView);initHead();}public void setImageBitmap(Bitmap bit) {this.bmp = bit;if (isHaveHead)this.removeHeaderView(headerView);initHead();}/** * 初始化圖片 */private void initView() {/* 取得螢幕解析度大小 */DisplayMetrics dm = new DisplayMetrics();WindowManager mWm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);mWm.getDefaultDisplay().getMetrics(dm);displayWidth = dm.widthPixels;TypedArray a = mContext.obtainStyledAttributes(attrs,R.styleable.ImgListView);mImageId = a.getResourceId(R.styleable.ImgListView_headimage, 0);a.recycle();if (null == bmp && mImageId != 0) {bmp = BitmapFactory.decodeResource(getResources(), mImageId);initHead();}}private void initHead() {/** * 得到頭部布局 */LayoutInflater inflater = LayoutInflater.from(mContext);headerView = inflater.inflate(R.layout.top_img, null);/** * 找到圖片控制項 */imageView = (ImageView) headerView.findViewById(R.id.qingxi);mohu = (ImageView) headerView.findViewById(R.id.mohu);/** * 把圖片做模糊處理 */Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.c);Bitmap bit = BlurBitmap.BoxBlurFilter(bitmap);/** * 設定圖片 */imageView.setImageBitmap(bitmap);mohu.setImageBitmap(bit);float scale = (float) displayWidth / (float) bmp.getWidth();// 1080/1800matrix.postScale(scale, scale, 0, 0);mohu.setImageMatrix(matrix);defaultMatrix.set(matrix);/** * 這邊是要設定的子View 的寬和高 */imgHeight = scale * bmp.getHeight();imgWidth = scale * bmp.getWidth();/** * RelativeLayout.LayoutParams 指定了子view的高和寬 這邊要注意,因為是有兩個子View的 設定兩次 */RelativeLayout.LayoutParams relativeLayouts = new RelativeLayout.LayoutParams((int) imgWidth, (int) imgHeight);imageView.setLayoutParams(relativeLayouts);RelativeLayout.LayoutParams relativeLayout = new RelativeLayout.LayoutParams((int) imgWidth, (int) imgHeight);mohu.setLayoutParams(relativeLayout);this.addHeaderView(headerView);isHaveHead = true;}/** * 向下滑動讓圖片變大 * * @param event * @return */public boolean onTouchEvent(MotionEvent event) {if (!isHaveHead) {// 無頭部圖片return super.onTouchEvent(event);}switch (event.getAction() & MotionEvent.ACTION_MASK) {// 手指壓下螢幕case MotionEvent.ACTION_DOWN:if (isBacking) {return super.onTouchEvent(event);}int[] location = new int[2];mohu.getLocationInWindow(location);if (location[1] >= 0) {mode = MODE_DRAG;// 記錄ImageView當前的移動位置mLastY = event.getY();currentMatrix.set(imageView.getImageMatrix());startPoint.set(event.getX(), event.getY());}break;// 手指在螢幕上移動,改事件會被不斷觸發case MotionEvent.ACTION_MOVE:/** * <=300: 指定了 只有在圖片地區才允許滑動 */if (event.getY() <= 300) {float dy = event.getY() - startPoint.y;if (dy > 0) {float y = event.getY();float alphaDelt = (mLastY - y) / 1000;alpha = mohu.getAlpha() + alphaDelt;if (alpha > 1.0) {alpha = 1.0f;} else if (alpha < 0.0) {alpha = 0.0f;}mohu.setAlpha(alpha);if (mode == MODE_DRAG) {float dx = event.getX() - startPoint.x; // 得到x軸的移動距離if (dy / 2 + imgHeight <= 1.5 * imgHeight) {matrix.set(currentMatrix);float scale = (dy / 2 + imgHeight) / (imgHeight);// 得到縮放倍數if (scale > 1.2) {scale = (float) 1.2;scaleY = dy;RelativeLayout.LayoutParams relativeLayout = new RelativeLayout.LayoutParams((int) (scale * imgWidth),(int) (scale * imgHeight));mohu.setLayoutParams(relativeLayout);matrix.postScale(scale, scale, imgWidth / 2, 0);mohu.setImageMatrix(matrix);}if (scale <= 1.2) {scaleY = dy;RelativeLayout.LayoutParams relativeLayouta = new RelativeLayout.LayoutParams((int) (scale * imgWidth),(int) (scale * imgHeight));imageView.setLayoutParams(relativeLayouta);matrix.postScale(scale, scale, imgWidth / 2, 0);/** * 放大縮小 */imageView.setImageMatrix(matrix); RelativeLayout.LayoutParams relativeLayout = new RelativeLayout.LayoutParams((int) (scale * imgWidth),(int) (scale * imgHeight));mohu.setLayoutParams(relativeLayout);matrix.postScale(scale, scale, imgWidth / 2, 0);/** * 放大縮小 */mohu.setImageMatrix(matrix);}}}}}break;// 手指離開螢幕case MotionEvent.ACTION_UP:// 當觸點離開螢幕,圖片還原mHandler.sendEmptyMessage(BACK_SCALE);case MotionEvent.ACTION_POINTER_UP:mode = 0;break;}return super.onTouchEvent(event);}private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubswitch (msg.what) {case BACK_SCALE:float scale = (scaleY / 2 + imgHeight) / (imgHeight);// 得到縮放倍數if (scale > 1.2) {scale = (float) 1.2;}if (scaleY > 0) {// mohu.setAlpha(alpha);mohu.setAlpha(1.0f);isBacking = true;matrix.set(currentMatrix);RelativeLayout.LayoutParams relativeLayouta = new RelativeLayout.LayoutParams((int) (scale * imgWidth), (int) (scale * imgHeight));imageView.setLayoutParams(relativeLayouta);matrix.postScale(scale, scale, imgWidth / 2, 0);imageView.setImageMatrix(matrix);RelativeLayout.LayoutParams relativeLayout = new RelativeLayout.LayoutParams((int) (scale * imgWidth), (int) (scale * imgHeight));mohu.setLayoutParams(relativeLayout);matrix.postScale(scale, scale, imgWidth / 2, 0);mohu.setImageMatrix(matrix);scaleY = (float) (scaleY / 2 - 1);mHandler.sendEmptyMessageDelayed(BACK_SCALE, 20);} else {scaleY = 0;RelativeLayout.LayoutParams relativeLayouta = new RelativeLayout.LayoutParams((int) (scale * imgWidth), (int) (scale * imgHeight));imageView.setLayoutParams(relativeLayouta);matrix.set(defaultMatrix);imageView.setImageMatrix(matrix);RelativeLayout.LayoutParams relativeLayout = new RelativeLayout.LayoutParams((int) imgWidth, (int) imgHeight);mohu.setLayoutParams(relativeLayout);matrix.set(defaultMatrix);mohu.setImageMatrix(matrix);isBacking = false;}break;default:break;}super.handleMessage(msg);}};}
三:效果實現
開始逐漸下拉:清晰且變大
最終變大 清晰。
資源下載: 項目下載
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
Android 高斯演算法在"在路上"APP 的實現