• Android之ImageSwitcher


      要点:

        (查看Api总结)

        1:ImageSwitcher 继承 ViewSwitcher, (ViewSwitcher 有继承FrameLayout )

        2: 要实现切图必须实现 ViewSwitcher.ViewFactory接口中的 makeView() 

      易错点:

        1: 

          mImageSwitcher.setFactory(this);
          mImageSwitcher.setImageResource(mImages[0]);

          setImageResource必须调用在setFactory之后。  原因:根据源码分析调用setFactory获得接口,才能

        获得makeView()返回的view 否则抛空指针异常

        2: 在makeView中设置布局参数 要用FrameLayout布局参数

    package com.imageswitcher;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.animation.AnimationUtils;
    import android.widget.FrameLayout;
    import android.widget.ImageSwitcher;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.Toast;
    import android.widget.ViewSwitcher;
    
    
    public class MainActivity extends Activity implements ViewSwitcher.ViewFactory {
    
        private LinearLayout mLinearLayout;
        private ImageSwitcher mImageSwitcher;
    
        private Integer[] mImages = {R.drawable.android01, R.drawable.android02, R.drawable.android03, R.drawable.android04,
                R.drawable.android05, R.drawable.android06, R.drawable.android07, R.drawable.android08, R.drawable.android09};
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            initView();
            mImageSwitcher.setFactory(this);
            mImageSwitcher.setImageResource(mImages[0]);
            mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
            mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
    
            for (int i = 0; i < mImages.length; i++) {
                mLinearLayout.addView(getImageView(i));
            }
        }
    
        private ImageView getImageView(final int i) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(mImages[i]);
            imageView.setId(i);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mImageSwitcher.setImageResource(mImages[i]);
                    Toast.makeText(v.getContext(), "选择了" + (v.getId() + 1) + "张图", Toast.LENGTH_SHORT).show();
                }
            });
    
            return imageView;
        }
    
        private void initView() {
            mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout);
            mImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
        }
    
    
        @Override
        public View makeView() {
            ImageView imageView = new ImageView(this);
            Log.i(MainActivity.class.getSimpleName(), "第几次调用");
            imageView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            return imageView;
        }
    }
    

      

    xml

    <RelativeLayout 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"
        tools:context=".MainActivity">
    
        <ImageSwitcher
            android:id="@+id/imageSwitcher"
            android:layout_width="match_parent"
            android:layout_height="200dp">
            
        </ImageSwitcher>
    
        <HorizontalScrollView
            android:layout_below="@id/imageSwitcher"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            >
            
            <LinearLayout
                android:id="@+id/linearLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
            </LinearLayout>
            
        </HorizontalScrollView>
    </RelativeLayout>
  • 相关阅读:
    数据库学习笔记3--基本的SQL语句
    数据库学习笔记2--MySQL数据类型
    数据库学习笔记1----MySQL 5.6.21的安装和配置(setup版)
    JavaWeb学习笔记1---http协议
    Spring学习笔记18--通过工厂方法配置Bean
    Spring学习笔记17--在XML中使用SPEL
    Spring 学习笔记16---properties文件的两种方式
    Spring学习笔记15--注解Bean
    Spring4.0学习笔记1---开发环境搭建
    Installed JREs时 Standard 1.1.x VM与Standard VM的区别
  • 原文地址:https://www.cnblogs.com/Free-Wind/p/4652706.html
Copyright © 2020-2023  润新知