W3Cschool
恭喜您成為首批注冊(cè)用戶(hù)
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
上一節(jié)中我們講解了Android中Paint API中的ColorFilter(顏色過(guò)濾器)的第一個(gè)子類(lèi):ColorMatrixColorFilter(顏色矩陣顏色過(guò)濾器),相信又開(kāi)闊了大家的Android圖像處理視野, 而本節(jié)我們來(lái)研究它的第二個(gè)子類(lèi):LightingColorFilter(光照色彩顏色過(guò)濾器),先上一發(fā) 官方API文檔:LightingColorFilter,文檔里的東西也不多,關(guān)鍵的在這里:
大概意思就是:一個(gè)顏色過(guò)濾器,可以用來(lái)模擬簡(jiǎn)單的燈光效果,構(gòu)造方法的參數(shù)有兩個(gè),一個(gè) 用來(lái)乘以原圖的RPG值,一個(gè)添加到前面得出的結(jié)果上!其實(shí)計(jì)算方法無(wú)非: *(RGB值 mul + Add) % 255**,從而得到新的RPG值,這里的%是求余,另外,整個(gè)過(guò)程中Alpha不 參與改變!下面我們寫(xiě)個(gè)示例來(lái)驗(yàn)證驗(yàn)證!
運(yùn)行效果圖:
實(shí)現(xiàn)代碼:
先是一個(gè)簡(jiǎn)單的布局:activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
tools:context=".MainActivity">
<ImageView
android:id="@+id/img_meizi"
android:layout_width="300dp"
android:layout_height="300dp"
android:src="@mipmap/img_meizi" />
<EditText
android:id="@+id/edit_mul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/img_meizi"
android:text="0" />
<EditText
android:id="@+id/edit_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edit_mul"
android:text="0" />
<Button
android:id="@+id/btn_change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/img_meizi"
android:layout_below="@id/img_meizi"
android:text="變化" />
</RelativeLayout>
接著是我們的MainActiivty.java,同樣很簡(jiǎn)單:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ImageView img_meizi;
private EditText edit_mul;
private EditText edit_add;
private Button btn_change;
private Bitmap mBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.img_meizi);
bindViews();
}
private void bindViews() {
img_meizi = (ImageView) findViewById(R.id.img_meizi);
edit_mul = (EditText) findViewById(R.id.edit_mul);
edit_add = (EditText) findViewById(R.id.edit_add);
btn_change = (Button) findViewById(R.id.btn_change);
btn_change.setOnClickListener(this);
}
private Bitmap ProcessImage(Bitmap bp,int mul,int add){
Bitmap bitmap = Bitmap.createBitmap(bp.getWidth(),bp.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColorFilter(new LightingColorFilter(mul,add));
canvas.drawBitmap(bp,0,0,paint);
return bitmap;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_change:
int mul = Integer.parseInt(edit_mul.getText().toString());
int add = Integer.parseInt(edit_add.getText().toString());
img_meizi.setImageBitmap(ProcessImage(mBitmap,mul,add));
break;
}
}
}
好了,LightingColorFilter的使用演示完畢~
嗯,本節(jié)演示了一下LightingColorFilter的一個(gè)基本用法,用來(lái)模擬簡(jiǎn)單的燈光效果, 實(shí)現(xiàn)簡(jiǎn)單的圖片處理效果,好的,本節(jié)就到這里,謝謝~
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: