Android - 使用捏缩放缩放textview的字体大小

时间:2013-04-15 04:12:32

标签: android fonts textview zoom scale

我正在尝试使用双指缩放手势缩放TextView。我有捏缩放工作,但随着文本缩放它变得有点像素化。我认为这是因为实际的字体大小没有缩放。但是,当我尝试将setTextSize()置于onTouch()方法的任何位置时,它会在TextView边缘出现/消失的小线条上变得非常不连贯,并且当缩放比例足够大时文本会消失。我在LinearLayout中有TextView,而缩放缩放会缩放LinearLayout。

这是布局xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF000000" >

<LinearLayout
    android:id="@+id/textLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:text="texthere" />

</LinearLayout>

<View
    android:id="@+id/touchView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</FrameLayout>

这是触控侦听器java:

scaleView.setOnTouchListener(new View.OnTouchListener(){
        public boolean onTouch(View v, MotionEvent event){
            float viewWidth = mTextView.getWidth();
            float viewHeight = mTextView.getHeight();
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                mode = DRAG;
                x_0 = mTextView.getX();//+50;
                y_0 = mTextiew.getY();//+50;
                initialTouchRawX = event.getRawX();
                initialTouchRawY = event.getRawY();
                currentScale = mTextView.getScaleX();
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                oldDist2 = spacing(event);
                if (oldDist2 > 10f) {
                    mode = ZOOM;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                mode = NONE;
                break;
            case MotionEvent.ACTION_MOVE:
                float xmin = x_0 - viewWidth/2*(currentScale-1);
                float xmax = x_0 + viewWidth + viewWidth/2*(currentScale-1);
                float ymin = y_0 - viewHeight/2*(currentScale-1) + viewShift;
                float ymax = y_0 + viewHeight + viewHeight/2*(currentScale-1) + viewShift;
                if (mode == DRAG && initialTouchRawX >= xmin && initialTouchRawX <= xmax && initialTouchRawY >= ymin && initialTouchRawY <= ymax ) { 
                    float mShiftX = -(initialTouchRawX - x_0);
                    float mShiftY = -(initialTouchRawY - y_0);
                    mTextView.setX((float) event.getRawX()+mShiftX);
                    mTextView.setY((float) event.getRawY()+mShiftY);
                } else if (mode == ZOOM) {
                    float newDist2 = spacing(event);
                    if (newDist2 > 10f) {
                        newScale = newDist2 / oldDist2 * currentScale;
                        mTextView.setScaleX(newScale);
                        mTextView.setScaleY(newScale);
                        //***I've tried setting the font size here***
                    }
                }
                break;
            }
            //***I've tried setting the font size here***
            //*** Now I write the scaling float to shared preferences here
            return true; 
        }
    });

我对实际缩放字体大小的位置和方式感到困惑。我已在代码中指出我尝试实现setFontSize()。我的意思是,它确实起作用了,但是就像我上面提到的那样,它的质量很差,文字消失了,而且有时会出现在TextView的边缘。任何提示都非常感谢。

===编辑:===

我在下面实现了一个自定义TextView:

public class ScaleTextView extends TextView {

public ScaleTextView(Context context) {
    this(context, null);
}

public ScaleTextView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public ScaleTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    updateTextSize(context);
}

public void updateTextSize(Context context) {
    getTypeface();
    float currentTextSize = getTextSize();
    SharedPreferences otherSettings = context.getSharedPreferences("settings", 0);
    float newScale = otherSettings.getFloat("key_scaling", 1f);
    setTextSize(newScale * currentTextSize); 
}

}

所以这有点帮助,除了肖像和横向的字体大小现在不同,这导致每次方向改变时缩放。一旦比例足够大,文本仍然会消失,有时文本视图的边缘会有行。也许有更好的方法将缩放变量传递给自定义的ScaleTextView类?

0 个答案:

没有答案