放大Tablelayout

时间:2011-09-15 12:09:34

标签: android android-tablelayout

我创建了TableLayout。在TableLayout我创建动态行并在表格行上添加按钮数。 OnTouchListener适用于按钮以放大和缩小。     当我移动我的手指慢慢放大并缩小工作正常但当我的手指移动快速缩放功能不能正常工作。

我的源代码是。
    请建议我如何以正常的方式改进我的代码

package com.exa;


import android.app.Activity;
import android.content.res.Resources;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class DynamictablerowActivity extends Activity implements OnTouchListener,OnClickListener{
    /** Called when the activity is first created. */
     Button textView[][]= new Button[39][40];
     TableRow tableRow;
     TableLayout tableLayout;
     ScrollView sc;
     float c=10;
     RelativeLayout re;
     float olddistanc;
     static final int NONE = 0;
     static final int DRAG = 1;
     static final int ZOOM = 2;
     int mode = NONE;
     PointF oldDistance= new PointF();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       sc= (ScrollView) findViewById(R.id.scroll_view);
        tableLayout=(TableLayout) findViewById(R.id.table_layout);
        for (Integer i = 0; i < 20; i++) {
            tableRow = new TableRow(getApplicationContext());
            tableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            for (Integer j = 0; j < 24; j++) {
                textView[i][j] = new Button(getApplicationContext());
                textView[i][j].setOnTouchListener(this);
                textView[i][j].setOnClickListener(this);
                Resources res = getResources();
                Drawable drawable = res.getDrawable(R.drawable.curent);
                textView[i][j].setBackgroundDrawable(drawable);
                textView[i][j].setLayoutParams(new TableRow.LayoutParams(30, 30));
                textView[i][j].setPadding(20, 20, 20, 20);
                tableRow.addView(textView[i][j]);
                int w = textView[i][j].getWidth();
                int h = textView[i][j].getHeight();
            }
            tableLayout.addView(tableRow);
        }
    }
static int a=0;
int b=10;

    @Override
    public boolean onTouch(View v, MotionEvent event) {


        switch(event.getAction()&MotionEvent.ACTION_MASK)
        {
    case MotionEvent.ACTION_DOWN:
            Log.d("ok", "i am motion down event");
            Log.d("ok", "i a==ACTION_DOWN=="+a);
            mode= DRAG;
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            Log.d("ok", "i am in pointer down listener");
            a++;
            Log.d("ok", "i a=ACTION_POINTER_DOWN==="+a);
            olddistanc= spacing(event);
            //oldDistance.set(olddistanc);
            Log.d("dis", "i get the old Distance"+olddistanc);
            if(olddistanc>10f)
            {
                mode=ZOOM;
            }
            break;
        case MotionEvent.ACTION_UP:
            Log.d("ok", "i am in the action up listener");
            a++;
            Log.d("ok", "i a=ACTION_UP==="+a);
            break;
        case MotionEvent.ACTION_POINTER_UP:
            a++;
            Log.d("ok", "i a==ACTION_POINTER_UP=="+a);
            Log.d("ok", "i am in the  actioon pointer up listener");
            mode=NONE;

            break;
        case MotionEvent.ACTION_MOVE:
            if(mode==DRAG)
            {
            Log.d("ok", "the value of the mode is ="+mode); 
            }
            else if(mode==ZOOM)
            {
                Log.d("ok", "the value of the mode is ="+mode);
            Log.d("ok", "i am move gesture in the touch event");
            a++;
            Log.d("ok", "i a==ACTION_MOVE=="+a);
            float ndistance= spacing(event);
            Log.d("dis", "i get the new Distance"+ndistance);
            Log.d("dis", "i get the old Distance"+olddistanc);
            c = ndistance - olddistanc;
            Log.d("dis", "number of pointers = "+event.getPointerCount());
            if(event.getPointerCount() < 2)
            break;
                if((c < 0 && textView[0][0].getWidth()+ c <= 30) || (c >= 0 && textView[0][0].getWidth() + c >= 100)){
                }
            else
            {
                int incordec = 10;
                if(c < 0)
                    incordec = -10;
                if(incordec==10)
                    Log.d("dis", "zoooooom ++++");
                else
                    Log.d("dis", "zoom outtttttt");
                for(int i=0;i<20;i++)
                {
                    for(Integer j = 0; j < 24; j++)
                    {
                        textView[i][j].setLayoutParams(new TableRow.LayoutParams(textView[i][j].getWidth()+incordec, textView[i][j].getHeight()+incordec));
                    }
                }
            }
            olddistanc= ndistance;
            break;
        }
        }
        return false;
}
    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);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.d("ok", "i am in click listener");

    }

    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);
        }
}

0 个答案:

没有答案