android提高图片亮度,Android 调节图片亮度

该博客介绍了一个自定义的 `LightControlView` 类,它继承自 `ImageView` 并实现了 `OnCheckedChangeListener` 和 `OnSeekBarChangeListener` 接口。这个视图允许用户通过 `SeekBar` 调整图片的亮度。在 `onDraw()` 方法中,首先根据给定的尺寸缩放图片,并在画布上绘制。`addEffectBrightness()` 方法通过 `ColorMatrix` 应用亮度效果,`turnOnOff()` 方法用于切换亮度状态。此外,`SeekBar` 的改变事件会调用 `addEffectBrightness()` 更新图片亮度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.ColorMatrix;

import android.graphics.ColorMatrixColorFilter;

import android.graphics.Matrix;

import android.graphics.Paint;

import android.util.AttributeSet;

import android.widget.CompoundButton;

import

android.widget.CompoundButton.OnCheckedChangeListener;

import android.widget.ImageView;

import android.widget.SeekBar;

import android.widget.SeekBar.OnSeekBarChangeListener;

public class LightControlView extends ImageView implements

OnCheckedChangeListener,OnSeekBarChangeListener{

Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

Bitmap dstBmap;

int imgId;

int height;

int width;

public void setImgId(int imgId) {

this.imgId = imgId;

}

public LightControlView(Context context,int imgId,int

height,int width) {

super(context);

this.imgId = imgId;

this.height = height;

this.width = width;

}

public LightControlView(Context context, AttributeSet attrs)

{

super(context, attrs);

}

public LightControlView(Context context, AttributeSet attrs,

int defStyle) {

super(context, attrs, defStyle);

}

@Override

protected void onDraw(Canvas canvas) {

if(dstBmap == null){

Bitmap mBitmap =

BitmapFactory.decodeResource(getResources(),imgId);

Matrix matrix = new Matrix();

matrix.postScale(width * 1f/ mBitmap.getWidth(), height *

1f/mBitmap.getHeight());

matrix.postScale(1, 1);

dstBmap = Bitmap.createBitmap(mBitmap, 0, 0,

mBitmap.getWidth(), mBitmap.getHeight(),matrix,true);

}

canvas.drawBitmap(dstBmap, 0, 0, mPaint);

}

public void addEffectBrightness(float brightness) {

ColorMatrix mCm = new ColorMatrix();

mCm.set(new float[] { 1, 0, 0, 0, brightness, 0, 1, 0, 0,

brightness,

0, 0, 1, 0, brightness, 0, 0, 0, 1, 0 });

mPaint.setColorFilter(new ColorMatrixColorFilter(mCm));

mCm = null;

this.postInvalidate();

}

public void turnOnOff(boolean isOnOff) {

float brightness;

if(isOnOff){

brightness = 60;

}else{

brightness = 1;

}

addEffectBrightness(brightness);

}

public void onProgressChanged(SeekBar seekBar, int

progress,

boolean fromUser) {

}

public void onStartTrackingTouch(SeekBar seekBar) {

// TODO Auto-generated method stub

}

public void onStopTrackingTouch(SeekBar seekBar) {

this.addEffectBrightness(seekBar.getProgress());

}

public void onCheckedChanged(CompoundButton buttonView,

boolean isChecked) {

if(isChecked){

this.addEffectBrightness(60);

}else{

this.addEffectBrightness(1);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值