9补丁投影技术适用于图像视图,但不适用于相机预览

时间:2012-01-05 09:56:08

标签: android

我用来跟随9-patch图像来创建阴影效果。

enter image description here

它适用于以下图像视图。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#FFFFFF"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center" >
    <ImageView
            android:id="@+id/image_test"
            android:background="@drawable/dropshadow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/pic1"
            />    
</LinearLayout>

我能够获得完美的阴影效果。请注意,图片右/底部有一条1像素宽的红线。我故意将1像素宽的红线放在图片右/底部周围,以证明9张贴片图像中没​​有像素掩盖了我的原始图片。

enter image description here

然而,当我尝试在相机预览视图上应用完全相同的技术时,事情就变得复杂了。我对API演示示例com.example.android.apis.graphics.CameraActivity

进行了一些修改

我通过执行以下修改来修改GUI视图。

class Preview extends ViewGroup implements SurfaceHolder.Callback {
    // ...
    Preview(Context context) {
        // ...
        mSurfaceView.setBackgroundDrawable(getResources().getDrawable(drawable.dropshadow));
        this.setBackgroundColor(Color.WHITE);
        // ...
    }
}

我得到以下结果。请注意,在图片右边缘和底边缘结束之前,阴影效果开始出现。在到达边缘之前,它的距离是9像素。

enter image description here

我很确定我为这两个项目提供了正确的图像文件名dropshadow.9.png。但是,我不明白为什么后者不能按预期工作?

1 个答案:

答案 0 :(得分:4)

这是因为您正在使用SurfaceView,它为您提供了一个专用的绘图表面,它不会尊重您在九个补丁图像中定义的内容边界。

一种可行的解决方法是将相机的SurfaceView置于稍微大一点的视图上,该视图将投影作为背景。