创建裁剪视图
Creating a crop view
我正在尝试实现自己的裁剪屏幕。
见下图:
布局有背景,图像有听众在屏幕上移动和缩放。我有 'cropping transparency layer' 和 frame.
的框架布局
这是我目前拥有的:
<?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 个不同的盒子,一个在顶部和底部,框架的每一侧各 2 个。
这是执行此操作的正确方法还是可以用更少的代码完成?
请务必注意,我将支持动态形状的框架,因此我最终会更改 Java 代码中的值。
感谢您的帮助。
我可以用代码做到这一点:
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;
}
我正在尝试实现自己的裁剪屏幕。
见下图:
布局有背景,图像有听众在屏幕上移动和缩放。我有 'cropping transparency layer' 和 frame.
的框架布局这是我目前拥有的:
<?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 个不同的盒子,一个在顶部和底部,框架的每一侧各 2 个。
这是执行此操作的正确方法还是可以用更少的代码完成?
请务必注意,我将支持动态形状的框架,因此我最终会更改 Java 代码中的值。
感谢您的帮助。
我可以用代码做到这一点:
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;
}