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


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

    可以实现以下需求:

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

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

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

     运行效果图:

    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_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>

      

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

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

  • 相关阅读:
    GhostBSD 3.0RC3,基于GNOME的FreeBSD
    Nagios 3.4.3 发布,企业级监控系统
    Jolokia 1.0.6 发布, JMX远程访问方法
    微软希望开发人员不要使 WebKit 成为新版 IE6
    Kwort Linux 3.5 正式版发布
    EJDB 1.0.24 发布,嵌入式 JSON 数据库引擎
    Pale Moon 15.3 Firefox“苍月”优化版发布
    Galera Load Balancer 0.8.1 发布
    SmartSVN V7.5 正式发布
    PostgresQL建立索引如何避免写数据锁定
  • 原文地址:https://www.cnblogs.com/johnsonwei/p/5830277.html
Copyright © 2020-2023  润新知