ImageView實現按壓後實現動畫縮小,鬆手或者滑出ImageView的邊界時,實現動畫放大效果。android
具體案例請參考去哪兒網的 【去哪兒旅行app】app
首先是佈局文件image_wall_layout.xmlide
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="horizontal" android:padding="10dip" android:weightSum="2" > <ImageView android:id="@+id/layout_wallitem_1" android:layout_width="match_parent" android:layout_height="80dip" android:layout_marginBottom="16dip" android:layout_weight="1" android:scaleType="fitCenter" android:src="@drawable/ic_category_15" /> <View android:layout_width="16dip" android:layout_height="match_parent" /> <ImageView android:id="@+id/layout_wallitem_2" android:layout_width="match_parent" android:layout_height="80dip" android:layout_marginBottom="16dip" android:layout_weight="1" android:scaleType="fitCenter" android:src="@drawable/ic_category_2" /> </LinearLayout>
Activity代碼佈局
public class ImageWallActivity extends Activity implements OnTouchListener { private View leftLayoutItem; private View rightLayoutItem; @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.image_wall_layout); leftLayoutItem = findViewById(R.id.layout_wallitem_1); rightLayoutItem = findViewById(R.id.layout_wallitem_2); leftLayoutItem.setOnTouchListener(this); rightLayoutItem.setOnTouchListener(this); } @Override protected void onDestroy() { super.onDestroy(); } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: scaleToSmaller(v); break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: scaleToNormal(v); break; default: break; } //這裡必須是true,否則事件監聽有問題 return true; } private void scaleToSmaller(View v) { Animation loadAnimation = AnimationUtils.loadAnimation(this, R.anim.scale_xysize_smaller_anim); v.clearAnimation(); v.startAnimation(loadAnimation); } private void scaleToNormal(View v) { Animation loadAnimation = AnimationUtils.loadAnimation(this, R.anim.scale_xysize_normal_anim); v.clearAnimation(); v.startAnimation(loadAnimation); } }
動畫代碼動畫
scale_xysize_normal_anim.xmlui
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/bounce_interpolator" android:duration="300" android:fromXScale="0.9" android:fromYScale="0.9" android:toXScale="1" android:toYScale="1" android:pivotX="50%" android:pivotY="50%" android:fillAfter="true" />
scale_xysize_smaller_anim.xmlthis
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:duration="300" android:fromXScale="1" android:fromYScale="1" android:toXScale="0.9" android:toYScale="0.9" android:pivotX="50%" android:pivotY="50%" android:fillAfter="true" />
問題來了,發現點擊是能夠了,按壓時縮小,鬆手或者滑出邊界也自動放大了,但新的問題來了,點擊事件沒法監聽,解決辦法以下spa
private long startTime = 0; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: scaleToSmaller(v); startTime = System.currentTimeMillis(); break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: scaleToNormal(v); if(System.currentTimeMillis()-startTime<=800) { onItemClick(v); } break; default: break; } //這裡必須是true,否則事件監聽有問題 return true; } public void onItemClick(View v) { Log.d("v", "id="+v.getId()); }
正常狀態code
點擊左圖標,以動畫的方式縮小,手指釋放,以動畫的方式還原orm
點擊右圖標,以動畫的方式縮小,手指釋放,以動畫的方式還原
說明:須要的jar爲:android-17版本及以上版本(或者須要android-support-v8-renderscript.jar),對於SDK版本較低的同窗,可使用android-support-v8-rendscript.jar,
假如說你上面2個都沒有咋辦,看下面的代碼註釋部分,可移除部分
package com.charbgr.BlurNavigationDrawer; public class BlurUtils { @SuppressLint("NewApi") public static Bitmap fastblur(Context context, Bitmap sentBitmap, int radius, boolean canReuseInBitmap) { /***************************start:可移除部分************************************/ if (VERSION.SDK_INT >= 17) { Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true); final RenderScript rs = RenderScript.create(context); final Allocation input = Allocation.createFromBitmap( rs, sentBitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT); final Allocation output = Allocation.createTyped(rs, input.getType()); final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius /* e.g. 3.f */); script.setInput(input); script.forEach(output); output.copyTo(bitmap); return bitmap; } /***************************end:可移除部分************************************/ // Stack Blur Algorithm by Mario Klingemann <mario@quasimondo.com> Bitmap bitmap; if (canReuseInBitmap) { bitmap = sentBitmap; } else { bitmap = sentBitmap.copy(sentBitmap.getConfig(), true); } if (radius < 1) { return (null); } int w = bitmap.getWidth(); int h = bitmap.getHeight(); int[] pix = new int[w * h]; bitmap.getPixels(pix, 0, w, 0, 0, w, h); int wm = w - 1; int hm = h - 1; int wh = w * h; int div = radius + radius + 1; int r[] = new int[wh]; int g[] = new int[wh]; int b[] = new int[wh]; int rsum, gsum, bsum, x, y, i, p, yp, yi, yw; int vmin[] = new int[Math.max(w, h)]; int divsum = (div + 1) >> 1; divsum *= divsum; int dv[] = new int[256 * divsum]; for (i = 0; i < 256 * divsum; i++) { dv[i] = (i / divsum); } yw = yi = 0; int[][] stack = new int[div][3]; int stackpointer; int stackstart; int[] sir; int rbs; int r1 = radius + 1; int routsum, goutsum, boutsum; int rinsum, ginsum, binsum; for (y = 0; y < h; y++) { rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; for (i = -radius; i <= radius; i++) { p = pix[yi + Math.min(wm, Math.max(i, 0))]; sir = stack[i + radius]; sir[0] = (p & 0xff0000) >> 16; sir[1] = (p & 0x00ff00) >> 8; sir[2] = (p & 0x0000ff); rbs = r1 - Math.abs(i); rsum += sir[0] * rbs; gsum += sir[1] * rbs; bsum += sir[2] * rbs; if (i > 0) { rinsum += sir[0]; ginsum += sir[1]; binsum += sir[2]; } else { routsum += sir[0]; goutsum += sir[1]; boutsum += sir[2]; } } stackpointer = radius; for (x = 0; x < w; x++) { r[yi] = dv[rsum]; g[yi] = dv[gsum]; b[yi] = dv[bsum]; rsum -= routsum; gsum -= goutsum; bsum -= boutsum; stackstart = stackpointer - radius + div; sir = stack[stackstart % div]; routsum -= sir[0]; goutsum -= sir[1]; boutsum -= sir[2]; if (y == 0) { vmin[x] = Math.min(x + radius + 1, wm); } p = pix[yw + vmin[x]]; sir[0] = (p & 0xff0000) >> 16; sir[1] = (p & 0x00ff00) >> 8; sir[2] = (p & 0x0000ff); rinsum += sir[0]; ginsum += sir[1]; binsum += sir[2]; rsum += rinsum; gsum += ginsum; bsum += binsum; stackpointer = (stackpointer + 1) % div; sir = stack[(stackpointer) % div]; routsum += sir[0]; goutsum += sir[1]; boutsum += sir[2]; rinsum -= sir[0]; ginsum -= sir[1]; binsum -= sir[2]; yi++; } yw += w; } for (x = 0; x < w; x++) { rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; yp = -radius * w; for (i = -radius; i <= radius; i++) { yi = Math.max(0, yp) + x; sir = stack[i + radius]; sir[0] = r[yi]; sir[1] = g[yi]; sir[2] = b[yi]; rbs = r1 - Math.abs(i); rsum += r[yi] * rbs; gsum += g[yi] * rbs; bsum += b[yi] * rbs; if (i > 0) { rinsum += sir[0]; ginsum += sir[1]; binsum += sir[2]; } else { routsum += sir[0]; goutsum += sir[1]; boutsum += sir[2]; } if (i < hm) { yp += w; } } yi = x; stackpointer = radius; for (y = 0; y < h; y++) { // Preserve alpha channel: ( 0xff000000 & pix[yi] ) pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum]; rsum -= routsum; gsum -= goutsum; bsum -= boutsum; stackstart = stackpointer - radius + div; sir = stack[stackstart % div]; routsum -= sir[0]; goutsum -= sir[1]; boutsum -= sir[2]; if (x == 0) { vmin[y] = Math.min(y + r1, hm) * w; } p = x + vmin[y]; sir[0] = r[p]; sir[1] = g[p]; sir[2] = b[p]; rinsum += sir[0]; ginsum += sir[1]; binsum += sir[2]; rsum += rinsum; gsum += ginsum; bsum += binsum; stackpointer = (stackpointer + 1) % div; sir = stack[stackpointer]; routsum += sir[0]; goutsum += sir[1]; boutsum += sir[2]; rinsum -= sir[0]; ginsum -= sir[1]; binsum -= sir[2]; yi += w; } } bitmap.setPixels(pix, 0, w, 0, 0, w, h); return (bitmap); } }