zoukankan      html  css  js  c++  java
  • Android控件之ZoomButton

    Android控件之ZoomButton

    1 ZoomButton简介

    ZoomButton,称为放大按钮。实际上它继承于ImageButton,并在ImageButton基础上增加了“按下ZoomButton时,会不断上报点击事件”。至于上报的时间间隔,可以通过setZoomSpeed()去设置。

    2 ZoomButton示例

    对比ZoomButton和ImageButton。写一个activity,包含一个ZoomButton和一个ImageButton。
    点击ZoomButton和ImageButton时,分别会放大不同的文本。测试时,请分别按住它们不放,查看效果。

    应用层代码

    package com.skywang.control;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ImageButton;
    import android.widget.ZoomButton;
    import android.widget.TextView;
    
    /*
     * @author: skywang
     * @description:对比ZoomButton和ImageButton的差异。 
     *   主要包含1个ZoomButton和ImageButton。
     *   分别按住“ZoomButton”和“ImageButton”不放,可以发现:
     *   ZoomButton会不断上拨onClick事件;而ImageButton只会上报一次。
     */
    public class ZoomTest extends Activity implements View.OnClickListener {
    
        // ZoomButton
        private static int pauseSize= 12;
        private TextView mTvShow;     
        private ZoomButton mZoomPause;
    
        // ImageButton    
        private static int pauseCom= 12;
        private TextView mTvCom;
        private ImageButton mBtnCom;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.zoom_test);
            
            mTvShow = (TextView)findViewById(R.id.tv_show);        
            mZoomPause = (ZoomButton)findViewById(R.id.zoom_pause);
            mZoomPause.setOnClickListener(this);
        
            mTvCom = (TextView)findViewById(R.id.tv_com);
            mBtnCom = (ImageButton)findViewById(R.id.btn_com);
            mBtnCom.setOnClickListener(this);
        }
    
        public void onClick(View v) {
            switch(v.getId()) {
                case R.id.zoom_pause:{
                    // 按住ZoomButton不方,会不断的执行下面的操作:将文本放大
                    pauseSize += 2;
                    mTvShow.setTextSize(pauseSize);
                    break;
                }
                case R.id.btn_com:{
                    // 按住ImageButton,只有松开时才会执行下面操作:将文本放大
                    pauseCom += 2;
                    mTvCom.setTextSize(pauseCom);
                    break;
                }
                default:
                    break;
            }
        }
        
    }

    layout文件

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/tv_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:textColor="#FF00AA00"
            android:text="@string/text_zoom" />
    
        <ZoomButton
            android:id="@+id/zoom_pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:background="@android:color/transparent"
            android:src="@drawable/btn_pause"/>
        
        
        <TextView
            android:id="@+id/tv_com"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:textColor="#FF0000AA"
            android:text="@string/text_com" />
    
        <ImageButton
            android:id="@+id/btn_com"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:background="@android:color/transparent"
            android:src="@drawable/btn_pause"/>
        
    </LinearLayout>

    其中,btn_pause.xml文件内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <!-- 按下状态 -->
        <item android:state_pressed="true" android:drawable="@drawable/pause_on" />
        <!-- 未按下状态 -->
        <item android:state_focused="true" android:drawable="@drawable/pause_off" />
        <!-- 初始化状态 -->
        <item android:drawable="@drawable/pause_off" />    
    
    </selector>

    pause_on.png如下图

    pasue_off如下图

    manifest文件

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.skywang.control"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.skywang.control.ZoomTest"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>

    点击下载:源代码

    运行效果:如图

  • 相关阅读:
    全屏后无法检查键值
    根据秒转化为周天时秒分,不足补0
    日期时间格式化(Date对象)
    AS3 RGB颜色
    Egret eui中eui.ItemRenderer类型的组件在渲染界面时,dataChanged()重复调用
    egret 画圆 画圆角矩形 画矩形
    egert 缓动
    Flashdeveloper开多个实例进程解决方案
    flash 发布exe文件
    sublime Text3 如何自动排版代码
  • 原文地址:https://www.cnblogs.com/skywang12345/p/3123319.html
Copyright © 2011-2022 走看看