我想做的是
我想创建一个旋转和翻转的EditText视图,它具有普通EditText视图的所有属性。
我的问题
我已经成功地(在许多用户的帮助下)制作了一个既可以旋转又可以翻转的自定义EditText视图。这是通过覆盖onDraw方法来完成的。但是,光标和高亮显示消失了,并且长触摸事件仍然指示原始文本位置。基本上,视图被重新绘制,但触摸事件没有。
如何让触摸事件、高亮显示和光标也被旋转和翻转?
我所读到的
EditText scale with selection (一个类似的问题,但不完全相同。)
How to make a custom Edittext,so that it will look like as 45 degree rotated in android (@CommonsWare指出了一个解决方案,即需要使用触摸事件完成额外的工作。这是什么工作?)
http://developer.android.com/training/graphics/opengl/touch.html (很有帮助,但我不知道如何在这种情况下应用它。)
我尝试过的东西
我制作了一个扩展EditText的自定义视图。其中重写了onDraw方法来旋转和翻转画布。我重写了onMeasure,使视图具有正确的布局尺寸。
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.EditText;
public class MongolEditText extends EditText {
// Constructors
public MongolEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MongolEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MongolEditText(Context context) {
super(context);
init();
}
// This class requires the mirrored Mongolian font to be in the assets/fonts folder
private void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/MongolChagaanMirrored.ttf");
setTypeface(tf);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
@Override
protected void onDraw(Canvas canvas) {
TextPaint textPaint = getPaint();
textPaint.setColor(getCurrentTextColor());
textPaint.drawableState = getDrawableState();
canvas.save();
canvas.translate(getWidth(), 0);
canvas.rotate(90);
canvas.translate(0, getWidth());
canvas.scale(1, -1);
canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());
getLayout().draw(canvas);
canvas.restore();
}
}布局xml没有什么特殊之处。
(更新)这个问题是对它的又一次尝试,但最终我无法让它工作:Does invalidateDrawable() need to be overridden in addition to onDraw()?
进一步解释
如果你想知道为什么我要旋转和翻转EditText视图,这里是原因。传统的蒙古语是从左到右竖排书写的。与垂直镜像的蒙古文字体相结合,将文本顺时针旋转90度并翻转可产生具有正确换行的可读性输出。
这不是一个晦涩难懂或孤立的问题。传统蒙古语有数百万用户,但Android应用程序很少。在这些工具中,我还没有找到任何开源的。如果我能让它工作,我想让代码对其他开发人员可用。
我现在看的地方
(更新)
我在考虑从头开始创建一个自定义视图(它扩展了View)来创建类似TextView的东西。可以从应用程序更新此TextView,使其行为类似于EditText视图。在这种情况下,我只需要用普通字体将文本旋转90度,而不需要翻转它。但是,我必须自己进行换行。
然而,在阅读了@Chitrang的答案后,我想我可以通过扩展一个TextView来做一些类似的事情。这样我就可以省去自己换行的麻烦了。
图片更新

蒙古语是从上到下,从左到右书写的。现在,我正在使用此键盘在文本周围移动光标,但我希望能够触摸屏幕以将光标移动到某个位置。
发布于 2015-04-20 08:56:07
更新
我最终从头开始开发了一个垂直脚本MongolEditText。它是mongol-library的一部分。
在这里,它与两个不同的第三方键盘一起使用。

