欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何利用matrix實(shí)現(xiàn)圖片倒影效果

 更新時(shí)間:2016年05月25日 14:18:28   作者:summerpxy  
利用matrix可以實(shí)現(xiàn)各種圖片的特效,比如圖片的旋轉(zhuǎn)、縮放、移動(dòng),甚至是圖片倒影效果,這篇文章為大家介紹了matrix實(shí)現(xiàn)圖片倒影的代碼,感興趣的小伙伴們可以參考一下

本文主要內(nèi)容就是用marix加上漸變色實(shí)現(xiàn)圖片倒影的效果,步驟如下:

 1. 獲取需要倒影效果的圖片,這里取原圖片的一半

 2. 添加顏色漸變到倒影圖片上

 具體的實(shí)現(xiàn)如下面代碼所述,我們以一種自定義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) {
    //設(shè)置背景色
    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();
    //畫(huà)出原圖
    canvas.drawBitmap(oldBitmap, 0, 0, p1);
    //畫(huà)出倒影圖
    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));
    //畫(huà)出漸變顏色
    canvas.drawRect(0, mHeight+gap, mWidth, background.getHeight(), shaderPaint);
    return background;
  }

}

實(shí)現(xiàn)的效果如下圖:

以上就是本文的全部?jī)?nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

最新評(píng)論