国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

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

2019-12-12 06:20:12
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文主要內(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è)參考,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 靖西县| 斗六市| 汕尾市| 社旗县| 攀枝花市| 宜章县| 天祝| 茶陵县| 阿鲁科尔沁旗| 临武县| 安庆市| 淄博市| 许昌县| 萍乡市| 五莲县| 竹溪县| 灵武市| 紫金县| 静海县| 台前县| 资源县| 普安县| 辽中县| 垦利县| 镇平县| 鹤峰县| 金山区| 西平县| 岱山县| 青岛市| 交城县| 瓦房店市| 临漳县| 安化县| 台北市| 即墨市| 青州市| 晋中市| 高唐县| 雷州市| 桐梓县|