zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    基于VIP的keepalived高可用架构
    高性能Nginx最佳实践
    Ubuntu安装Nginx
    Synchronized总结及底层原理分析#网易微专业# #Java#
    springboot Restful开发
    Volatile详解
    IOC知识点详细汇总
    python 一个函数让你决定你的二维码
    用python批量生成简单的xml文档
    基于tensorflow搭建一个神经网络
  • 原文地址:https://www.cnblogs.com/Free-Wind/p/4652706.html
Copyright © 2011-2022 走看看