如何利用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();
//畫出原圖
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;
}
}
實(shí)現(xiàn)的效果如下圖:

以上就是本文的全部內(nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
將Eclipse工程轉(zhuǎn)Android Studio工程的步驟與注意事項(xiàng)
這篇文章主要給大家介紹了將Eclipse工程轉(zhuǎn)Android Studio工程的方法步驟,并給大家分享了其中的一些注意事項(xiàng),文中將實(shí)現(xiàn)的步驟一步步介紹的非常詳細(xì),需要的朋友們可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
OpenGL關(guān)于glStencilFuncSeparate()和glStencilFunc()函數(shù)的區(qū)別講解
今天小編就為大家分享一篇OpenGL關(guān)于glStencilFuncSeparate()和glStencilFunc()函數(shù)的區(qū)別講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-04-04
Android筆記之:App調(diào)試的幾個(gè)命令的實(shí)踐與分析
本篇文章介紹了,在Android中:App調(diào)試的幾個(gè)命令的實(shí)踐與分析。需要的朋友參考下2013-04-04
Android開發(fā)學(xué)習(xí)路線的七大階段
這篇文章主要介紹了Android開發(fā)學(xué)習(xí)路線的七大階段,本文講解了Java面向?qū)ο缶幊?、Java Web開發(fā)、android UI編程、android網(wǎng)絡(luò)編程與數(shù)據(jù)存儲(chǔ)、android手機(jī)硬件管理等七大階段,需要的朋友可以參考下2015-04-04
解決在eclipse中將android項(xiàng)目生成apk并且給apk簽名的實(shí)現(xiàn)方法詳解
本篇文章是對在eclipse中將android項(xiàng)目生成apk并且給apk簽名的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android?14新功能HighLights快速實(shí)現(xiàn)文本高亮
這篇文章主要為大家介紹了Android?14新功能HighLights快速實(shí)現(xiàn)文本高亮示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

