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;
    }

    }

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

  • 相关阅读:
    @FeignClient常用属性
    前端调用接口成功但后端没收到请求
    @EnableDiscoveryClient与Nacos的服务注册与拉取
    解决WebStorm开发vue提示Module is not installed、Unresolved variable or type
    Docker内使用Nignx
    Docker内运行的nginx除了80端口其他端口都无法访问
    在Win11的WSL中体验IDEA等GUI程序
    python小工具:编码转换
    php nginx 504 Gateway Timeout 网关超时错误
    Centos下安装php mysql pdo以及gd扩展
  • 原文地址:https://www.cnblogs.com/liyuanjinglyj/p/4656613.html
Copyright © 2011-2022 走看看