zoukankan      html  css  js  c++  java
  • ImageSwitcher的简单使用

    测试代码:

    activity_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/MyLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <ImageSwitcher
            android:id="@+id/imageSwitcher"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/btnPrevious"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:enabled="false"
                android:text="上一张" />
    
            <Button
                android:id="@+id/btnNext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:enabled="true"
                android:text="下一张" />
        </LinearLayout>
    
    </LinearLayout>

    MainActivity.java:

    package com.example.zz;
    
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup.LayoutParams;
    import android.view.animation.AnimationUtils;
    import android.widget.Button;
    import android.widget.ImageSwitcher;
    import android.widget.ImageView;
    import android.widget.ViewSwitcher.ViewFactory;
    
    public class MainActivity extends Activity {
        private ImageSwitcher imageSwitcher;
        private Button btnPrevious;
        private Button btnNext;
        private int foot=0;
        private int[] imgRes=new int[]{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,};
        
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState); // 生命周期方法
            super.setContentView(R.layout.activity_main); // 设置要使用的布局管理器
        imageSwitcher=(ImageSwitcher)findViewById(R.id.imageSwitcher);
        btnPrevious=(Button)findViewById(R.id.btnPrevious);
        btnNext=(Button)findViewById(R.id.btnNext);
        
        imageSwitcher.setFactory(new ViewFactory() {//设置转化工厂
            
            @Override
            public View makeView() {
                ImageView imageView=new ImageView(MainActivity.this);
                imageView.setBackgroundColor(0xFFFFFFFF);
                imageView.setScaleType(ImageView.ScaleType.CENTER);//居中显示
                imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));//定义组件
                return imageView;
            }
        });
        imageSwitcher.setImageResource(imgRes[foot++]);//初始化时显示,必须放在工厂后面,否则会报NullPointerException
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));//设置动画
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));//设置动画
        btnPrevious.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
                MainActivity.this.imageSwitcher.setImageResource(imgRes[foot--]);
                MainActivity.this.checkBtnEnable();
                
            }
        });
        btnNext.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
                MainActivity.this.imageSwitcher.setImageResource(imgRes[foot++]);
                MainActivity.this.checkBtnEnable();
                
            }
        });
    
        }
    
        protected void checkBtnEnable() {//判断按钮可用状态
            if(this.foot<this.imgRes.length-1){
                this.btnNext.setEnabled(true);
            }else{
                this.btnNext.setEnabled(false);
            }
            if(this.foot==0){
                this.btnPrevious.setEnabled(false);
            }else {
                this.btnPrevious.setEnabled(true);
            }
            
        }
    }
  • 相关阅读:
    (原创)批处理中变量的用法
    (收藏)Android 的各种listener and states event
    (转)Android 、BlackBerry 文本对齐方式对比
    (转)Android中尺寸单位杂谈
    批处理文章集锦
    Launch custom android application from android browser
    【原创】我的批处理命令例子
    Android文字居中
    (转)androd之绘制文本(FontMetrics)
    (批处理之二):setlocal enabledelayedexpansion (详解)
  • 原文地址:https://www.cnblogs.com/zzw1994/p/5082870.html
Copyright © 2011-2022 走看看