毛玻璃效果

  效果图



首先要自定义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);
    }


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值