码迷,mamicode.com
首页 > 移动开发 > 详细

Android之ImageSwitcher

时间:2015-07-17 00:19:18      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

  要点:

    (查看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>

 

Android之ImageSwitcher

标签:

原文地址:http://www.cnblogs.com/Free-Wind/p/4652706.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!