2013-05-01 19 views
12

Ciao amico Ho fatto pizzicare zoom textview e il suo lavoro bene, ma il problema è quando ridimensiono la posizione della textview cambiata e voglio che la posizione di textview sia fissa durante lo zoom in e zoom out che è il mio codicePizzico Zoom su textview android

public class MainActivity extends Activity { 

    TextView scaleGesture; 
    ScaleGestureDetector scaleGestureDetector; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     scaleGesture = (TextView)findViewById(R.id.article); 
     scaleGesture.setText("this is some text"); 
     scaleGestureDetector = new ScaleGestureDetector(this, new simpleOnScaleGestureListener()); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 


    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     // TODO Auto-generated method stub 
     scaleGestureDetector.onTouchEvent(event); 
     return true; 
    } 

    public class simpleOnScaleGestureListener extends 
      SimpleOnScaleGestureListener { 

     @Override 
     public boolean onScale(ScaleGestureDetector detector) { 
      // TODO Auto-generated method stub 
      float size = scaleGesture.getTextSize(); 
      Log.d("TextSizeStart", String.valueOf(size)); 

      float factor = detector.getScaleFactor(); 
      Log.d("Factor", String.valueOf(factor)); 


      float product = size*factor; 
      Log.d("TextSize", String.valueOf(product)); 
      scaleGesture.setTextSize(TypedValue.COMPLEX_UNIT_PX, product); 

      size = scaleGesture.getTextSize(); 
      Log.d("TextSizeEnd", String.valueOf(size)); 
      return true; 
     } 
    } 
} 
+0

Grazie suo lavoro per me –

risposta

17

ho fatto in questo modo:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/mytv" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:text="This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks" /> 

</RelativeLayout> 

MainActivity.java:

import android.app.Activity; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.widget.TextView; 

public class MyTextViewPinchZoomClass extends Activity implements OnTouchListener { 

    final static float STEP = 200; 
    TextView mytv; 
    float mRatio = 1.0f; 
    int mBaseDist; 
    float mBaseRatio; 
    float fontsize = 13; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mytv = (TextView) findViewById(R.id.mytv); 
     mytv.setTextSize(mRatio + 13); 
    } 

    public boolean onTouchEvent(MotionEvent event) { 
     if (event.getPointerCount() == 2) { 
      int action = event.getAction(); 
      int pureaction = action & MotionEvent.ACTION_MASK; 
      if (pureaction == MotionEvent.ACTION_POINTER_DOWN) { 
       mBaseDist = getDistance(event); 
       mBaseRatio = mRatio; 
      } else { 
       float delta = (getDistance(event) - mBaseDist)/STEP; 
       float multi = (float) Math.pow(2, delta); 
       mRatio = Math.min(1024.0f, Math.max(0.1f, mBaseRatio * multi)); 
       mytv.setTextSize(mRatio + 13); 
      } 
     } 
     return true; 
    } 

    int getDistance(MotionEvent event) { 
     int dx = (int) (event.getX(0) - event.getX(1)); 
     int dy = (int) (event.getY(0) - event.getY(1)); 
     return (int) (Math.sqrt(dx * dx + dy * dy)); 
    } 

    public boolean onTouch(View v, MotionEvent event) { 
     return false; 
    } 
} 

Fatto

+0

Può spiegare che cosa ogni variabile (passo, basedist, baseRatio) rappresentano in questo esempio? – AdamMc331

+0

