zoukankan      html  css  js  c++  java
  • android图片切换ImageSwichter的动画切换效果

    activity_main.xml

    控件的线性布局

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageSwitcher
    android:id="@+id/imageSwitcher"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <Button
    android:id="@+id/butPrevious"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:enabled="false"
    android:text="上一张图片"/>
    <Button
    android:id="@+id/butNext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="下一张图片"/>
    </LinearLayout>
    </LinearLayout>

    MainActivity.java

    package com.example.imageswitcherproject;

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    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 Button butPrevious=null;//上一张图片按钮
    private Button butNext=null;//下一张图片按钮
    private ImageSwitcher imageSwitcher=null;
    private int[] imgRes={R.drawable.ispic_a,R.drawable.ispic_b,
    R.drawable.ispic_c,R.drawable.ispic_d,
    R.drawable.ispic_e};//图片控件的ID
    private int foot=0;//记录图片的下标
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.butPrevious=(Button)super.findViewById(R.id.butPrevious);
    this.butNext=(Button)super.findViewById(R.id.butNext);
    this.imageSwitcher=(ImageSwitcher)super.findViewById(R.id.imageSwitcher);
    this.imageSwitcher.setFactory(new OnFactorylmpl());//设置转换工厂
    this.imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));//设置动画
    this.imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));//设置动画
    this.imageSwitcher.setImageResource(this.imgRes[foot++]);//设置初始图片
    this.butPrevious.setOnClickListener(new OnClickListenerPrevious());
    this.butNext.setOnClickListener(new OnClickListenerNext());
    }

    private void checkButEnable(){//设置按钮状态
    if(this.foot<this.imgRes.length-1){
    this.butNext.setEnabled(true);
    }else{
    this.butNext.setEnabled(false);
    }
    if(this.foot==0){
    this.butPrevious.setEnabled(false);
    }else{
    this.butPrevious.setEnabled(true);
    }
    }

    private class OnClickListenerNext implements OnClickListener{//监听按钮

    @Override
    public void onClick(View v) {
    MainActivity.this.imageSwitcher.setImageResource(MainActivity.this.imgRes[MainActivity.this.foot++]);
    MainActivity.this.checkButEnable();
    }
    }

    private class OnClickListenerPrevious implements OnClickListener{//监听按钮

    @Override
    public void onClick(View v) {
    MainActivity.this.imageSwitcher.setImageResource(MainActivity.this.imgRes[MainActivity.this.foot--]);
    MainActivity.this.checkButEnable();
    }

    }

    private class OnFactorylmpl implements ViewFactory{

    @Override
    public View makeView() {
    ImageView image=new ImageView(MainActivity.this);//创建图片控件
    image.setBackgroundColor(0xFFFFFFFF);//设置背景颜色
    image.setScaleType(ImageView.ScaleType.CENTER); //设置显示方式
    image.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,
    LayoutParams.FILL_PARENT));//定义组建
    return image;
    }

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    margin问题
    IE6里面子集尺寸大的会把父亲撑大
    第一个元素<flout>写了,想在他的旁边加一个元素.IE6会出现缝隙. 不要用margin撑开,要用flout
    兼容性,float
    HTML5的兼容问题以及调用js文件的方法
    表单
    表格的编写,课程表
    SmartThreadPool
    C# 多线程的等待所有线程结束的一个问题
    DataTable保存与读取 stream
  • 原文地址:https://www.cnblogs.com/liyuanjinglyj/p/4656613.html
Copyright © 2011-2022 走看看