缩放系列(二):所有子控件也随着缩放、手势缩放、多点触控layout

下面是一个功能强大的改造的例子:

可以实现以下需求:

1.两个手指进行缩放布局

2.所有子控件也随着缩放,

3.子控件该有的功能不能丢失(像button有可被点击的功能,缩放后不能丢失该功能)

运行效果图:

缩放系列(二):所有子控件也随着缩放、手势缩放、多点触控layout

java代码如下

MainActivity.java:

public class MainActivity extends ActionBarActivity {
private ScaleGestureDetector mScaleGestureDetector = null;
private View view; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.test);
view = View.inflate(this, R.layout.activity_main, null);
setContentView(view); mScaleGestureDetector = new ScaleGestureDetector(this,
new ScaleGestureListener()); } @Override
public boolean onTouchEvent(MotionEvent event) {
// 返回给ScaleGestureDetector来处理
return mScaleGestureDetector.onTouchEvent(event);
} public class ScaleGestureListener implements
ScaleGestureDetector.OnScaleGestureListener { private float scale;
private float preScale = 1;// 默认前一次缩放比例为1 @Override
public boolean onScale(ScaleGestureDetector detector) { float previousSpan = detector.getPreviousSpan();
float currentSpan = detector.getCurrentSpan();
if (currentSpan < previousSpan) {
// 缩小
// scale = preScale-detector.getScaleFactor()/3;
scale = preScale - (previousSpan - currentSpan) / 1000;
} else {
// 放大
// scale = preScale+detector.getScaleFactor()/3;
scale = preScale + (currentSpan - previousSpan) / 1000;
} // 缩放view
ViewHelper.setScaleX(view, scale );// x方向上缩小
ViewHelper.setScaleY(view, scale );// y方向上缩小 return false;
} @Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
// 一定要返回true才会进入onScale()这个函数
return true;
} @Override
public void onScaleEnd(ScaleGestureDetector detector) {
preScale = scale;//记录本次缩放比例
}
}
}

布局文件(activity_main.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/home_tools"
> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试" /> <ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/abc_ic_voice_search_api_holo_light" /> <ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/selector_button2" /> <ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/selector_button1"
android:layout_gravity="center"
/> </LinearLayout>

selector_button1.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/home_tools" android:state_pressed="true"></item>
<item android:drawable="@drawable/home_*"></item>
</selector>

selector_button2.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/home_sysoptimize" android:state_pressed="true"></item>
<item android:drawable="@drawable/home_taskmanager"></item>
</selector>

如果想要整个布局能够移动,可以看下面的帖子:

http://www.cnblogs.com/johnsonwei/p/5831925.html

上一篇:CocoaPods安装记录


下一篇:三菱plc编程电缆通讯端口设置方法(转载)