我正在尝试实现自己的裁剪屏幕。
见下图:
布局有背景,图像有侦听器在屏幕上移动和缩放。我有'裁剪透明层'和框架的framelayout。
这是我到目前为止所做的:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- This is the transparent border -->
<item
android:id="@+id/crop_transparent_top"
android:bottom="550dp">
<shape android:shape="rectangle" >
<solid android:color="#AA0A0A0A" />
</shape>
</item>
<item
android:id="@+id/crop_transparent_bottom"
android:top="550dp">
<shape android:shape="rectangle" >
<solid android:color="#AA0A0A0A" />
</shape>
</item>
<item
android:id="@+id/crop_transparent_right"
android:bottom="110dp"
android:left="1000dp"
android:top="110dp">
<shape android:shape="rectangle" >
<solid android:color="#AA0A0A0A" />
</shape>
</item>
<item
android:id="@+id/crop_transparent_left"
android:bottom="110dp"
android:right="1000dp"
android:top="110dp">
<shape android:shape="rectangle" >
<solid android:color="#AA0A0A0A" />
</shape>
</item>
<!-- This is a border -->
<item
android:id="@+id/crop_frame"
android:bottom="100dp"
android:left="10dp"
android:right="10dp"
android:top="100dp">
<nine-patch android:src="@drawable/img_border" />
</item>
</layer-list>
我不熟悉drawable。我正在做的是绘制4个不同的盒子,一个在顶部和底部,另外两个用于框架。
这是正确的方法,还是可以用更少的代码完成?
重要的是要注意我将支持动态形状的框架,所以我最终将改变Java代码中的值。
感谢您的帮助。
答案 0 :(得分:0)
我能用代码执行此操作:
private Drawable createBackground(int widthForRatio, int heightForRatio, int displayWidth, int displayHeight) {
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.img_border);
NinePatchDrawable frame = new NinePatchDrawable(getResources(), b, b.getNinePatchChunk(), new Rect(), null);
Drawable[] layers = { getTransparent(), getTransparent(), getTransparent(), getTransparent(), frame };
LayerDrawable layerDrawable = new LayerDrawable(layers);
final TypedArray styledAttributes = getTheme().obtainStyledAttributes(new int[] { android.R.attr.actionBarSize });
int mActionBarSize = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();
layerDrawable.setLayerInset(0, 0, 0, 0, displayHeight - 100 - mActionBarSize);
layerDrawable.setLayerInset(1, 0, displayHeight - 100 - mActionBarSize, 0, 0);
layerDrawable.setLayerInset(2, displayWidth - 50, 100, 0, 100);
layerDrawable.setLayerInset(3, 0, 100, displayWidth - 50, 100);
layerDrawable.setLayerInset(4, 50, 100, 50, 100);
return layerDrawable;
}
private ShapeDrawable getTransparent() {
ShapeDrawable transparent = new ShapeDrawable();
transparent.getPaint().setColor(Color.parseColor("#AA0A0A0A"));
return transparent;
}