日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android中的滤镜,android 实现滤镜效果

發(fā)布時(shí)間:2024/3/26 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android中的滤镜,android 实现滤镜效果 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

最近在看關(guān)于自定義控件的東西,順便看到了關(guān)于濾鏡的東西,就寫出來分享一下

這個(gè)有三個(gè)已知子類,寫一個(gè)濾鏡效果,只需要第一個(gè)已知子類就夠用了,這個(gè)類構(gòu)造方法public ColorMatrixColorFilter (ColorMatrix matrix),ColorMatrix的初始化則需要一個(gè)4*5的fload行的矩陣 大家看下面就知道了

這是我的寫法

這個(gè)就是顏色矩陣,修改這里面的值就可以做到濾鏡的效果,先改一下試試,看運(yùn)行結(jié)果

對了 首先要給畫筆設(shè)置上一個(gè)顏色

mpaint.setColor(Color.argb(255,255,128,103)); ,然后看效果

這個(gè)解釋繪制出來的效果,然后改一改,再看效果

ColorMatrix colorMatrix = newColorMatrix(new float[]{

0.9F,1,0,0,0,//A0,0.5F,0.6f,0,0,//R0,0,0.5F,0,0.7f,//G0,0,0,1,0,//B}); 隨便改了改

這就是效果

換一張圖片可能效果更加明顯

?原圖

運(yùn)行代碼后的圖

還有這樣

是不是很酷炫?

然而剛才的矩陣怎么計(jì)算的,下面是他的公式

矩陣ColorMatrix的一行乘以矩陣MyColor的一列作為矩陣Result的一行,這里MyColor的RGBA值我們需要轉(zhuǎn)換為[0, 1]。那么我們依據(jù)此公式來計(jì)算下我們得到的RGBA值是否跟我們計(jì)算得出來的圓的RGBA值一樣:

就是這樣。

下面是這個(gè)控件的源碼,只需要在里面填上一個(gè)方法,就可以做一個(gè)濾鏡了。 全部項(xiàng)目的源碼在我的 GitHub?上。。。

package com.liran.custom_view.MyView;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.ColorMatrix;

import android.graphics.ColorMatrixColorFilter;

import android.graphics.Paint;

import android.util.AttributeSet;

import android.util.Log;

import android.view.View;

import com.liran.custom_view.R;

import com.liran.custom_view.Utils.DisplayUtil;

import com.liran.custom_view.Utils.MeasureUtil;

/**

* Created by lr 李冉 on 2015-08-31.

*/

public class CustomView extends View {

private String TAG = "CustomView";

private Paint mpaint;

private int radious = 50;

private int x, y;// 位圖繪制時(shí)左上角的起點(diǎn)坐標(biāo)

private Bitmap bitmap;

public CustomView(Context context) {

super(context);

initPaint(context);

}

public CustomView(Context context, AttributeSet attrs) {

super(context, attrs);

initPaint(context);

}

public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

initPaint(context);

}

private void initPaint(Context context) {

mpaint = new Paint();

mpaint.setAntiAlias(true);

/*

* 設(shè)置畫筆樣式為描邊,圓環(huán)嘛……當(dāng)然不能填充不然就么意思了

*

* 畫筆樣式分三種:

* 1.Paint.Style.STROKE:描邊

* 2.Paint.Style.FILL_AND_STROKE:描邊并填充

* 3.Paint.Style.FILL:填充

*/

// mpaint.setStyle(Paint.Style.STROKE);

mpaint.setStyle(Paint.Style.FILL);

// 設(shè)置畫筆顏色為自定義顏色

mpaint.setColor(Color.argb(255, 255, 128, 103));

//設(shè)置顏色矩陣 注意 是 4x5的float[]類型的矩陣 修改這里的數(shù)值就相當(dāng)于做一個(gè)濾鏡了

ColorMatrix colorMatrix = new ColorMatrix(new float[]{

0.9F, 0, 0.5f, 0, 0, //A

0, 0.5F, 0, 0.6f, 0, //R

0, 0, 0.5F, 0.3f, 0, //G

0, 0, 0, 1, 0, //B

});

// 設(shè)置顏色過濾

mpaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

// 設(shè)置顏色過濾

// mpaint.setColorFilter(new LightingColorFilter(0xFFFF00FF, 0x00000000));

// 設(shè)置顏色過濾

// mpaint.setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.DARKEN));

/*

* 設(shè)置描邊的粗細(xì),單位:像素px

* 注意:當(dāng)setStrokeWidth(0)的時(shí)候描邊寬度并不為0而是只占一個(gè)像素

*/

mpaint.setStrokeWidth(10);

initRes(context);

}

private void initRes(Context context) {

bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.bg2);

/* x = Math.abs(getMeasuredWidth() / 2 - bitmap.getWidth() / 2);

y = Math.abs(getMeasuredHeight() / 2 - bitmap.getHeight() / 2);*/

x = MeasureUtil.getScreenWidth(context)[0] / 2 - bitmap.getWidth() / 2 - 18;

y = (MeasureUtil.getScreenWidth(context)[1] - MeasureUtil.getStatusBarHeight(context) - DisplayUtil.dip2px(context, 50)) / 2 - bitmap.getHeight() / 2;

Log.d(TAG, "initRes x=" + x + "y=" + y + " MeasureUtil.getScreenWidth(context)[1]= " + MeasureUtil.getScreenWidth(context)[1]);

Log.d(TAG, "initRes bitmap.getHeight()=" + bitmap.getHeight());

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

// Log.d(TAG, "onDraw with="+getMeasuredWidth()+" heigh="+getMeasuredHeight());

// canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2, radious, mpaint);

Log.d(TAG, "onDraw x=" + x + " y= " + y);

canvas.drawBitmap(bitmap, x, y, mpaint);

}

public synchronized void setRadius(int radius) {

this.radious = radius;

invalidate();

}

/**

* 開啟一個(gè)線程去執(zhí)行動畫

*/

public void startAnmi() {

new Thread(new Runnable() {

@Override

public void run() {

while (true) {

/*

* 確保線程不斷執(zhí)行不斷刷新界面

*/

while (true) {

try {

/*

* 如果半徑小于200則自加否則大于200后重置半徑值以實(shí)現(xiàn)往復(fù)

*/

if (radious <= 200) {

radious += 10;

//非主線程刷新view

postInvalidate();

} else {

radious = 0;

}

// 每執(zhí)行一次暫停40毫秒

Thread.sleep(40);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

}).start();

}

}

總結(jié)

以上是生活随笔為你收集整理的android中的滤镜,android 实现滤镜效果的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。