在android中显示带镜像的副本图像

时间:2012-11-30 11:36:13

标签: android imageview

enter image description here

我想在android中使用上面的图像视图。

请为我提供更好的解决方案。

2 个答案:

答案 0 :(得分:10)

我使用以下代码来实现此效果

public Bitmap applyReflection(Drawable drawable) {
    Bitmap originalImage = ((BitmapDrawable)drawable).getBitmap();
    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, 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(originalImage, 0, 0, null);
    Paint defaultPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
    canvas.drawBitmap(reflectionImage,0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0,

            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

如果您在使用上述代码时遇到任何问题,请与我们联系。

致谢:http://xjaphx.wordpress.com/learning/tutorials/

答案 1 :(得分:8)

试试这个:

public static Bitmap reflection(Bitmap mainImage) {
        // gap space between original and reflected
        final int reflectionGap = 4;
        // get image size
        int width = mainImage.getWidth();
        int height = mainImage.getHeight();

        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);

        Bitmap reflectionImage = Bitmap.createBitmap(mainImage, 0,
                height / 2, width, height / 2, matrix, false);

        Bitmap reflectedBitmap = Bitmap.createBitmap(width,
                (height + height / 2), Config.ARGB_8888);

        Canvas canvas = new Canvas(reflectedBitmap);
        canvas.drawBitmap(mainImage, 0, 0, null);
        Paint defaultPaint = new Paint();
        canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
        canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0,
                mainImage.getHeight(), 0, reflectedBitmap.getHeight()
                        + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
        paint.setShader(shader);
        paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
        canvas.drawRect(0, height, width, reflectedBitmap.getHeight()
                + reflectionGap, paint);

        return reflectedBitmap;
    }