import
android.content.Context;
import
android.graphics.Bitmap;
import
android.graphics.Bitmap.Config;
import
android.graphics.BitmapFactory;
import
android.graphics.Canvas;
import
android.graphics.LinearGradient;
import
android.graphics.Matrix;
import
android.graphics.Paint;
import
android.graphics.PorterDuff.Mode;
import
android.graphics.PorterDuffXfermode;
import
android.graphics.Shader.TileMode;
import
android.graphics.drawable.BitmapDrawable;
import
android.util.AttributeSet;
import
android.widget.ImageView;
public
class
ReflectionImage
extends
ImageView
{
private
boolean
mReflectionMode =
true
;
public
ReflectionImage(Context context)
{
super
(context);
}
public
ReflectionImage(Context context, AttributeSet attrs)
{
super
(context, attrs);
BitmapDrawable bd = (BitmapDrawable)
this
.getDrawable();
Bitmap originalImage = bd.getBitmap();
DoReflection(originalImage);
}
public
ReflectionImage(Context context, AttributeSet attrs,
int
defStyle)
{
super
(context, attrs, defStyle);
Bitmap originalImage = ((BitmapDrawable)
this
.getDrawable()).getBitmap();
DoReflection(originalImage);
}
public
void
setReflectionMode(
boolean
isRef)
{
mReflectionMode = isRef;
}
public
boolean
getReflectionMode()
{
return
mReflectionMode;
}
@Override
public
void
setImageResource(
int
resId)
{
Bitmap originalImage = BitmapFactory.decodeResource(getResources(), resId);
DoReflection(originalImage);
}
private
void
DoReflection(Bitmap originalImage)
{
final
int
reflectionGap =
4
;
int
width = originalImage.getWidth();
int
height = originalImage.getHeight();
Matrix matrix =
new
Matrix();
matrix.preScale(
1
, -
1
);
Bitmap reflectionImage = Bitmap.createBitmap(originalImage,
0
,
0
, width, height, matrix,
false
);
Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height), Config.ARGB_8888);
Canvas canvasRef =
new
Canvas(bitmapWithReflection);
canvasRef.drawBitmap(originalImage,
0
,
0
,
null
);
Paint deafaultPaint =
new
Paint();
canvasRef.drawRect(
0
, height, width, height + reflectionGap, deafaultPaint);
canvasRef.drawBitmap(reflectionImage,
0
, height + reflectionGap,
null
);
Paint paint =
new
Paint();
LinearGradient shader =
new
LinearGradient(
0
, originalImage.getHeight(),
0
, bitmapWithReflection.getHeight() + reflectionGap,
0x80ffffff
,
0x00ffffff
,
TileMode.CLAMP);
paint.setShader(shader);
paint.setXfermode(
new
PorterDuffXfermode(Mode.DST_IN));
canvasRef.drawRect(
0
, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);
this
.setImageBitmap(bitmapWithReflection);
}
}