我有一个带有Hello文本的旋转textview。当textview被拖到屏幕的右侧时,textview是从右侧切割的吗?由于单行设置为真实文本未被切断,但文本视图与洋红色颜色被切断。底面也是如此。对于屏幕的左侧和顶部,它不会被切断。
parentLayout = new LinearLayout(this);
parentLayout.setId(6);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
parentLayout.setLayoutParams(params);
layout.addView(parentLayout);
objTextView = new TextView(this);
objTextView.setId(5);
objTextView.setText("Hello");
objTextView.setTextSize(60);
objTextView.setMaxLines(1);
objTextView.setSingleLine(true);
objTextView.setTextColor(Color.WHITE);
objTextView.setBackgroundColor(Color.MAGENTA);
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
parentLayout.addView(objTextView,params);
parentLayout.setOnTouchListener(MyOnTouchListener);
OnTouchListener MyOnTouchListener = new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
// When single finger is down.
case MotionEvent.ACTION_DOWN:
//Start is the point from where we start moving/draging/scaling.
if(view instanceof LinearLayout)
{
start.set(event.getX() - rlLayoutParamsCanvas.leftMargin, event.getY() - rlLayoutParamsCanvas.topMargin);
}
Log.d(TAG, "mode=DRAG");
mode = DRAG;
break;
//When single finger is up.
case MotionEvent.ACTION_UP:
//When second finger is up.
case MotionEvent.ACTION_POINTER_UP:
break;
//When 2 fingers are down.
case MotionEvent.ACTION_POINTER_DOWN:
if(view instanceof LinearLayout){
fTextViewSize = objTextView.getTextSize();
}
break;
//When fingers move.
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) // drag view
{
if(view instanceof LinearLayout)
{
rlLayoutParamsCanvas.leftMargin = (int) (event.getX() - start.x);
rlLayoutParamsCanvas.topMargin = (int) (event.getY() - start.y);
objTextView.setLayoutParams(rlLayoutParamsCanvas);
}
}
//When two fingers are down the mode = zoom, means we can now zoom and rotate as well
else if (mode == ZOOM && event.getPointerCount() == 2) {
float newDist = CommonMethods.spacing(event);
if (view instanceof LinearLayout)
{
objTextView.setTextSize(scale * fTextViewSize);
}
//Rotate watermark/textview.
if (lastEvent != null)
{
//Calculate rotation
newRotation = CommonMethods.rotation(event);
float rotation = newRotation - fingersDownRot;
if (view instanceof LinearLayout)
{
objTextView.setRotation(SetRotation(rotation));
}
}
}
break;
}
更新图片。
答案 0 :(得分:6)
使用相同的代码进行调试和模拟时,您会遇到以下问题:
rlLayoutParamsCanvas.leftMargin = (int) (event.getX() - start.x);
rlLayoutParamsCanvas.topMargin = (int) (event.getY() - start.y);
objTextView.setLayoutParams(rlLayoutParamsCanvas);
现在你说的是设置它的边距,如果它超过了屏幕的宽度,那么切断它的一部分,你可以通过设置它的边距比它的父节点更多地以常规视图来尝试它切掉它的一部分。
<强>溶液强>
而不是仅使用layoutparams
移动您的textview,使用其setX
和setY
来更改屏幕上的位置
<强>样品:强>
if (mode == DRAG) // drag view
{
if(view instanceof LinearLayout)
{
objTextView.setY((int) (event.getY() - start.y));
objTextView.setX((int) (event.getX() - start.x));
}
}