放大表格布局
我创建了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);
}
}
I create TableLayout
. In the TableLayout
I create dynamic row and add Number of Button on table row. OnTouchListener
apply on the Button for zoom in and zoom out.
When I move my finger slowly zoom in and zoom out work fine but when my finger move fast zoom function not work in decent manner.
My source code is .
Please Suggest me how to improve my code when it work decent manner
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论