我使用下面的9-patch图像来创建投影效果。

对于下面的图像视图,它工作得非常好。
<?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补丁图像中没有像素覆盖了我的原始图片。

然而,当我尝试在相机预览视图上应用完全相同的技术时,事情变得复杂起来。我对API Demo示例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个像素。

我非常确定我有正确的图像文件名dropshadow.9.png为这两个项目。然而,我不知道为什么后者不能像预期的那样工作?
发布于 2012-01-05 20:22:26
这是因为您使用的是一个SurfaceView,它为您提供了一个专用的绘图图面,该图面不会遵守您在九个补丁图像中定义的内容边界。
一种可能的解决方法是将相机的SurfaceView放在一个稍微大一点的视图上,该视图将投影作为背景。
https://stackoverflow.com/questions/8740528
复制相似问题