Issue
I am trying to implement my own cropping screen.
See below image:
The layout has a background and the image has listeners to move around the screen and zoom. I have a framelayout for the 'cropping transparency layer' and frame.
This is what I have so far:
<?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>
I am not the most familiar with drawable. What I am doing is drawing 4 different boxes, one on top and bottom, and 2 for each side of the frame.
Is this the correct way to do this or could it be done with less code?
It's important to note that I will support dynamic shaped frames so I will ultimately be changing the values in Java code.
Thanks for any assistance.
Solution
I was able to do this with code:
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;
}
Answered By - Bill Shakespeare
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.