1
0
forked from GitHub/gf-core

scroll bars for the ParseTreeView

This commit is contained in:
kr.angelov
2014-07-03 13:42:26 +00:00
parent e0fa954256
commit 272a8487f8
6 changed files with 68 additions and 19 deletions

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.grammaticalframework.ui.android" package="org.grammaticalframework.ui.android"
android:versionCode="1" android:versionCode="2"
android:versionName="1.0" android:versionName="1.0"
android:installLocation="auto" > android:installLocation="auto" >

View File

@@ -21,7 +21,7 @@
<org.grammaticalframework.ui.android.ParseTreeView <org.grammaticalframework.ui.android.ParseTreeView
android:id="@+id/desc_details" android:id="@+id/desc_details"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/alternative_desc" android:layout_below="@id/alternative_desc"
android:textSize="25sp" android:textSize="25sp"

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Theme">
<attr name="parseTreeViewStyle" format="reference"/>
</declare-styleable>
</resources>

View File

@@ -15,6 +15,10 @@
<!-- Application theme. --> <!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme"> <style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. --> <!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="parseTreeViewStyle">@style/ParseTreeViewStyle</item>
</style>
<style name="ParseTreeViewStyle">
<item name="android:scrollbars">horizontal</item>
</style> </style>
</resources> </resources>

View File

@@ -141,7 +141,7 @@ public class AlternativesActivity extends ListActivity {
parseView = new ParseTreeView(this); parseView = new ParseTreeView(this);
parseView.setId(R.id.desc_details); parseView.setId(R.id.desc_details);
RelativeLayout.LayoutParams params = RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.alternative_desc); params.addRule(RelativeLayout.BELOW, R.id.alternative_desc);
((RelativeLayout) view).addView(parseView, params); ((RelativeLayout) view).addView(parseView, params);
} }

View File

@@ -1,17 +1,15 @@
package org.grammaticalframework.ui.android; package org.grammaticalframework.ui.android;
import java.util.HashMap;
import java.util.Map;
import android.content.Context; import android.content.Context;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.PointF; import android.graphics.PointF;
import android.graphics.Rect; import android.graphics.Rect;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Pair;
import android.util.SparseArray; import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View; import android.view.View;
import org.grammaticalframework.pgf.Bracket; import org.grammaticalframework.pgf.Bracket;
@@ -24,20 +22,20 @@ public class ParseTreeView extends View {
private Paint paint; private Paint paint;
private Object[] brackets; private Object[] brackets;
private float lastMotionX;
private float scrollRange;
public ParseTreeView(Context context) { public ParseTreeView(Context context) {
super(context); this(context, null);
initialize();
} }
public ParseTreeView(Context context, AttributeSet attrs) { public ParseTreeView(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs, R.attr.parseTreeViewStyle);
initialize();
}
private void initialize() {
paint = new Paint(); paint = new Paint();
paint.setTextSize(60); paint.setTextSize(60);
brackets = null; brackets = null;
scrollRange = 0;
} }
public Object[] getBrackets() { public Object[] getBrackets() {
@@ -45,7 +43,8 @@ public class ParseTreeView extends View {
} }
public void setBrackets(Object[] brackets) { public void setBrackets(Object[] brackets) {
this.brackets = brackets; this.brackets = brackets;
awakenScrollBars();
} }
static class MeasureResult { static class MeasureResult {
@@ -150,12 +149,24 @@ public class ParseTreeView extends View {
height = mr.height; height = mr.height;
} }
width += 10; height += paint.getFontMetrics().descent;
height += 10;
int w = getPaddingLeft() + (int) width + getPaddingRight(); int w = getPaddingLeft() + (int) width + getPaddingRight();
int h = getPaddingTop() + (int) height + getPaddingBottom(); int h = getPaddingTop() + (int) height + getPaddingBottom();
scrollRange = w+80;
int widthReq = MeasureSpec.getSize(widthMeasureSpec);
if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY ||
(MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.AT_MOST && w > widthReq)) {
w = widthReq;
}
int heightReq = MeasureSpec.getSize(heightMeasureSpec);
if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY ||
(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST && h > heightReq)) {
h = heightReq;
}
setMeasuredDimension(w, h); setMeasuredDimension(w, h);
} }
@@ -218,4 +229,32 @@ public class ParseTreeView extends View {
startX += w + SISTER_SKIP; startX += w + SISTER_SKIP;
} }
} }
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
// Remember where the motion event started
lastMotionX = ev.getX();
break;
case MotionEvent.ACTION_MOVE:
// Scroll to follow the motion event
float x = ev.getX();
final int deltaX = (int) (lastMotionX - x);
lastMotionX = x;
final int offset = computeHorizontalScrollOffset() + deltaX;
final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
if (range > 0 && offset > 0 && offset < range)
scrollTo(offset, 0);
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
@Override
protected int computeHorizontalScrollRange() {
return (int) scrollRange;
}
} }