Android 如何在imageview上添加3d效果

Android 如何在imageview上添加3d效果,android,canvas,imageview,effect,Android,Canvas,Imageview,Effect,如何在imageview上添加阴影效果,如此shreen快照 如何添加这种类型的3d效果i imageview有什么想法 <ImageView android:id="@+id/test_button_image" android:layout_width="fill_parent" android:layout_height="200dp" android:scaleType="fitXY" android:background="@d

如何在imageview上添加阴影效果,如此shreen快照

如何添加这种类型的3d效果i imageview有什么想法

<ImageView
     android:id="@+id/test_button_image"
     android:layout_width="fill_parent"
     android:layout_height="200dp"
     android:scaleType="fitXY"
     android:background="@drawable/border6"
     android:src="@drawable/ic_launcher" >
</ImageView>

第一步 创建一个可绘制的,如下图所示

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item><layer-list>
        <item android:left="3dp" android:top="3dp"><shape>
            <gradient android:angle="270" android:endColor="#E2E2E2" android:startColor="#BABABA" />
            <stroke android:width="3dp" android:color="#BABABA" />

            <corners android:radius="2dp" />

            <padding android:bottom="3dp" android:left="2dp" android:right="3dp" android:top="2dp" />
        </shape></item>
    </layer-list></item>
</selector>
输出

左边的图像没有阴影,右边的图像有阴影

注意

您需要根据需要修改代码。这是我的要求的样品演示

编辑

使用以下代码在图像上应用反射效果

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

    // this will not scale but will flip on the Y axis
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);

    // create a Bitmap with the flip matrix applied to it.
    // we only want the bottom half of the image
    Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
            height / 2, width, height / 2, matrix, false);

    // create a new bitmap with same width but taller to fit reflection
    Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
            (height + height / 2), Config.ARGB_8888);

    // create a new Canvas with the bitmap that's big enough for
    // the image plus gap plus reflection
    Canvas canvas = new Canvas(bitmapWithReflection);
    // draw in the original image
    canvas.drawBitmap(originalImage, 0, 0, null);
    // draw in the gap
    Paint defaultPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
    // draw in the reflection
    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    // create a shader that is a linear gradient that covers the reflection
    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0,
            originalImage.getHeight(), 0, bitmapWithReflection.getHeight()
                    + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    // set the paint to use this shader (linear gradient)
    paint.setShader(shader);
    // set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
            + reflectionGap, paint);

    return bitmapWithReflection;
}

您可以检查此项,您可能会得到您需要的。

尝试此项在imageview上进行镜像反射

public Bitmap getRefelection(Bitmap image) {

// The gap we want between the reflection and the original image

final int reflectionGap = 0;

// Get your bitmap from drawable folder

Bitmap originalImage = image;

int width = originalImage.getWidth();
int height = originalImage.getHeight();

// This will not scale but will flip on the Y axis

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

/*Create a Bitmap with the flip matix applied to it.
We only want the bottom half of the image*/

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

// Create a new bitmap with same width but taller to fit reflection

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

// Create a new Canvas with the bitmap that's big enough for
// the image plus gap plus reflection

Canvas canvas = new Canvas(bitmapWithReflection);

// Draw in the original image

canvas.drawBitmap(originalImage, 0, 0, null);

//Draw the reflection Image

canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

// Create a shader that is a linear gradient that covers the reflection

Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0,
originalImage.getHeight(), 0, bitmapWithReflection.getHeight()
+ reflectionGap, 0x99ffffff, 0x00ffffff, TileMode.CLAMP);

// Set the paint to use this shader (linear gradient)

paint.setShader(shader);

// Set the Transfer mode to be porter duff and destination in

paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

// Draw a rectangle using the paint with our linear gradient

canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
+ reflectionGap, paint);
if(originalImage!=null && originalImage.isRecycled()){
originalImage.recycle();
originalImage=null;
}
if(reflectionImage!=null && reflectionImage.isRecycled()){
reflectionImage.recycle();
reflectionImage=null;
}
return bitmapWithReflection;
}
}

你试过什么?仅仅创建一个imageView实际上并不符合您的要求。我在我的一个应用程序中进行了尝试。它怎么会没有任何效果。。?请查看我编辑的输出..我不想要灰色e阴影我想要底部的镜像阴影,就像我的截图一样
public Bitmap getRefelection(Bitmap image) {

// The gap we want between the reflection and the original image

final int reflectionGap = 0;

// Get your bitmap from drawable folder

Bitmap originalImage = image;

int width = originalImage.getWidth();
int height = originalImage.getHeight();

// This will not scale but will flip on the Y axis

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

/*Create a Bitmap with the flip matix applied to it.
We only want the bottom half of the image*/

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

// Create a new bitmap with same width but taller to fit reflection

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

// Create a new Canvas with the bitmap that's big enough for
// the image plus gap plus reflection

Canvas canvas = new Canvas(bitmapWithReflection);

// Draw in the original image

canvas.drawBitmap(originalImage, 0, 0, null);

//Draw the reflection Image

canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

// Create a shader that is a linear gradient that covers the reflection

Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0,
originalImage.getHeight(), 0, bitmapWithReflection.getHeight()
+ reflectionGap, 0x99ffffff, 0x00ffffff, TileMode.CLAMP);

// Set the paint to use this shader (linear gradient)

paint.setShader(shader);

// Set the Transfer mode to be porter duff and destination in

paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

// Draw a rectangle using the paint with our linear gradient

canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
+ reflectionGap, paint);
if(originalImage!=null && originalImage.isRecycled()){
originalImage.recycle();
originalImage=null;
}
if(reflectionImage!=null && reflectionImage.isRecycled()){
reflectionImage.recycle();
reflectionImage=null;
}
return bitmapWithReflection;
}
}