android Bitmap 원 각 과 그림자 의 구체 적 인 실현 코드
4661 단어 Bitmap원 각거꾸로 비 친 그림자
/**
*
*
* @param bitmap
* @param roundPx
* @return
*/
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
/**
*
*
* @return
*/
public boolean createReflectedImages() {
//
final int reflectionGap = 4;
int index = 0;
for (GalleryWith3DData imageId : mImageIds) {
// bitmap
Bitmap originalImage = BitmapFactory.decodeResource(
mContext.getResources(), imageId.getInteger());
int width = originalImage.getWidth();
int height = originalImage.getHeight();
//
Matrix matrix = new Matrix();
// (x ,y )
matrix.preScale(1, -1);
// , , 1/2
Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
height / 2, width, height / 2, matrix, false);
// , +
Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
(height + height / 2), Config.ARGB_8888);
//
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(getRoundedCornerBitmap(originalImage, 20), 0, 0,
null);
int len = imageId.getstr().length();
double lenWeght = len * 50 * 0.9;
int ban = width / 2;
int ban1 = (int) (lenWeght / 2);
int hua = ban - ban1;
if (imageId.getFlagRecommend()) {
canvas.rotate(30);
canvas.drawText(mStrRecommend, hua - 20, 150,
createPaint(Color.RED));
canvas.rotate(-30);
}
Paint deafaultPaint = new Paint();
deafaultPaint.setAntiAlias(false);
canvas.drawBitmap(getRoundedCornerBitmap(reflectionImage, 20), 0,
height + reflectionGap, null);
Paint paint = new Paint();
paint.setAntiAlias(false);
/**
* : x , : y , : , ,
* Gradient Shader , Paint setShader
*/
LinearGradient shader = new LinearGradient(0,
originalImage.getHeight(), 0,
bitmapWithReflection.getHeight() + reflectionGap,
0x70ffffff, 0x00ffffff, TileMode.MIRROR);
//
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(
android.graphics.PorterDuff.Mode.DST_IN));
//
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);
canvas.drawText(imageId.getstr(), hua, 430,
createPaint(Color.WHITE));
// ImageView bitmapWithReflection
ImageView imageView = new ImageView(mContext);
imageView.setImageBitmap(bitmapWithReflection);
// imageView ,
imageView.setLayoutParams(new GalleryWith3D.LayoutParams(150, 250));
// imageView.setScaleType(ScaleType.MATRIX);
mImages[index++] = imageView;
}
return true;
}
다음은 효과 그림