捏缩放文本视图

时间:2012-03-29 10:21:48

标签: android

我想为文本视图执行捏合功能(放大/缩小)。

我已经参考了许多教程,但没有得到任何富有成效的..请帮助   我,如果有人这样做或知道怎么做....

使用按钮作为放大和/或使用2指针捏还可以吗?

任何帮助将不胜感激..   感谢

无法使用代码

  setContentView(R.layout.main);
  mainView =(LinearLayout)findViewById(R.id.linearLayout);

  Button buttonZoomOut = (Button)findViewById(R.id.buttonZoomOut);
  Button buttonNormal = (Button)findViewById(R.id.buttonNormal);
  Button buttonZoomIn = (Button)findViewById(R.id.buttonZoomIn);

  buttonZoomOut.setOnClickListener(new View.OnClickListener() {    
   public void onClick(View v) {
    zoom(0.5f,0.5f,new PointF(0,0));    
   }
  });
  buttonNormal.setOnClickListener(new OnClickListener() {

   public void onClick(View v) {
    zoom(1f,1f,new PointF(0,0));  
   }
  });
  buttonZoomIn.setOnClickListener(new View.OnClickListener() {

   public void onClick(View v) {
    zoom(2f,2f,new PointF(0,0));   
   }
  });
 }

 /** zooming is done from here */
 public void zoom(Float scaleX,Float scaleY,PointF pivot){
  mainView.setPivotX(pivot.x);
  mainView.setPivotY(pivot.y);  
  mainView.setScaleX(scaleX);
  mainView.setScaleY(scaleY);  
 } 

2 个答案:

答案 0 :(得分:3)

如果您想实施手指捏,您可以看到Google开发者博客提供的sample code

我之前尝试过。它有效且有益于学习。

答案 1 :(得分:0)

您可以尝试以下代码.... 它将TextView转换为可以缩放它的图像。

public class temp extends Activity实现OnTouchListener {

private static final String TAG = "Touch";

private static final float MIN_ZOOM = 1.0f;

private static final float MAX_ZOOM = 5.0f;

// These matrices will be used to move and zoom image

Matrix matrix = new Matrix();

Matrix savedMatrix = new Matrix();

// We can be in one of these 3 states

static final int NONE = 0;

static final int DRAG = 1;

static final int ZOOM = 2;

int mode = NONE;

// Remember some things for zooming

PointF start = new PointF();

PointF mid = new PointF();

float oldDist = 1f;

// public static ArrayList str;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

TextView tv = new TextView(this);
    LinearLayout.LayoutParams layoutParamsForText = new LinearLayout.LayoutParams(
            width,height);
    tv.setLayoutParams(layoutParamsForText);

    Bitmap testB;

    testB = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(testB);
    tv.layout(0, 0, width, height);
    tv.draw(c);

    ImageView view = (ImageView) findViewById(R.id.test_X);
    view.setLayoutParams(layoutParamsForText);
    view.setBackgroundColor(Color.WHITE);
    view.setImageBitmap(testB);
    view.setMaxHeight(height);
    view.setMaxWidth(width);
    // ImageView view = (ImageView) findViewById(R.id.test_X);

    // view.setLayoutParams(new GridView.LayoutParams(85, 85));

    view.setScaleType(ImageView.ScaleType.FIT_CENTER);

    view.setOnTouchListener(this);
}

public boolean onTouch(View v, MotionEvent event) {

    ImageView view = (ImageView) v;

    view.setScaleType(ImageView.ScaleType.MATRIX);

    float scale;

    // Dump touch event to log

    dumpEvent(event);

    // Handle touch events here...

    switch (event.getAction() & MotionEvent.ACTION_MASK) {

    case MotionEvent.ACTION_DOWN: // first finger down only

        savedMatrix.set(matrix);

        start.set(event.getX(), event.getY());

        Log.d(TAG, "mode=DRAG");

        mode = DRAG;

        break;

    case MotionEvent.ACTION_UP: // first finger lifted

    case MotionEvent.ACTION_POINTER_UP: // second finger lifted

        mode = NONE;

        Log.d(TAG, "mode=NONE");

        break;

    case MotionEvent.ACTION_POINTER_DOWN: // second finger down

        oldDist = spacing(event);

        Log.d(TAG, "oldDist=" + oldDist);

        if (oldDist > 5f) {

            savedMatrix.set(matrix);

            midPoint(mid, event);

            mode = ZOOM;

            Log.d(TAG, "mode=ZOOM");

        }

        break;

    case MotionEvent.ACTION_MOVE:

        if (mode == DRAG) { // movement of first finger

            matrix.set(savedMatrix);

            if (view.getLeft() >= -392) {

                matrix.postTranslate(event.getX() - start.x, event.getY()
                        - start.y);

            }

        }

        else if (mode == ZOOM) { // pinch zooming

            float newDist = spacing(event);

            Log.d(TAG, "newDist=" + newDist);

            if (newDist > 5f) {

                matrix.set(savedMatrix);

                scale = newDist / oldDist; /*
                                             * thinking i need to play
                                             * around with this value to
                                             * limit it
                                             */

                matrix.postScale(scale, scale, mid.x, mid.y);

            }

        }

        break;

    }

    // Perform the transformation

    view.setImageMatrix(matrix);

    return true; // indicate event was handled

}

private float spacing(MotionEvent event) {

    float x = event.getX(0) - event.getX(1);

    float y = event.getY(0) - event.getY(1);

    return FloatMath.sqrt(x * x + y * y);

}

private void midPoint(PointF point, MotionEvent event) {

    float x = event.getX(0) + event.getX(1);

    float y = event.getY(0) + event.getY(1);

    point.set(x / 2, y / 2);

}

/** Show an event in the LogCat view, for debugging */

private void dumpEvent(MotionEvent event) {

    String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE",

    "POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };

    StringBuilder sb = new StringBuilder();

    int action = event.getAction();

    int actionCode = action & MotionEvent.ACTION_MASK;

    sb.append("event ACTION_").append(names[actionCode]);

    if (actionCode == MotionEvent.ACTION_POINTER_DOWN

    || actionCode == MotionEvent.ACTION_POINTER_UP) {

        sb.append("(pid ").append(

        action >> MotionEvent.ACTION_POINTER_ID_SHIFT);

        sb.append(")");

    }

    sb.append("[");

    for (int i = 0; i < event.getPointerCount(); i++) {

        sb.append("#").append(i);

        sb.append("(pid ").append(event.getPointerId(i));

        sb.append(")=").append((int) event.getX(i));

        sb.append(",").append((int) event.getY(i));

        if (i + 1 < event.getPointerCount())

            sb.append(";");

    }

    sb.append("]");

    Log.d(TAG, sb.toString());

}