效果图
首先要自定义View ,添加特有的方法
package com.BlurEffect; import android.content.Context; import android.graphics.Matrix; import android.util.AttributeSet; import android.widget.ImageView; public class TopCenterImageView extends ImageView { public TopCenterImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setScaleType(ScaleType.MATRIX); } public TopCenterImageView(Context context, AttributeSet attrs) { super(context, attrs); setScaleType(ScaleType.MATRIX); } public TopCenterImageView(Context context) { super(context); setScaleType(ScaleType.MATRIX); } @Override protected boolean setFrame(int l, int t, int r, int b) { if (getDrawable() == null) { return super.setFrame(l, t, r, b); } Matrix matrix = getImageMatrix(); float scaleFactor = getWidth() / (float) getDrawable().getIntrinsicWidth(); matrix.setScale(scaleFactor, scaleFactor, 0, 0); setImageMatrix(matrix); return super.setFrame(l, t, r, b); } }
package com.BlurEffect; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; class ScrollableImageView extends View { private Bitmap adaptedImage; private Paint paint = new Paint(); private Bitmap originalImage; private int screenWidth; private int scrollY = 0; public ScrollableImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ScrollableImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public ScrollableImageView(Context context) { this(context, null); } @Override protected void onDraw(Canvas canvas) { if (adaptedImage != null) canvas.drawBitmap(adaptedImage, 0, 0, paint); } public void handleScroll(float distY) { if (getHeight() > 0 && originalImage != null) { if (scrollY <= originalImage.getHeight() - getHeight()) { adaptedImage = Bitmap.createBitmap(originalImage, 0, (int) -distY, screenWidth, getHeight()); invalidate(); } } } public void setoriginalImage(Bitmap bmp) { this.originalImage = bmp; adaptedImage = Bitmap.createBitmap(bmp); invalidate(); } public void setScreenWidth(int screenWidth) { this.screenWidth = screenWidth; } }在 MainActivity.xml中进行控件布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.BlurEffect.TopCenterImageView android:id="@+id/blurred_image" android:layout_width="match_parent" android:layout_height="match_parent" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > <com.BlurEffect.ScrollableImageView android:id="@+id/blurred_image_header" android:layout_width="match_parent" android:layout_height="match_parent" /> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/header" android:gravity="center_vertical" /> </RelativeLayout> </RelativeLayout>
在 mainActivity 中 进行控件的查找以及 更新UI 的操作
package com.BlurEffect; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.Window; import android.widget.ImageView; import java.io.File; public class Main2Activity extends Activity { private static final String BLURRED_IMG_PATH = "blurred_image.png"; private ImageView mBlurredImage; private ScrollableImageView mBlurredImageHeader; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.activity_main2); final int screenWidth = ImageUtils.getScreenWidth(this); mBlurredImage = (ImageView) findViewById(R.id.blurred_image); mBlurredImageHeader = (ScrollableImageView) findViewById(R.id.blurred_image_header); // 把 资源图片 加载到内存中 final File blurredImage = new File(getFilesDir() + BLURRED_IMG_PATH); if (!blurredImage.exists()) { setProgressBarIndeterminateVisibility(true); new Thread(new Runnable() { @Override public void run() { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.image, options); Bitmap newImg = Blur.fastblur(Main2Activity.this, image, 12); ImageUtils.storeImage(newImg, blurredImage); // 在主线程更新UI runOnUiThread(new Runnable() { @Override public void run() { updateView(screenWidth); setProgressBarIndeterminateVisibility(false); } }); } }).start(); } else { // 更新BitMap updateView(screenWidth); } } private void updateView(final int screenWidth) { Bitmap bmpBlurred = BitmapFactory.decodeFile(getFilesDir() + BLURRED_IMG_PATH); bmpBlurred = Bitmap.createScaledBitmap(bmpBlurred, screenWidth, (int) (bmpBlurred.getHeight() * ((float) screenWidth) / (float) bmpBlurred.getWidth()), false); mBlurredImage.setImageBitmap(bmpBlurred); mBlurredImageHeader.setoriginalImage(bmpBlurred); } }