所有子控件也随着缩放手势缩放layout多点触控layout
Posted 达哥的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了所有子控件也随着缩放手势缩放layout多点触控layout相关的知识,希望对你有一定的参考价值。
下面是一个功能强大的改造的例子:
可以实现以下需求:
1.两个手指进行缩放布局
2.所有子控件也随着缩放,
3.子控件该有的功能不能丢失(像button有可被点击的功能,缩放后不能丢失该功能)
运行效果图:http://pan.baidu.com/s/1o8GU302
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.test, 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;//记录本次缩放比例 } } }
布局文件:
<?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_trojan"></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>
如果想要整个布局能够移动,可以看下面的帖子:
以上是关于所有子控件也随着缩放手势缩放layout多点触控layout的主要内容,如果未能解决你的问题,请参考以下文章
Android自定义ImageView实现图片缩放滑动,双击放大缩小,多点触控缩放
Android自定义ImageView实现图片缩放滑动,双击放大缩小,多点触控缩放