我设计了一本互动儿童书。我在surfaceview上使用画布,因为我需要动画,我认为最适合于surfaceview
我想让每个单词都可以触摸并大声朗读这个单词。当我使用带有textview的常规视图时,这非常有用。然后我尝试使用画布制作动画,但不知道该怎么做。
我在画布上使用了staticlayout来保存spannable文本。我不知道如何激活onTouchListener。
这里我设置了staticlayout
public class OurView extends SurfaceView implements Runnable, Callback {
public OurView(Context context, AttributeSet attribs) {
super(context, attribs);
init(context);
}
private void init(Context context) {
ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
text = context.getString(R.string.para1);
tpaint = new TextPaint();
tpaint.setColor(Color.WHITE);
tpaint.setStyle(Style.FILL);
tpaint.setTextSize(40F);
TextView tv = splitWords(text);
staticLayout = new StaticLayout(??????????, tpaint, 1000F,
Layout.Alignment.ALIGN_NORMAL, 1, 0, false);
holder = getHolder();
holder.addCallback(this);
}
这是我将文本分成单词并为每个单词做一个触摸式监听器。它目前返回Textview,我试图返回spaanablestring,但我无法激活监听器。我想改变它以满足我的需要。 我不知道怎么做没有textview的tv.setMovementMethod(新的LinkTouchMovementMethod())。
public TextView splitWords(final String text) {
TextView tv = new TextView(getContext());
final SpannableString ssText = new SpannableString(text);
Pattern pattern;
String regex = "\\w+";
pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
final int begin = matcher.start();
final int end = matcher.end();
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
String word = (String) text.subSequence(begin, end)
.toString();
Toast.makeText(getContext(), word, Toast.LENGTH_SHORT)
.show();
return;
}
};
ssText.setSpan(clickableSpan, begin, end, 0);
}
tv.setMovementMethod(new LinkMovementMethod());
tv.setText(text, BufferType.SPANNABLE);
return tv;
}
为了完整性我的DoDraw
private void doDraw(Canvas canvas) {
canvas.drawBitmap(ball, x, 100, null);
incrementX();
canvas.save();
canvas.translate(100.0f, 350.0f);
staticLayout.draw(canvas);
canvas.restore();
}
我希望将textview添加到staticlayout或添加spannableString并激活侦听器。
答案 0 :(得分:2)
您需要在自定义视图中覆盖onTouch方法。请参阅以下代码,其中大部分都是从LinkTouchMovementMethod.onTouchEvent()复制的。
private boolean updateSelection(MotionEvent event, Spannable buffer,Layout layout) {
int action = event.getAction();
if (action == MotionEvent.ACTION_UP ||
action == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= getPaddingLeft();
y -= getPaddingTop();
x += getScrollX();
y += getScrollY();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
if (link.length != 0) {
if (action == MotionEvent.ACTION_UP) {
link[0].onClick(this);
} else {
Selection.setSelection(buffer,
buffer.getSpanStart(link[0]),
buffer.getSpanEnd(link[0]));
}
return true;
} else {
Selection.removeSelection(buffer);
}
}
return false;
在View.onTouchEvent()方法中调用updateSelection方法。
@Override
public boolean onTouchEvent(MotionEvent event) {
updateSelection(event, layout.getText(), layout);
}
请参阅Weex RichText以查看真实演示。