如何放大/移动textview ontouch

时间:2015-02-13 11:47:31

标签: android textview imageview touch gesture

我有这个代码用于旋转/放大和移动图像视图,现在我需要将这些功能应用于文本视图,但我没有成功完成此任务,任何人都可以帮助我做这个代码或其他编辑码?感谢

public class MainActivity extends Activity implements OnTouchListener {
private EditText ed_text;
private ImageView view;
private LinearLayout toolbar_lay;
private FrameLayout frame1;
private Matrix mMatrix = new Matrix();
private float mScaleFactor = .4f;
private float mRotationDegrees = 0.f;
private float mFocusX = 0.f;
private float mFocusY = 0.f;  
private int mAlpha = 255;
private int mImageHeight, mImageWidth;
private ImageView background;
private ScaleGestureDetector mScaleDetector;
private RotateGestureDetector mRotateDetector;
private MoveGestureDetector mMoveDetector;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.frame3);
    background=(ImageView) findViewById(R.id.frame_back);
        Display display = getWindowManager().getDefaultDisplay();
    mFocusX = display.getWidth()/2f;
    mFocusY = display.getHeight()/2f;

    // Set this class as touchListener to the ImageView
     view = (ImageView) findViewById(R.id.id1);
    Intent intent=getIntent();
    String path=intent.getStringExtra("mybm");
    Bitmap bm=BitmapFactory.decodeFile(path);
    view.setImageBitmap(bm);
    view.setOnTouchListener(this);
    // Determine dimensions of 'earth' image
    Drawable d      = this.getResources().getDrawable(R.drawable.icon3);
    mImageHeight    = d.getIntrinsicHeight();
    mImageWidth     = d.getIntrinsicWidth();


    float scaledImageCenterX = (mImageWidth*mScaleFactor)/2; 
    float scaledImageCenterY = (mImageHeight*mScaleFactor)/2;

    mMatrix.postScale(mScaleFactor, mScaleFactor);
    mMatrix.postTranslate(mFocusX - scaledImageCenterX,
   mFocusY - scaledImageCenterY);
    view.setImageMatrix(mMatrix);

    // Setup Gesture Detectors
    mScaleDetector  = new ScaleGestureDetector(getApplicationContext(),
    new ScaleListener());
    mRotateDetector = new RotateGestureDetector(getApplicationContext(), 
  new RotateListener());
    mMoveDetector   = new MoveGestureDetector(getApplicationContext(), 
  new MoveListener());
    //mShoveDetector    = 
    new    ShoveGestureDetector(getApplicationContext(),     
    new ShoveListener());

}

@SuppressWarnings("deprecation")
public boolean onTouch(View v, MotionEvent event) {
    mScaleDetector.onTouchEvent(event);
    mRotateDetector.onTouchEvent(event);
    mMoveDetector.onTouchEvent(event);
  //  mShoveDetector.onTouchEvent(event);

    float scaledImageCenterX = (mImageWidth*mScaleFactor)/2;
    float scaledImageCenterY = (mImageHeight*mScaleFactor)/2;

    mMatrix.reset();
    mMatrix.postScale(mScaleFactor, mScaleFactor);
    mMatrix.postRotate(mRotationDegrees,  
    scaledImageCenterX,   scaledImageCenterY);
    mMatrix.postTranslate(mFocusX - scaledImageCenterX,
       mFocusY - scaledImageCenterY);

    ImageView view = (ImageView) v;
    view.setImageMatrix(mMatrix);
    view.setAlpha(mAlpha);

    return true; // indicate event was handled
  }

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

        mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 10.0f)); 

        return true;
    }
}

private class RotateListener
  extends      RotateGestureDetector.SimpleOnRotateGestureListener 
  {
    @Override
    public boolean onRotate(RotateGestureDetector detector) {
        mRotationDegrees -= detector.getRotationDegreesDelta();
        return true;
    }
}   

private class MoveListener 
 extends    MoveGestureDetector.SimpleOnMoveGestureListener {
    @Override
    public boolean onMove(MoveGestureDetector detector) {
        PointF d = detector.getFocusDelta();
        mFocusX += d.x;
        mFocusY += d.y;     

        // mFocusX = detector.getFocusX();
        // mFocusY = detector.getFocusY();
        return true;
    }
}       


}

1 个答案:

答案 0 :(得分:0)

我认为你有正确的想法将文本视为一个 ImageView的。但是从TextView中删除转换。而是将文本图像一直视为ImageView。创建一个空白的Canvas和Paint,使用Canvas.drawText在Canvas的位图中的适当位置绘制文本,然后使用上面的图像处理代码来展开/缩小/旋转文本。