老答案
这仍然是一项正在进行的工作,所以我不会将其标记为已解决,但请允许我发布我到目前为止所做的工作。它能做我想做的大部分事情。基本上,我使用TextView而不是EditText,因为EditText在旋转时会处理许多奇怪的事情。
我有一个不闪烁的光标,可以响应触摸事件,但仍然不支持高亮显示。代码如下:
public class MongolTextView extends TextView {
private TextPaint textPaint;
private Paint cursorPaint = new Paint();
private boolean mCursorIsVisible;
private CursorTouchLocationListener listener;
// Naming is based on pre-rotated/mirrored values
private float mCursorBaseY;
private float mCursorBottomY;
private float mCursorAscentY; // This is a negative number
private float mCursorX;
private static final float CURSOR_THICKNESS = 2f;
// Constructors
public MongolTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MongolTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MongolTextView(Context context) {
super(context);
init();
}
// This class requires the mirrored Mongolian font to be in the assets/fonts folder
private void init() {
//Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
// "fonts/ChimeeWhiteMirrored.ttf");
//setTypeface(tf);
// Use the above commented code is using a single font in another application
Typeface tf = FontCache.get(SettingsActivity.FONT_DEFAULT, getContext());
if(tf != null) {
setTypeface(tf);
}
this.mCursorIsVisible = true;
cursorPaint.setStrokeWidth(CURSOR_THICKNESS);
cursorPaint.setColor(Color.BLACK); // TODO should be same as text color
}
// This interface may be deleted if touch functionality is not needed
public interface CursorTouchLocationListener {
/**
* Returns the touch location to be used for the cursor so you can update the insert
* location in a text string.
*
* @param glyphIndex
* You will need to translate glyphIndex into a Unicode index if you are using a
* Unicode string.
*/
public void onCursorTouchLocationChanged(int glyphIndex);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// swap the height and width
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
@Override
protected void onDraw(Canvas canvas) {
textPaint = getPaint();
textPaint.setColor(getCurrentTextColor());
textPaint.drawableState = getDrawableState();
canvas.save();
// flip and rotate the canvas
canvas.translate(getWidth(), 0);
canvas.rotate(90);
canvas.translate(0, getWidth());
canvas.scale(1, -1);
canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());
// draw the cursor
if (mCursorIsVisible) {
canvas.drawLine(mCursorX, mCursorBottomY, mCursorX, mCursorBaseY + mCursorAscentY,
cursorPaint);
}
getLayout().draw(canvas);
canvas.restore();
}
public void showCursor(boolean visible) {
mCursorIsVisible = visible;
this.invalidate();
// TODO make the cursor blink
}
public void setCursorColor(int color) {
cursorPaint.setColor(color);
}
public void setCursorLocation(int characterOffset) {
Layout layout = this.getLayout();
if (layout!=null){
try {
// This method is giving a lot of crashes so just surrounding with
// try catch for now
int line = layout.getLineForOffset(characterOffset);
mCursorX = layout.getPrimaryHorizontal(characterOffset);
mCursorBaseY = layout.getLineBaseline(line);
mCursorBottomY = layout.getLineBottom(line);
mCursorAscentY = layout.getLineAscent(line);
this.invalidate();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class InputWindowTouchListener implements OnTouchListener {
@Override
public boolean onTouch(View view, MotionEvent event) {
Layout layout = ((TextView) view).getLayout();
// swapping x and y for touch events
int y = (int) event.getX();
int x = (int) event.getY();
if (layout != null) {
int line = layout.getLineForVertical(y);
int offset = layout.getOffsetForHorizontal(line, x);
mCursorX = layout.getPrimaryHorizontal(offset);
mCursorBaseY = layout.getLineBaseline(line);
mCursorBottomY = layout.getLineBottom(line);
mCursorAscentY = layout.getLineAscent(line);
//mCursorHeightY = layout.getLineTop(line);
view.invalidate();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//handler.postDelayed(mLongPressed, 1000);
listener.onCursorTouchLocationChanged(offset);
break;
case MotionEvent.ACTION_UP:
//handler.removeCallbacks(mLongPressed);
// notify the host activity of the new cursor location
break;
}
}
return false;
}
}
public void setCursorTouchLocationListener(CursorTouchLocationListener listener) {
this.listener = listener;
}
}如果你有更好的东西,请随意添加你自己的答案,或者如果你有什么需要改进的地方(添加高亮显示,让光标闪烁等),可以添加评论。此代码的最新版本应该在github上。
发布于 2014-11-05 20:46:57
EditText有一个用于旋转的属性。这是简单的,也容易的use.It可能会帮助你,我想。
<EditText
android:id="@+id/editTextNumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:rotation="90"
android:layout_marginBottom="10dp"
android:hint="Enter Mobile Number"
android:textAppearance="?android:attr/textAppearanceLarge" />发布于 2014-11-02 22:11:46
事情要简单得多。你需要的一切都已经内置在视图类中了:http://developer.android.com/reference/android/view/View.html#setRotation(float)如果之前的解决方案由于某种原因不适合你,那就是:http://developer.android.com/reference/android/view/animation/RotateAnimation.html它也用于视图的旋转(但在大多数情况下,你可以使用它来实现零过渡持续时间)。
请让我知道,在这种情况下,你有一些额外的问题(猜想没有-有自我解释的方法和类)。
只旋转画布-你只旋转屏幕上的图像。setRotation还可以处理所有的事件,布局流等,所以它在你的情况下应该工作得很好!
希望它能有所帮助!
https://stackoverflow.com/questions/25036816
复制相似问题