package com.mobage.ww.a975.widgets;
import java.io.IOException;
import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageButton;
public class ExampleImageButton extends ImageButton {
public enum State {
Normal,
Focused,
Selected,
Pressed,
Checked,
Custom,
Disabled,
PreNormal //only for blink
}
private static ColorFilter TRANS_DARKER;
private static ColorFilter TRANS_LIGHTER;
private int mNormalImageWith = -1;
private int mNormalImageHeight = -1;
private CustomImage mNormalStateDrawable;
private boolean mNeedStopBlink;
static {
TRANS_LIGHTER = new LightingColorFilter(Color.LTGRAY, 1);
TRANS_DARKER = new LightingColorFilter(Color.DKGRAY, 1);
}
private class CustomImage extends Drawable {
private Bitmap mBitmap;
private Bitmap mPreBitmap;
private Paint mPaint;
private Paint mPrepaint;
private Rect mSrcRect;
private Rect mDstRect;
private ColorMatrixColorFilter mDarkenFilter;
public CustomImage(Bitmap bitmap) {
super();
mBitmap = bitmap;
mPaint = new Paint();
mPaint.setAlpha(100);
mSrcRect = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
}
private void setPrebitmap(Bitmap bitmap) {
mPreBitmap = bitmap;
mPrepaint = new Paint();
}
@Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
if (mDstRect == null)
mDstRect = new Rect(0, 0, getWidth(), getHeight());
if (mPreBitmap != null) {
canvas.drawBitmap(mPreBitmap, mSrcRect, mDstRect, mPrepaint);
}
if (!isEnabled()) {
if (mDarkenFilter == null) {
float c = 0f;
float hs = 0.5f;
/*float[] colorTransform = {
hs, 0, 0, 0, 0,
0, hs, 0, 0, 0,
0, 0, hs, 0f, 0,
0, 0, 0, 1f, 0};*/
ColorMatrix colorMatrix = new ColorMatrix();
//colorMatrix.setSaturation(0f); //Remove Colour
colorMatrix.setScale(hs, hs, hs, 1.0f);
/*
float[] colorTransform = colorMatrix.getArray();
colorTransform[0] = hs;
colorTransform[6] = hs;
colorTransform[12] = hs;
colorMatrix.set(colorTransform); //Apply the Red
*/
mDarkenFilter = new ColorMatrixColorFilter(colorMatrix);
}
mPaint.setColorFilter(mDarkenFilter);
} else
mPaint.setColorFilter(null);
//canvas.drawColor(Color.WHITE, Mode.DARKEN);
canvas.drawBitmap(mBitmap, mSrcRect, mDstRect, mPaint);
}
@Override
public int getOpacity() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setAlpha(int alpha) {
// TODO Auto-generated method stub
mPaint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf) {
// TODO Auto-generated method stub
}
}
private final Handler mHandler = new Handler() {
private int count = 0;
private final int period = 20;
public void handleMessage(Message msg) {
if (msg.what == 0) {
mNormalStateDrawable.setAlpha(0xFF);
} else {
count++;
int alpha = (count * period);
if (alpha < 256) {
mNormalStateDrawable.setAlpha(0xFF - (alpha % 0x100));
} else if (alpha < 512) {
mNormalStateDrawable.setAlpha(alpha % 0x100);
} else {
count = 0;
if (mNeedStopBlink) {
mBlinkTimer.cancel();
this.obtainMessage(0).sendToTarget();
}
}
}
invalidate();
}
};
private class BlinkTimerTask extends TimerTask{
private WGFImageButton mButton;
public BlinkTimerTask(WGFImageButton button) {
mButton = button;
}
//@Override
public void run() {
if (mButton.isShown()) {
mHandler.obtainMessage(1).sendToTarget();
}
}
}
public class UnsupportedStateException extends Exception {
private static final long serialVersionUID = 273433377901363739L;
public UnsupportedStateException(String name){
super();
}
}
private StateListDrawable stateList;
private Timer mBlinkTimer;
private boolean mIsBlinking = false;
public WGFImageButton(Context context) {
super(context);
//startBlink(80);
stateList = new StateListDrawable();
}
public void startBlink(int interval) {
if (mIsBlinking) return;
if (mBlinkTimer != null)
mBlinkTimer.cancel();
else
mBlinkTimer = new Timer();
mNeedStopBlink = false;
mIsBlinking = true;
mBlinkTimer.schedule(new BlinkTimerTask(this), 500, interval);
}
public void stopBlink() {
mNeedStopBlink = true;
mIsBlinking = false;
}
protected void finalize() {
if (mBlinkTimer != null)
mBlinkTimer.cancel();
}
public void loadImage(String imageName, State state) throws UnsupportedStateException, IOException {
InputStream is = getContext().getResources().getAssets().open("odin/us/images/buttons/" + imageName);
Bitmap bitmap = BitmapFactory.decodeStream(is);
CustomImage bitmapDrawable = new CustomImage(bitmap);
int[] controlState;
switch(state) {
case Normal:
controlState = new int[]{ android.R.attr.state_enabled , -android.R.attr.state_pressed};
mNormalImageWith = bitmap.getWidth();
mNormalImageHeight = bitmap.getHeight();
mNormalStateDrawable = bitmapDrawable;
break;
case PreNormal:
mNormalStateDrawable.setPrebitmap(bitmap);
return;
case Pressed:
controlState = new int[]{ android.R.attr.state_pressed};
break;
case Disabled:
controlState = new int[]{ -android.R.attr.state_enabled};
break;
default:
throw new UnsupportedStateException("unsupported control state" + state);
}
stateList.addState(controlState, bitmapDrawable);
setBackgroundDrawable(stateList);
}
public int getNormalStateWidth() {
return mNormalImageWith;
}
public int getNormalStateHeight() {
return mNormalImageHeight;
}
}