public class CabinetActivity extends Activity {
protected static final String TAG = "43";
LinearLayout[] layoutliner;
private float currentDistance;
private float lastDistance;
private LinearLayout lout;
List<LinearLayout> lout1 = null;
TextView[][] tViews ;
Context context;
int ss,yy;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.cabinet);
context = this;
lout= (LinearLayout)findViewById(R.id.linearlayout);
lout1 = new ArrayList <LinearLayout>();
int x= 20,y=20;
tViews = new TextView[x][y];
final int length = 60;
final int width = 60;
ss = length;
yy = width;
// LinearLayout.LayoutParams textviewParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
// ViewGroup.LayoutParams.WRAP_CONTENT);
//LinearLayout lout = new LinearLayout(this);
// layout.addView(textView,textviewParams);//
// Button btnButton = new Button(this);
// btnButton.setText("1");
/* for(int i = 0 ;i<20;i++)
{
TextView btnButton = new TextView(this);
String str = null;
str = Integer.toString(i);
btnButton.setText(str);
//btnButton.setBackgroundColor(0xFFFF6FFF);
btnButton.setWidth(50);
btnButton.setHeight(50);
ImageView img = new ImageView(this);
// img.setBackgroundResource(resid)
btnButton.setBackgroundResource(R.anim.shape);
lout.addView(btnButton);
} */
///
// layoutliner = new LinearLayout[20];
//setContentView(R.layout.cabinet);
for(int i = 0 ;i<20;i++)
{
LinearLayout laytemp;
laytemp = new LinearLayout(this);
lout1.add(laytemp);
LinearLayout.LayoutParams pr = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// lout1.add(new LinearLayout(this));
lout.addView(laytemp, pr);
for(int j=0; j<20; j++)
{
TextView btnButton = new TextView(this);
String str = null;
str = Integer.toString(i*20 +j);
btnButton.setText(str);
//btnButton.setBackgroundColor(0xFFFF6FFF);
btnButton.setWidth(length);
btnButton.setHeight(width);
ImageView img = new ImageView(this);
// img.setBackgroundResource(resid)
btnButton.setBackgroundResource(R.anim.shape);
laytemp.addView(btnButton);
// lout.addView(btnButton);
}
}
// setContentView(R.layout.cabinet);
RelativeLayout lyout = (RelativeLayout)findViewById(R.id.linearllll);
lout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
/**
* 手指按下
*/
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "down!!!");
if (event.getPointerCount() < 2)
{
}
break;
/**
* 手指移动
*/
case MotionEvent.ACTION_MOVE:
Log.d(TAG, "move!!!");
/**
* 首先判断按下手指的个数是不是大于两个。
* 如果大于两个则执行以下操作(即图片的缩放操作)。
*/
if (event.getPointerCount() >= 2) {
float offsetX = event.getX(0) - event.getX(1);
float offsetY = event.getY(0) - event.getY(1);
/**
* 原点和滑动后点的距离差
*/
currentDistance = (float) Math.sqrt(offsetX * offsetX
+ offsetY * offsetY);
if (lastDistance < 0) {
lastDistance = currentDistance;
} else {
/**
* 如果当前滑动的距离(currentDistance)比最后一次记录的距离(lastDistance)相比大于5英寸(也可以为其他尺寸),
* 那么现实图片放大
*/
if (currentDistance - lastDistance > 5) {
Log.d(TAG, "放大!!!");
/**
* 图片宽高一次放大为原来图片的1.1倍(当然,也可以为其他数值)。
*/
if(lout1 != null)
{
lout.removeAllViews();
for(int i = 0 ;i<20;i++)
{
LinearLayout laytemp;
laytemp = new LinearLayout(context);
lout1.add(laytemp);
LinearLayout.LayoutParams pr = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// lout1.add(new LinearLayout(this));
lout.addView(laytemp, pr);
for(int j=0; j<20; j++)
{
TextView btnButton = new TextView(context);
String str = null;
str = Integer.toString(i*20 +j);
btnButton.setText(str);
//btnButton.setBackgroundColor(0xFFFF6FFF);
btnButton.setWidth((int) (ss+1));
btnButton.setHeight((int) (yy+1));
// img.setBackgroundResource(resid)
btnButton.setBackgroundResource(R.anim.shape);
laytemp.addView(btnButton);
// lout.addView(btnButton);
}
}
ss = (int) (ss*1.1);
yy = (int) (yy*1.1);
lout1.clear();
}
lastDistance = currentDistance;
/**
* 如果最后的一次记录的距离(lastDistance)与当前的滑动距离(currentDistance)相比小于5英寸,
* 那么图片缩小。
*/
} else if (lastDistance - currentDistance > 5) {
Log.d(TAG, "缩小!!!");
/**
* 图片宽高一次缩小为原来图片的0.9倍。
*/
lastDistance = currentDistance;
}
}
}
break;
/**
* 手指抬起
*/
case MotionEvent.ACTION_UP:
Log.d(TAG, "up!!!");
break;
}
return true;
}
});
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/linearllll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
<ZoomControls
android:id="@+id/zoomControls1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
package com.example.zhangjjtest;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.view.View.OnTouchListener;
public class MainActivity extends Activity {
Button btnButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout ly = (RelativeLayout)findViewById(R.id.ert123);
ly.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
/**
* 手指按下
*/
case MotionEvent.ACTION_DOWN:
Log.d("21", "down!!!");
break;
/**
* 手指移动
*/
case MotionEvent.ACTION_MOVE:
Log.d("21", "move!!!");
break;
}
return false;
}
});
btnButton = (Button)findViewById(R.id.tton1123);
btnButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
CabinetActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}