<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Button
android:layout_width="20dp"
android:layout_height="match_parent"
android:id="@+id/b1"
android:text="L"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/b1"
android:layout_toLeftOf="@id/b2">
<ImageSwitcher
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageSwitcher"
/>
</FrameLayout>
<Button
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:id="@+id/b2"
android:text="R"/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
XML代码
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
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.ViewSwitcher;
public class MainActivity extends AppCompatActivity {
private int index=0;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int imgId[]={R.drawable.i1,R.drawable.i2,R.drawable.i3,R.drawable.i5};
@SuppressLint({"MissingInflatedId", "LocalSuppress"})
ImageSwitcher imageSwitcher=findViewById(R.id.imageSwitcher);
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_out));
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
imageView=new ImageView(MainActivity.this);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT));
return imageView;
}
});
imageSwitcher.setImageResource(R.drawable.i1);
Button b1=findViewById(R.id.b1);
Button b2=findViewById(R.id.b2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(index==0){
index=3;
}else {
index--;
}
imageSwitcher.setImageResource(imgId[index]);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(index==3){
index=0;
}else {
index++;
}
imageSwitcher.setImageResource(imgId[index]);
}
});
}
}
JAVA代码
不要忘记setFactory()
其中请注意:
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() { public View makeView() { imageView=new ImageView(MainActivity.this); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); imageView.setLayoutParams(new FrameLayout.LayoutParams( FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT)); return imageView; } });