[Il codice originale] (http://blog.stevepark.org/2012/06/android-java-code-for-pinch-zoom-event.html) –

6
public class MainActivity extends Activity { 

TextView scaleGesture; 
ScaleGestureDetector scaleGestureDetector; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    scaleGesture = (TextView)findViewById(R.id.article); 
    scaleGesture.setText("this is some text"); 
    scaleGestureDetector = new ScaleGestureDetector(this, new simpleOnScaleGestureListener()); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 


@Override 
public boolean onTouchEvent(MotionEvent event) { 
    // TODO Auto-generated method stub 
    scaleGestureDetector.onTouchEvent(event); 
    return true; 
} 

public class simpleOnScaleGestureListener extends 
     SimpleOnScaleGestureListener { 

    @Override 
    public boolean onScale(ScaleGestureDetector detector) { 
     // TODO Auto-generated method stub 
     float size = scaleGesture.getTextSize(); 
     Log.d("TextSizeStart", String.valueOf(size)); 

     float factor = detector.getScaleFactor(); 
     Log.d("Factor", String.valueOf(factor)); 


     float product = size*factor; 
     Log.d("TextSize", String.valueOf(product)); 
     scaleGesture.setTextSize(TypedValue.COMPLEX_UNIT_PX, product); 

     size = scaleGesture.getTextSize(); 
     Log.d("TextSizeEnd", String.valueOf(size)); 
     return true; 
    } 
} 
} 

View

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" > 
<TextView 
    android:id="@+id/article" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

0

Si prega di utilizzare il codice

classe pubblica TouchExampleView extends View { private static final int INVALID_POINTER_ID = -1;

private Drawable mIcon; 
private float mPosX; 
private float mPosY; 

private float mLastTouchX; 
private float mLastTouchY; 
private int mActivePointerId = INVALID_POINTER_ID; 

private ScaleGestureDetector mScaleDetector; 
private float mScaleFactor = 1.f; 

public TouchExampleView(Context context) { 
    this(context, null, 0); 
} 

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

public TouchExampleView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    mIcon = context.getResources().getDrawable(R.drawable.toggle); 
    mIcon.setBounds(0, 0, mIcon.getIntrinsicWidth(), 
      mIcon.getIntrinsicHeight()); 

    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener()); 
} 

@Override 
public boolean onTouchEvent(MotionEvent ev) { 
    // Let the ScaleGestureDetector inspect all events. 
    mScaleDetector.onTouchEvent(ev); 

    final int action = ev.getAction(); 
    switch (action & MotionEvent.ACTION_MASK) { 
    case MotionEvent.ACTION_DOWN: { 
     final float x = ev.getX(); 
     final float y = ev.getY(); 

     mLastTouchX = x; 
     mLastTouchY = y; 
     mActivePointerId = ev.getPointerId(0); 
     break; 
    } 

    case MotionEvent.ACTION_MOVE: { 
     final int pointerIndex = ev.findPointerIndex(mActivePointerId); 
     final float x = ev.getX(pointerIndex); 
     final float y = ev.getY(pointerIndex); 

     // Only move if the ScaleGestureDetector isn't processing a gesture. 
     if (!mScaleDetector.isInProgress()) { 
      final float dx = x - mLastTouchX; 
      final float dy = y - mLastTouchY; 

      mPosX += dx; 
      mPosY += dy; 

      invalidate(); 
     } 

     mLastTouchX = x; 
     mLastTouchY = y; 

     break; 
    } 

    case MotionEvent.ACTION_UP: { 
     mActivePointerId = INVALID_POINTER_ID; 
     break; 
    } 

    case MotionEvent.ACTION_CANCEL: { 
     mActivePointerId = INVALID_POINTER_ID; 
     break; 
    } 

    case MotionEvent.ACTION_POINTER_UP: { 
     final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; 
     final int pointerId = ev.getPointerId(pointerIndex); 
     if (pointerId == mActivePointerId) { 
      // This was our active pointer going up. Choose a new 
      // active pointer and adjust accordingly. 
      final int newPointerIndex = pointerIndex == 0 ? 1 : 0; 
      mLastTouchX = ev.getX(newPointerIndex); 
      mLastTouchY = ev.getY(newPointerIndex); 
      mActivePointerId = ev.getPointerId(newPointerIndex); 
     } 
     break; 
    } 
    } 

    return true; 
} 

@Override 
public void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 

    canvas.save(); 
    canvas.translate(mPosX, mPosY); 
    canvas.scale(mScaleFactor, mScaleFactor); 
    Paint paint = new Paint(); 
    paint.setStyle(Style.FILL); 
    paint.setColor(Color.BLACK); 
    paint.setTextSize(20); 
    canvas.drawText("Hello how are you", mPosX, mPosY, paint); 
    mIcon.draw(canvas); 
    canvas.restore(); 
} 

private class ScaleListener extends 
     ScaleGestureDetector.SimpleOnScaleGestureListener { 
    @Override 
    public boolean onScale(ScaleGestureDetector detector) { 
     mScaleFactor *= detector.getScaleFactor(); 

     // Don't let the object get too small or too large. 
     mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f)); 

     invalidate(); 
     return true; 
    } 
} 

}

Problemi correlati