如何利用matrix实现图片倒影效果

所属分类: 软件编程 / Android 阅读数: 328
收藏 0 赞 0 分享

本文主要内容就是用marix加上渐变色实现图片倒影的效果,步骤如下:

 1. 获取需要倒影效果的图片,这里取原图片的一半

 2. 添加颜色渐变到倒影图片上

 具体的实现如下面代码所述,我们以一种自定义view的形式给出效果图,代码如下:

package com.flection.view;

import com.flection.main.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;

public class FlectionView extends View {

  Context mContext=null;
  public FlectionView(Context context) {
    super(context);
  }

  public FlectionView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.mContext=context;
  }

  @SuppressLint("DrawAllocation")
  @Override
  protected void onDraw(Canvas canvas) {
    //设置背景色
    this.setBackgroundColor(Color.parseColor("#8B8378"));
    Bitmap oldBitmap = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.dropbox);
    Bitmap newBitmap = createFlectionBitmap(oldBitmap);
    canvas.drawBitmap(newBitmap,newBitmap.getWidth() ,newBitmap.getHeight(), new Paint());
    this.invalidate();
  }

  //获取原图+倒影图的bitmap
  private Bitmap createFlectionBitmap(Bitmap oldBitmap) {
    int mWidth = oldBitmap.getWidth();
    int mHeight = oldBitmap.getHeight();
    //原图和倒影图之间的缝隙
    int gap = 2;
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);
    Bitmap flection = Bitmap.createBitmap(oldBitmap, 0, mHeight / 2,
        mWidth, mHeight / 2, matrix, false);
    Bitmap background = Bitmap.createBitmap(mWidth, mHeight+gap+mHeight/2, Config.ARGB_8888);
    Canvas canvas = new Canvas(background);
    Paint p1 = new Paint();
    //画出原图
    canvas.drawBitmap(oldBitmap, 0, 0, p1);
    //画出倒影图
    canvas.drawBitmap(flection, 0, mHeight+gap, p1);
    Paint shaderPaint = new Paint();
    LinearGradient shader = new LinearGradient(0, mHeight, 0,
        flection.getHeight(), 0x70ffffff, 0x00ffffff, TileMode.MIRROR);
    shaderPaint.setShader(shader);
    shaderPaint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));
    //画出渐变颜色
    canvas.drawRect(0, mHeight+gap, mWidth, background.getHeight(), shaderPaint);
    return background;
  }

}

实现的效果如下图:

以上就是本文的全部内容,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android studio点击跳转WebView详解

这篇文章主要为大家详细介绍了Android studio点击跳转WebView的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义Drawable实现圆形和圆角

这篇文章主要为大家详细介绍了Android自定义Drawable实现圆形和圆角,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义水平渐变进度条

这篇文章主要为大家详细介绍了Android自定义水平渐变进度条,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ToolBar中menu无法同时显示图标和文字问题的解决方法

这篇文章主要为大家详细介绍了ToolBar中menu无法同时显示图标和文字问题的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解React Native监听Android回退按键与程序化退出应用

这篇文章主要介绍了详解React Native监听Android回退按键与程序化退出应用的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享

android实现上传本地图片到网络功能

这篇文章主要为大家详细介绍了android实现上传本地图片到网络功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android实现QQ登录功能

这篇文章主要为大家详细介绍了Android实现QQ登录功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android实现简单的城市列表功能

这篇文章主要为大家详细介绍了Android实现简单的城市列表功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android Animation之TranslateAnimation(平移动画)

这篇文章主要为大家详细介绍了Animation之TranslateAnimation平移动画,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android 中Failed to read key from keystore解决办法

这篇文章主要介绍了Android 中Failed to read key from keystore解决办法的相关资料,希望通过本能帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多