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);
}
}
}