2012-06-08 59 views
2

我有一個線性佈局的活動,在這個佈局中有幾個線性佈局,每個線性佈局都設置了按鈕和文本視圖。如果用戶使用他的手指進行放大縮小,則我想要實現整個屏幕方式的多點觸摸功能,然後它應該放大並縮小整個屏幕(增加和減少所有按鈕,相應文本視圖大小一次)。如何實現整個活動的多點觸控?

如何使用android 2.1實現它?

問候, 皮克斯

+0

有你解決了這個問題。 。 。 。我需要相同的解決方案。 。 .plz幫助我 –

回答

0

這可能會給你一個想法:

import java.util.ArrayList; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.DashPathEffect; 
import android.graphics.Paint; 
import android.graphics.PointF; 
import android.util.AttributeSet; 
//import android.util.FloatMath; 
import android.view.MotionEvent; 
import android.view.View; 

public class MultitouchView extends View { 
    private static final int STROKE_WIDTH = 1; 
    private static final int CIRCLE_RADIUS = 20; 

    private ArrayList<PointF> touchPoints = null; 
    private Paint drawingPaint = null; 
    private boolean isMultiTouch = false; 
    private int pathEffectPhase = 0; 

    public MultitouchView(Context context) { 
     super(context); 

     initialize(context); 
    } 

    public MultitouchView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 

     initialize(context); 
    } 

    public MultitouchView(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     initialize(context); 
    } 

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

     if(touchPoints.size() > 0) 
     { 
      DashPathEffect effect = new DashPathEffect(new float[] {7,7}, pathEffectPhase); 
      PointF midpt = null; 

      drawingPaint.setPathEffect(effect); 

      for(int index=1; index<touchPoints.size(); ++index) 
      { 
       midpt = getMidPoint(
         touchPoints.get(index - 1).x,touchPoints.get(index - 1).y, 
         touchPoints.get(index).x,touchPoints.get(index).y); 

       canvas.drawCircle(
         touchPoints.get(index - 1).x,touchPoints.get(index - 1).y, 
         1, drawingPaint); 
       canvas.drawCircle(
         touchPoints.get(index - 1).x,touchPoints.get(index - 1).y, 
         CIRCLE_RADIUS, drawingPaint); 

       canvas.drawCircle(touchPoints.get(index).x,touchPoints.get(index).y, 
         1, drawingPaint); 
       canvas.drawCircle(touchPoints.get(index).x,touchPoints.get(index).y, 
         CIRCLE_RADIUS, drawingPaint); 

       canvas.drawLine(
         touchPoints.get(index - 1).x,touchPoints.get(index - 1).y, 
         touchPoints.get(index).x,touchPoints.get(index).y, 
         drawingPaint); 

       canvas.drawCircle(midpt.x,midpt.y, 10, drawingPaint); 
      } 

      ++pathEffectPhase; 

      invalidate(); 
     } 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     super.onTouchEvent(event); 

     int action = event.getAction() & MotionEvent.ACTION_MASK; 

     switch(action) 
     { 
      case MotionEvent.ACTION_DOWN: 
      { 
       invalidate(); 

       break; 
      } 
      case MotionEvent.ACTION_POINTER_DOWN: 
      { 
       isMultiTouch = true; 

       setPoints(event); 
       invalidate(); 

       break; 
      } 
      case MotionEvent.ACTION_POINTER_UP: 
      { 
       isMultiTouch = false; 

       break; 
      } 
      case MotionEvent.ACTION_MOVE: 
      { 
       if(isMultiTouch) 
       { 
        setPoints(event); 
        invalidate(); 
       } 

       break; 
      } 
     } 

     return true; 
    } 

    private void initialize(Context context){ 
     drawingPaint = new Paint(); 

     drawingPaint.setColor(Color.RED); 
     drawingPaint.setStrokeWidth(STROKE_WIDTH); 
     drawingPaint.setStyle(Paint.Style.STROKE); 
     drawingPaint.setAntiAlias(true); 

     touchPoints = new ArrayList<PointF>(); 
    } 

    public void setPoints(MotionEvent event){ 
     touchPoints.clear(); 

     int pointerIndex = 0; 

     for(int index=0; index<event.getPointerCount(); ++index) 
     { 
      pointerIndex = event.getPointerId(index); 

      touchPoints.add(new PointF(event.getX(pointerIndex),event.getY(pointerIndex))); 
     } 
    } 

    private PointF getMidPoint(float x1,float y1, float x2, float y2) { 
     PointF point = new PointF(); 

     float x = x1 + x2; 
     float y = y1 + y2; 

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

     return point; 
    } 
} 
+0

請你簡單介紹一下上面的代碼如何幫助我實現我的場景,即用戶在屏幕上執行觸摸操作時必須調整整個屏幕的大小。如果上述代碼適用於我的場景,那麼如何啓用它對於我的活動的父級線性佈局,所以多點觸控將適用於佈局中的所有組件,如果我想在我的活動中使用此類來支持多點觸控,那麼如何在我的活動中使用它? – piks

+0

您正在使用哪個API級別? – reuf

+0

看看這個博客文章和下面的評論:http://myandroidnote.blogspot.com/2011/03/zooming-view-completely.html – reuf