如何利用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)文章
Android畫(huà)板開(kāi)發(fā)之基本畫(huà)筆功能
這篇文章主要為大家詳細(xì)介紹了Android畫(huà)板開(kāi)發(fā)之基本畫(huà)筆功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android通過(guò)ConnectivityManager檢查網(wǎng)絡(luò)狀態(tài)
這篇文章主要為大家詳細(xì)介紹了Android通過(guò)ConnectivityManager檢查網(wǎng)絡(luò)狀態(tài)的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-08-08Android ToggleButton 詳解及實(shí)例代碼
這篇文章主要介紹了Android ToggleButton 詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02Android Viewpager實(shí)現(xiàn)輪播廣告圖
這篇文章主要為大家詳細(xì)介紹了Android Viewpager實(shí)現(xiàn)輪播廣告圖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05建造者模式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
建造者實(shí)現(xiàn)抽象類(lèi)的所有未實(shí)現(xiàn)的方法,具體來(lái)說(shuō)一般是兩項(xiàng)任務(wù),組建產(chǎn)品;返回組建好的產(chǎn)品2017-08-08Android調(diào)用系統(tǒng)的發(fā)郵件功能的小例子
這篇文章介紹了Android調(diào)用系統(tǒng)的發(fā)郵件功能的小例子,有需要的朋友可以參考一下2013-08-08Android APP之WebView校驗(yàn)SSL證書(shū)的方法
這篇文章主要介紹了Android APP之WebView校驗(yàn)SSL證書(shū)的方法,需要的朋友可以參考下2017-09-09