[Android] 图片切换组件ImageSwitcher

[Android] 图片切换组件ImageSwitcher,第1张

ImageSwitcher组件的主要功能是完成图片的切换显示。
ImageSwitche是View的子类,所以View类的属性和方法ImageSwitcher都具备。

1.ImageSwitcher常用方法
  • public ImageSwitcher(Context context): 创建ImageSwitcher对象
  • public void setFactory(ViewSwitcher.ViewFactory factory):设置ViewFactory对象,用于完成两个图片切换时ViewSwitcher的转换 *** 作
  • public void setImageResource(int resid): 设置显示的图片ID
  • public void setInAnimation (Animation inAnimation): 图片读进ImageSwitcher时的动画效果
  • public void setOutAnimation(Animation outAnimation): 图片从ImageSwitcher消失时的动画效果
  • public void setImageDrawable (Drawable drawable):绘制图片
  • public void setImageURI (Uri uri):设置图片地址,可读取网络图片
2.运行图



3.代码 (1)activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
   <ImageSwitcher
       android:layout_margin="20dp"
       android:layout_gravity="center_horizontal"
       android:layout_width="match_parent"
       android:layout_height="500dp"
       android:id="@+id/imageSwitcher"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/pre"
            android:layout_margin="20dp"
            android:text="上一张"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/next"
            android:layout_margin="20dp"
            android:text="下一张"/>

    LinearLayout>

LinearLayout>
(2)MainActivity.java
package com.example.progressdialog;

import androidx.appcompat.app.AppCompatActivity;


import android.os.Bundle;
import android.view.View;

import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher;


public class MainActivity extends AppCompatActivity {
    private ImageSwitcher imageSwitcher;
    private Button pre,next;
    private int []images={R.drawable.p1,R.drawable.p2,R.drawable.p3
            ,R.drawable.p4,R.drawable.p5,R.drawable.p6};
    private int count=0;//用于计数的变量

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageSwitcher=findViewById(R.id.imageSwitcher);
        pre=findViewById(R.id.pre);
        next=findViewById(R.id.next);  //获取组件id

        pre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(MainActivity.this.count==0)
                    Toast.makeText(MainActivity.this,
                            "已是第一张,不能往前了!",Toast.LENGTH_SHORT).show();
                else //获取指定图片,并将图片计数减1
                    MainActivity.this.imageSwitcher.setImageResource
                            (MainActivity.this.images[--MainActivity.this.count]);

            }
        });
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(MainActivity.this.count==images.length-1)
                    Toast.makeText(MainActivity.this,
                            "已是最后一张,不能往后了!",Toast.LENGTH_SHORT).show();
                else  //获取指定图片,并将图片计数加1
                    MainActivity.this.imageSwitcher.setImageResource
                            (MainActivity.this.images[++MainActivity.this.count]);

            }
        });
        //设置转换工厂
        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView imageView=new ImageView(MainActivity.this); //定义imageView组件
                imageView.setBackgroundColor(0xFFFFFF); //设置背景
                //按图片居中显示,当图片长/宽超过view的长/宽时,截取图片居中部分进行显示
                imageView.setScaleType(ImageView.ScaleType.CENTER);
                imageView.setLayoutParams(new FrameLayout.LayoutParams
                        (FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
                return imageView ;
            }
        });
        imageSwitcher.setImageResource(images[count]);
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));//设置进入时的动画效果
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));//设置离开时的动画效果




    }
}

图片资源下载后,复制到 res目录下drawable文件夹下。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/726101.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存