Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down Expand Up @@ -43,6 +44,13 @@
android:id="@+id/paintView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:showCanvasBounds="true"
app:strokeColor="@android:color/darker_gray"
app:maxZoom="3"
app:minZoom="1"
app:canvasHeight="1080px"
app:canvasWidth="1920px"
app:strokeWidth="15"
/>

</LinearLayout>
33 changes: 33 additions & 0 deletions library/src/main/java/me/panavtec/drawableview/DrawableView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.os.Build;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
Expand Down Expand Up @@ -49,17 +52,20 @@ public DrawableView(Context context) {
public DrawableView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
setAttributeSet(context, attrs);
}

public DrawableView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
setAttributeSet(context, attrs);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP) public DrawableView(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
setAttributeSet(context, attrs);
}

private void init() {
Expand All @@ -74,6 +80,33 @@ private void init() {
setOnTouchListener(this);
}

private void setAttributeSet(Context context, AttributeSet attributeSet){

TypedArray a = context.getTheme().obtainStyledAttributes(
attributeSet,
R.styleable.DrawableView,
0, 0);

try {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int windowWidth = metrics.widthPixels;
int windowHeight = metrics.heightPixels;

DrawableViewConfig config = new DrawableViewConfig();
config.setCanvasHeight(a.getDimensionPixelSize(R.styleable.DrawableView_canvasHeight, windowHeight));
config.setCanvasWidth(a.getDimensionPixelSize(R.styleable.DrawableView_canvasWidth, windowWidth));
config.setStrokeColor(a.getColor(R.styleable.DrawableView_strokeColor, context.getResources().getColor(android.R.color.black)));
config.setShowCanvasBounds(a.getBoolean(R.styleable.DrawableView_showCanvasBounds, true));
config.setMinZoom(a.getFloat(R.styleable.DrawableView_minZoom, 1.0f));
config.setMaxZoom(a.getFloat(R.styleable.DrawableView_maxZoom, 3.0f));
config.setStrokeWidth(a.getFloat(R.styleable.DrawableView_strokeWidth, 20f));
setConfig(config);

} finally {
a.recycle();
}
}

public void setConfig(DrawableViewConfig config) {
if (config == null) {
throw new IllegalArgumentException("Paint configuration cannot be null");
Expand Down
12 changes: 12 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="DrawableView">
<attr name="canvasWidth" format="dimension" />
<attr name="canvasHeight" format="dimension" />
<attr name="maxZoom" format="float" />
<attr name="minZoom" format="float" />
<attr name="strokeWidth" format="float" />
<attr name="showCanvasBounds" format="boolean" />
<attr name="strokeColor" format="color" />
</declare-styleable>
</resources>