layout codes
<?xml version="1.0" encoding="utf-8"?> <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"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/a" /> <!--限制长宽--> <ImageView android:layout_width="70dp" android:layout_height="100dp" android:background="#FF0000" android:src="@drawable/a" /> <!--适应长宽,拉伸--> <ImageView android:layout_width="70dp" android:layout_height="100dp" android:scaleType="fitXY" android:src="@drawable/a" android:tint="#66FF0000" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxHeight="90dp" android:minWidth="200dp" android:src="@drawable/a" android:adjustViewBounds="true" /> <ImageSwitcher android:id="@+id/imageswitcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
java calling code
package com.example.myhighuii; import androidx.appcompat.app.AppCompatActivity; import android.media.Image; import android.os.Bundle; import android.view.View; import android.view.animation.AnimationUtils; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher; public class MainActivity extends AppCompatActivity { ImageSwitcher imageSwitcher = null; @Override protected voidthe onCreate (the Bundle savedInstanceState) { Super .onCreate (savedInstanceState); the setContentView (R.layout.activity_main); imageSwitcher = (ImageSwitcher) the findViewById (R.id.imageswitcher); // fade effect imageSwitcher.setOutAnimation (AnimationUtils.loadAnimation ( the MainActivity. the this , android.R.anim.fade_out )); // fade effect imageSwitcher.setInAnimation (AnimationUtils.loadAnimation ( the MainActivity. the this , android.R.anim.fade_in )); // develop a plant output imageSwitcher.setFactory (new new ViewSwitcher.ViewFactory () { @Override public View makeView () { // designated image is an image switch the ImageView the imageView = new new the ImageView (the MainActivity. the this ); // assign a default load picture imageView.setImageResource (R. drawable.a); return the imageView; } }); // set the listener to switch the image imageSwitcher.setOnClickListener ( new new View.OnClickListener () { @Override public void the onClick (View V) { //Parameter v is imageSwitcher ((ImageSwitcher) v) .setImageResource (R.drawable.b); } }); } }
Rendering
Multi-picture cycle switch to achieve
<?xml version="1.0" encoding="utf-8"?> <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" tools:context=".MainActivity" android:orientation="vertical"> <ImageSwitcher android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageSwitcher"> </ImageSwitcher> </LinearLayout>
java call
package com.example.myhighuiii; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.animation.AnimationUtils; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher; public class MainActivity extends AppCompatActivity { private int[] arrayPicture = new int[]{ R.mipmap.a, R.mipmap.b, R.mipmap.c }; Private ImageSwitcher imageSwitcher = null ; Private int index = 0 ; Private a float touchDownX; // finger pressing the X coordinate Private a float touchUpX; // finger lift X coordinate @Override protected void the onCreate (the Bundle savedInstanceState) { Super .onCreate ( savedInstanceState); the setContentView (R.layout.activity_main); imageSwitcher = (ImageSwitcher) the findViewById (R.id.imageSwitcher); imageSwitcher.setFactory ( new new ViewSwitcher.ViewFactory () { @Override public MotionEvent.ACTION_DOWN) {MakeView View () { the ImageView the imageView = new new the ImageView (the MainActivity. The this ); imageView.setImageResource (arrayPicture [index]); // picture displayed currently to return the imageView; } }); // set event listener imageSwitcher.setOnTouchListener ( new new View.OnTouchListener () { @Override public boolean OnTouch (View v, MotionEvent event) { // If the event is triggered by pressing IF (event.getAction () == touchDownX = event.getX (); return to true ; } the else IF (event.getAction () == MotionEvent.ACTION_UP) { // triggered up event touchUpX = event.getX (); IF (touchUpX-touchDownX> 100) { // considered to be sliding from left to right, step forward index = index == 0 arrayPicture.length-1: index-1? ; // set fade imageSwitcher.setInAnimation (AnimationUtils.loadAnimation ( MainActivity. the this , Android .R.anim.fade_in )); ImageSwitcher.setOutAnimation (AnimationUtils.loadAnimation ( the MainActivity. The this , android.R.anim.fade_out )); // set the image switching resources imageSwitcher.setImageResource (arrayPicture [index]); } the else IF (touchUpX-touchDownX < -100) { // that slides from right to left index = index == arrayPicture.length-0. 1: index. 1 +? ; // set the fade imageSwitcher.setInAnimation (AnimationUtils.loadAnimation ( The MainActivity. The this , android.R.anim.fade_in )); imageSwitcher.setOutAnimation (AnimationUtils.loadAnimation ( the MainActivity. The this , android.R.anim.fade_out )); // set the image switching resources imageSwitcher.setImageResource (arrayPicture [index]); } return to true ; } return to false ; } }); } }